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.09382122 -0.50664862 -0.521255335 -0.734695416
#> 2   0.89526593  0.88454525  0.757609421  1.473452579
#> 3   1.30449645 -0.31040190  0.404083912 -0.004584287
#> 4  -0.85700886  1.10145390 -0.490797966  0.544807451
#> 5  -0.72615810  0.21564451 -0.004508819  0.887187386
#> 6   0.79510223  1.24553740  1.208772396  0.847168701
#> 7  -1.08686802  0.20542374 -0.001241857  0.193074464
#> 8  -0.39906602  0.53042333 -0.601213552 -0.236948812
#> 9   0.84926756  0.71676114 -0.222803659 -0.006101537
#> 10  0.91817794 -0.07973927  0.020993893  0.886064173

# 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.1992337 -0.5894921
#> B  0.1992337 1.0000000  0.4518370
#> C -0.5894921 0.4518370  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.2478804 -0.5018917
#> B  0.2478804 1.0000000  0.4988469
#> C -0.5018917 0.4988469  1.0000000