45  mvrnorm2()

The examples in Chapter 45 require

library(groupedHyperframe.random)
# Loading required package: groupedHyperframe

Function groupedHyperframe.random::mvrnorm2() is a wrapper of the multivariate normal simulator function MASS::mvrnorm() (Venables and Ripley 2002, v7.3.65) to accept the standard deviation(s) \(\sigma\) via parameter sd

Listing 45.1: Example: function mvrnorm2(), scalar \(\sigma\)
set.seed(12); a1 = MASS::mvrnorm(n = 3L, mu = c(0, 0), Sigma = diag(x = .9^2, nrow = 2L))
set.seed(12); a2 = mvrnorm2(n = 3L, mu = c(0, 0), sd = .9)
stopifnot(identical(a1, a2))
Listing 45.2: Example: function mvrnorm2(), vector \(\sigma\)
set.seed(42); b1 = MASS::mvrnorm(n = 3L, mu = c(0, 0), Sigma = diag(x = c(.9, 1.1)^2, nrow = 2L))
set.seed(42); b2 = mvrnorm2(n = 3L, mu = c(0, 0), sd = c(.9, 1.1))
stopifnot(identical(b1, b2))
Listing 45.3: Example: function mvrnorm2(), matrix \(\Sigma\)
R = matrix(c(1, .5, .5, 1), nrow = 2L) # correlation matrix
S = c(.9, 1.1) * R * rep(c(.9, 1.1), each = 2L) # variance-covariance matrix
set.seed(23); c1 = MASS::mvrnorm(n = 3L, mu = c(0, 0), Sigma = S)
set.seed(23); c2 = mvrnorm2(n = 3L, mu = c(0, 0), Sigma = S)
stopifnot(identical(c1, c2))