draw_multivariate: Draw multivariate random variables

View source: R/draw_multivariate.R

draw_multivariateR Documentation

Draw multivariate random variables

Description

Draw multivariate random variables

Usage

draw_multivariate(formula, sep = "_")

Arguments

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.

Value

tibble

Examples


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


fabricatr documentation built on June 29, 2022, 5:05 p.m.