[R] generating random covariance matrices (with a uniform distribution of correlations)
Ned Dochtermann
ned.dochtermann at gmail.com
Fri Jun 3 01:42:59 CEST 2011
List members,
Via searches I've seen similar discussion of this topic but have not seen
resolution of the particular issue I am experiencing. If my search on this
topic failed, I apologize for the redundancy. I am attempting to generate
random covariance matrices but would like the corresponding correlations to
be uniformly distributed between -1 and 1.
The approach I have been using is:
> st.dev<-.5 #this and k are aspects I would like to vary
> k<-6 #number of... things
> y1<-matrix(rnorm(k^2,mean=0,sd=st.dev),k)
> y1<-t(y1)%*%y1
This produces positive definite symmetrical matrices, which is what I want.
However, the following:
> k<-6; kk<-(k*(k-1))/2; st.dev<-.5
> x<-matrix(0,5000,kk)
> for(i in 1:5000){
> y1<-matrix(rnorm(k^2,mean=0,sd=st.dev),k)
> y1<-t(y1)%*%y1
> y1<-y1/k #this keeps the variances similar across different k's
> y1<-cov2cor(y1)
> x[i,]<- y1[lower.tri(y1)]
> }
> hist(c(x))
demonstrates that the distribution of corresponding correlations is not
uniformly distributed between -1 and 1 (of course there is no reason to
suspect that it would be). I have tried using both rcorrmatrix and
genPositiveDefMat in the "clusterGeneration" package but even when picking
the "unifcorrmat" option and setting "alphad" to 1, the distribution of
correlations is not uniform.
Any recommendations on how to generate the desired covariance matrices would
be appreciated.
Ned
--
Ned A. Dochtermann
Department of Biology
University of Nevada, Reno
ned.dochtermann at gmail.com
http://wolfweb.unr.edu/homepage/mpeacock/Ned.Dochtermann/
http://www.researcherid.com/rid/A-7146-2010
--
More information about the R-help
mailing list