Make normally distributed vectors with specified relationships. See vignette("rnorm_multi", package = "faux") for details.

rnorm_multi(
  n = 100,
  vars = NULL,
  mu = 0,
  sd = 1,
  r = 0,
  varnames = NULL,
  empirical = FALSE,
  as.matrix = FALSE,
  seed = NULL
)

Arguments

n

the number of samples required

vars

the number of variables to return

mu

a vector giving the means of the variables (numeric vector of length 1 or vars)

sd

the standard deviations of the variables (numeric vector of length 1 or vars)

r

the correlations among the variables (can be a single number, vars\*vars matrix, vars\*vars vector, or a vars\*(vars-1)/2 vector)

varnames

optional names for the variables (string vector of length vars) defaults if r is a matrix with column names

empirical

logical. If true, mu, sd and r specify the empirical not population mean, sd and covariance

as.matrix

logical. If true, returns a matrix

seed

DEPRECATED use set.seed() instead before running this function

Value

a tbl of vars vectors

Examples

# 4 10-item vectors each correlated r = .5
rnorm_multi(10, 4, r = 0.5)
#>             X1          X2          X3          X4
#> 1   0.01802558  0.25636386  0.58019692  0.08028537
#> 2  -1.82812785 -0.65769098 -2.83027285 -2.21576959
#> 3   0.90812009  0.18597855  1.09394285  0.69308582
#> 4   0.82690811  0.81208392  1.84790155  0.25361022
#> 5  -2.40031154 -1.86456845  0.45984866 -0.10324507
#> 6   0.90321967  0.09842935  0.05955834  0.07273941
#> 7   0.31865535 -1.34698381 -1.17313109 -1.81671727
#> 8  -1.54373252 -1.16776736 -1.42108043  0.02971391
#> 9  -1.41455579 -1.04093303  1.08889055 -0.18530451
#> 10  0.13024824 -0.08992106 -0.02768663 -0.91742788

# set r with the upper right triangle
b <- rnorm_multi(100, 3, c(0, .5, 1), 1, 
                 r = c(0.2, -0.5, 0.5), 
                 varnames=c("A", "B", "C"))
cor(b)
#>             A          B          C
#> A  1.00000000 0.04846831 -0.6560759
#> B  0.04846831 1.00000000  0.4773825
#> C -0.65607589 0.47738251  1.0000000

# set r with a correlation matrix and column names from mu names
c <- rnorm_multi(
  n = 100, 
  mu = c(A = 0, B = 0.5, C = 1),
  r = c( 1,   0.2, -0.5, 
         0.2, 1,    0.5, 
        -0.5, 0.5,  1)
)
cor(c)
#>            A         B          C
#> A  1.0000000 0.2183597 -0.5386588
#> B  0.2183597 1.0000000  0.4736930
#> C -0.5386588 0.4736930  1.0000000