[R] how to generate a random correlation matrix with restrictions
Giovanni Petris
GPetris at uark.edu
Mon Aug 31 17:47:02 CEST 2009
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 elements
Min. 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/
More information about the R-help
mailing list