Skip to contents

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

Usage

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.46894971 -0.8566868  0.8700007 -0.277605284
#> 2   0.71062932 -0.1333277 -0.3604500  0.330803256
#> 3   0.09549815  0.5060484  0.7286468  1.096548802
#> 4  -1.70330820 -0.4282653  0.1207761 -0.668776867
#> 5  -1.78865304  0.5372459  0.3838310 -0.237989406
#> 6  -0.53906501 -0.4029880 -0.6413800 -1.086578963
#> 7   0.75730935  0.3217305  1.1213584 -0.004724092
#> 8   1.95126575  0.7318149  0.2999517  1.302404256
#> 9  -1.33463881  0.8130180 -1.7403755 -0.693447886
#> 10  0.29850924 -1.5535492 -0.9020073 -1.424050271

# 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.0000000 0.2242373 -0.5357229
#> B  0.2242373 1.0000000  0.5216985
#> C -0.5357229 0.5216985  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.0188244 -0.6871065
#> B -0.0188244  1.0000000  0.4922153
#> C -0.6871065  0.4922153  1.0000000