Perplexed
Perplexed

Reputation: 13

How can I generate a 5-dimensional multivariate normal

How can I generate a 5-dimensional multivariate normal with names (Y, X1, X2, Z1, Z2), and 5347 observations, with bilateral correlations

Correlations:

Y x1 x2 Z1 Z2
Y 1.0 0.2 0.1 0.35 0.0
x1 0.2 1.0 0.0 0.4 0.0
x2 0.1 0.0 1.0 0.0 0.4
Z1 0.35 0.4 0.0 1.0 0.6
Z2 0.0 0.0 0.4 0.6 1.0

I tried to generate matrix and mvtnorm and MASS package

Upvotes: 1

Views: 1280

Answers (1)

jruf003
jruf003

Reputation: 1042

mvrnorm() from MASS works fine for me (see code below). Having said this, if you input a correlation vs. covariance matrix the random variables output from the function will be on a different scale to the original data (i.e., each will be scaled to have unit variance).

If for some reason you can't compute the covariance matrix but you know your variables' standard deviations, dayne's answer provides a way to get it from the correlation matrix.

library(MASS)

# Make covariance matrix. See note above re the implications of using a correlation matrix.
S = matrix(c(1.0, 0.2, 0.1, 0.35, 0.0,
             0.2, 1.0, 0.0, 0.4, 0.0,
             0.1, 0.0, 1.0, 0.0, 0.4,
             0.35, 0.4, 0.0, 1.0, 0.6,
             0.0, 0.0, 0.4, 0.6, 1.0), ncol = 5)
colnames(S) = c("Y1", "X1", "X2", "Z1" ,"Z2")
rownames(S) = colnames(S)

# Make mean vector
mus = c(1, 2, 3, 4, 5); names(mus) = colnames(S)

# Generate 5347 observations
obs = mvrnorm(n = 5347, mu = mus, Sigma = S)
head(obs)

Upvotes: 1

Related Questions