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   1.2118766  0.2572275  0.1571110  0.2575190
#> 2   0.2280110 -1.7824349 -0.4284778  0.3753033
#> 3  -2.8143796 -1.1818529 -2.9805132 -1.0861017
#> 4  -0.2597614 -1.0891385 -0.3931237 -0.8069949
#> 5  -0.3351507  1.5534359 -0.2864572  0.5937983
#> 6   0.8203114  1.0546222  1.4885085 -0.1421187
#> 7  -0.3444536  0.6700475 -0.2216156  0.6004805
#> 8   2.5177336  1.8905935  0.3940677  2.5061463
#> 9   0.3361325  0.3826101 -0.2626767  0.9996123
#> 10  0.9220443  1.2097444  0.8996677  1.0698033

# 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.1870509 -0.6016125
#> B  0.1870509 1.0000000  0.4327252
#> C -0.6016125 0.4327252  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.1636009 -0.4987858
#> B  0.1636009 1.0000000  0.5240712
#> C -0.4987858 0.5240712  1.0000000