rnorm_multi: Multiple correlated normal distributions

Description Usage Arguments Value Examples

View source: R/rnorm_multi.R

Description

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

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 4 10-item vectors each correlated r = .5
rnorm_multi(10, 4, r = 0.5)

# 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)

# 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)

faux documentation built on Sept. 14, 2021, 1:08 a.m.