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.4328938  0.71046741 -0.3545094  0.3296056
#> 2   0.5499955 -0.04515263  0.5200758  0.3126072
#> 3   1.2332014  0.84059575  1.4636452  1.2133793
#> 4   1.5940614  0.33277182  1.1490795  1.5716986
#> 5  -0.5075202 -1.20328029 -1.6145986 -1.5344455
#> 6  -0.7985165 -1.96248137 -1.7310021 -2.4968725
#> 7  -2.6613942 -1.51496840 -0.1958377 -1.1080823
#> 8   1.5503841  0.22264712 -0.4416368  0.7515796
#> 9  -0.1089382 -1.19677372  0.3412923  0.9013702
#> 10 -0.4104137  0.06785565 -0.1272144 -0.8141842

# 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.2612873 -0.4747722
#> B  0.2612873 1.0000000  0.5104176
#> C -0.4747722 0.5104176  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.3562369 -0.4429172
#> B  0.3562369 1.0000000  0.4324168
#> C -0.4429172 0.4324168  1.0000000