Make normally distributed vectors with specified relationships. See vignette("rnorm_multi", package = "faux")
for details.
rnorm_multi( n = 100, vars = NULL, mu = 0, sd = 1, r = 0, varnames = NULL, empirical = FALSE, as.matrix = FALSE, seed = NULL )
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 |
a tbl of vars vectors
# 4 10-item vectors each correlated r = .5 rnorm_multi(10, 4, r = 0.5)#> X1 X2 X3 X4 #> 1 0.61095292 -1.26859860 -1.0473425 -0.93833481 #> 2 1.00786700 -0.01045278 0.8011351 0.07257914 #> 3 0.82667232 1.04795522 0.4644770 -0.28091812 #> 4 -0.05394007 -1.44029737 -0.6811783 -0.30994736 #> 5 -1.41605036 -1.64280302 -0.4406393 -1.01325211 #> 6 0.46223117 2.18896422 1.5182443 0.46917235 #> 7 0.78039022 0.39089482 -0.2903739 -0.13676273 #> 8 0.63844483 0.07108052 0.5376613 -0.53491812 #> 9 0.72890749 -1.52370263 0.8834245 0.55849515 #> 10 -0.04290407 -0.28051289 -0.5307138 1.53489728# 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.3994863 -0.5147966 #> B 0.3994863 1.0000000 0.3896981 #> C -0.5147966 0.3896981 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.1615419 -0.5165878 #> B 0.1615419 1.0000000 0.5451974 #> C -0.5165878 0.5451974 1.0000000