A utility function for producing random regressors with a specified number of degrees of freedom.

rand(df = 1, rdist = rnorm, args = list(), nrow, seed = NULL)

Arguments

df

degrees of freedom, i.e., number of random regressors

rdist

random distribution function for sampling

args

arguments for rdist

nrow

number of rows in resulting matrix. This can often be omitted in the context of functions like lm where it is inferred from the data frame, if one is provided.

seed

seed for random number generation

Value

A matrix of random variates with df columns. In its intended use, the number of rows will be selected to match the size of the data frame supplied to lm

Examples

rand(2,nrow=4)
#>            [,1]       [,2]
#> [1,] -0.5286604 -0.6921394
#> [2,]  0.1539762 -0.3328700
#> [3,]  0.2783598  0.6448425
#> [4,] -1.2390959  1.0156624
rand(2,rdist=rpois, args=list(lambda=3), nrow=4)
#>      [,1] [,2]
#> [1,]    3    7
#> [2,]    4    2
#> [3,]    2    3
#> [4,]    5    2
summary(lm( waiting ~ eruptions + rand(1), faithful))
#> 
#> Call:
#> lm(formula = waiting ~ eruptions + rand(1), data = faithful)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -12.030  -4.438   0.176   3.902  16.198 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 33.49828    1.16009  28.875   <2e-16 ***
#> eruptions   10.72191    0.31654  33.872   <2e-16 ***
#> rand(1)      0.09474    0.34365   0.276    0.783    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 5.924 on 269 degrees of freedom
#> Multiple R-squared:  0.8115,	Adjusted R-squared:  0.8101 
#> F-statistic: 579.1 on 2 and 269 DF,  p-value: < 2.2e-16
#>