Ning Ma
2009-Aug-29 01:26 UTC
[R] how to generate a random correlation matrix with restrictions
Hi, How can I generate a random 100x100 correlation matrix, R={r_ij}, where about 10% of r_ij are greater than 0.9 Thanks in advance.
Kingsford Jones
2009-Aug-29 01:36 UTC
[R] how to generate a random correlation matrix with restrictions
R <- matrix(runif(10000), ncol=100) hth, Kingsford Jones On Fri, Aug 28, 2009 at 7:26 PM, Ning Ma<pningma at gmail.com> wrote:> Hi, > > How can I generate a random 100x100 correlation matrix, R={r_ij}, > where about 10% of r_ij are greater than 0.9 > > Thanks in advance. > > ______________________________________________ > R-help at 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. >
Kingsford Jones
2009-Aug-29 02:10 UTC
[R] how to generate a random correlation matrix with restrictions
Ahh -- Mark Leeds just pointed out off-list that it was supposed to be a correlation matrix. Perhaps R <- matrix(runif(10000), ncol=100) R <- (R * lower.tri(R)) + t(R * lower.tri(R)) diag(R) <- 1 These are of course uniformly distributed positive correlations. Kingsford On Fri, Aug 28, 2009 at 7:36 PM, Kingsford Jones<kingsfordjones at gmail.com> wrote:> ?R <- matrix(runif(10000), ncol=100) > > hth, > > Kingsford Jones > > On Fri, Aug 28, 2009 at 7:26 PM, Ning Ma<pningma at gmail.com> wrote: >> Hi, >> >> How can I generate a random 100x100 correlation matrix, R={r_ij}, >> where about 10% of r_ij are greater than 0.9 >> >> Thanks in advance. >> >> ______________________________________________ >> R-help at 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. >> >
Giovanni Petris
2009-Aug-31 15:47 UTC
[R] how to generate a random correlation matrix with restrictions
So, you want about 10% of the correlations to be greater than 0.9. And what about the others? It would be helpful to know why you need this kind of matrix and what you are going to do with it. Here is an attempt, based on generating a random variance matrix from a Wishart distribution and transforming it to a correlation matrix. (Package dlm contains a Wishart generator). This will generate all the correlations to be around 0.9.> require(dlm) > n <- 100 > df <- n + 5 > S <- matrix(0.9, n, n) > diag(S) <- 1 > S <- S / df > set.seed(125) > x <- cov2cor(rwishart(df, Sigma = S)) > summary(x[x!=1]) # off-diagonal elementsMin. 1st Qu. Median Mean 3rd Qu. Max. 0.8576 0.9025 0.9116 0.9108 0.9196 0.9525>This is one possibility to obtain just about 10% of the correlations to be around 0.9.> N <- n * (n - 1) / 2 # number of correlations > k <- 0.1 * N # want this number close to 0.9 > n1 <- round(max(Re(polyroot(c(-2*k, -1, 1))))) > S[row(S) > n1 | col(S) > n1] <- 0 > diag(S) <- S[1, 1] > y <- cov2cor(rwishart(df, Sigma = S)) > mean(y[y != 1] > 0.8) # about 10%[1] 0.1002020> summary(y[y != 1 & y > 0.8])Min. 1st Qu. Median Mean 3rd Qu. Max. 0.8630 0.8976 0.9051 0.9045 0.9126 0.9424>Again, with more background info we may be able to give you more meaningful advice. HTH Giovanni> Date: Sat, 29 Aug 2009 09:26:34 +0800 > From: Ning Ma <pningma at gmail.com> > Sender: r-help-bounces at r-project.org > Precedence: list > DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; > DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; > > Hi, > > How can I generate a random 100x100 correlation matrix, R={r_ij}, > where about 10% of r_ij are greater than 0.9 > > Thanks in advance. > > ______________________________________________ > R-help at 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. > >-- Giovanni Petris <GPetris at uark.edu> Associate Professor Department of Mathematical Sciences University of Arkansas - Fayetteville, AR 72701 Ph: (479) 575-6324, 575-8630 (fax) http://definetti.uark.edu/~gpetris/