Skip to content

R: Simulating multiple normal distribution with any given corr matrix

For example , we have a corr matrix for five standardized factors \left[\begin{array}{c}1.00\;0.42\;0.41\;0.55\;0.42\\ 0.42\;1.00\;0.48\;0.47\;0.46\\ 0.41\;0.48\;1.00\;0.48\;0.44\\ 0.55\;0.47\;0.48\;1.00\;0.50\\ 0.42\;0.46\;0.44\;0.50\;1.00\end{array}\right] (Hau, Chinese Textbook, pp. 49-50).

m_corr=c(1, .42, .41, .55, .42);
m_corr=cbind(m_corr, c(.42, 1,.48, .47, .46));
m_corr=cbind(m_corr, c(.41, .48,1, .48, .44));
m_corr=cbind(m_corr, c(.55, .47,.48, 1, .50));
m_corr=cbind(m_corr, c(.42, .46,.44, .50, 1));
m_corr;
## show the original corr matrix
dp=svd(m_corr);
plot(dp$d,type="o"); ## show the scree plot if there is a PC analysis
diag(dp$d); ## show the diagonal matrix of eigen values
dp$u %*% diag(dp$d) %*% t(dp$u); ## show corr matrix
P=dp$u %*% diag(sqrt(dp$d));
P %*% t(P); ## show corr matrix again
n=300; ## typical sample size, you can also try n=30000
f_five=matrix(rnorm(n*5),nrow=n); ## independant multiple normal distribution
f_five=f_five %*% t(P); ## correlated
cor(f_five); ## test the sample corr matrix

{ 1 } Comments

  1. lixiaoxu | November 9, 2007 at 4:42 am | Permalink

    A quicker solution is --

    require(MASS);example(mvrnorm);

Post a Comment

Your email is never published nor shared. Required fields are marked *


To prove you're a person (not a spam script), type the answer to the math equation shown in the picture.
Anti-spam equation