[R] Constructing correlation matrices

Horace Tso Horace.Tso at pgn.com
Mon Jul 30 23:41:12 CEST 2007


I take it that you're trying to generate a random correlation matrix, so first create a covariance matrix,

p = 6
v = matrix(rnorm(p*p), ncol=p)
cov = t(v) %*% v

Then convert it to a correlation matrix,




>>> Gregory Gentlemen <gregory_gentlemen at yahoo.ca> 7/29/2007 7:31:36 PM >>>

I have a seemingly simple task which I have not been able to solve today and I checked all of the help archives on this and have been unable to find anything useful. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it:

p <- 6
Rmat <- diag(p)
dat.cor <- rnorm(p*(p-1)/2)
Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor

However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric.

I'd be grateful for any adive and/or solutions.


	[[alternative HTML version deleted]]

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list