View source: R/draw_multivariate.R
draw_multivariate | R Documentation |
Draw multivariate random variables
draw_multivariate(formula, sep = "_")
formula |
Formula describing the multivariate draw. The lefthand side is the names or prefix and the right-hand side is the multivariate draw function call, such as mvrnorm from the MASS library or rmnom from the extraDistr library. |
sep |
Separator string between prefix and variable number. Only used when a single character string is provided and multiple variables created. |
tibble
library(MASS)
# draw from multivariate normal distribution
dat <-
draw_multivariate(c(Y_1, Y_2) ~ mvrnorm(
n = 500,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
cor(dat)
# equivalently, you can provide a prefix for the variable names
# (easier if you have many variables)
draw_multivariate(Y ~ mvrnorm(
n = 5,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
# within fabricate
fabricate(
N = 100,
draw_multivariate(c(Y_1, Y_2) ~ mvrnorm(
n = N,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
)
# You can also write the following, which works but gives less control over the names
fabricate(N = 100,
Y = mvrnorm(
n = N,
mu = c(0, 0),
Sigma = matrix(c(1, 0.5, 0.5, 1), 2, 2)
))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.