On Thu, Jun 02, 2011 at 04:42:59PM -0700, Ned Dochtermann wrote: > 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.
Hello. Let me suggest the following procedure. 1. Generate a symmetric matrix A with the desired distribution of the non-diagonal elements and with zeros on the diagonal. 2. Compute the smallest eigenvalue lambda_1 of A. 3. Replace A by A + t I, where I is the identity matrix and t is a number such that t + lambda_1 > 0. The resulting matrix will have the same non-diagonal elements as A, but will be positive definite. Petr Savicky. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.