For example , we have a corr matrix for five standardized factors (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
A quicker solution is --
Post a Comment