library(groupedHyperframe.random)
# Loading required package: groupedHyperframe45 mvrnorm2()
The examples in Chapter 45 require
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
- parameter \(\sigma\)
sdmay be a numeric scalar (Listing 45.1), indicating an all-equaldiagonal-variance zero-covariance matrix; - parameter \(\sigma\)
sdmay be a numericvector(Listing 45.2) of the same length as parameter \(\mu\)mu, indicating adiagonal-variance zero-covariance matrix; - To specify a full
variance-covariancematrix\(\Sigma\) (Listing 45.3), user may use either functionmvrnorm2()orMASS::mvrnorm()(Venables and Ripley 2002, v7.3.65).
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))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))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))