Arun Kumar Saha
2008-May-19 06:32 UTC
[R] Converting variance covariance matrix to correlation matrix
Suppose I have a Variance-covariance matrix A. Is there any fast way to calculate correlation matrix from 'A' and vice-versa without emplying any 'for' loop? [[alternative HTML version deleted]]
Peter Dalgaard
2008-May-19 07:02 UTC
[R] Converting variance covariance matrix to correlation matrix
Arun Kumar Saha wrote:> Suppose I have a Variance-covariance matrix A. Is there any fast way to > calculate correlation matrix from 'A' and vice-versa without emplying any > 'for' loop? >C <- cov2cor(A) The other way around is ill-defined, but if d is the vector of variances, d <- sqrt(diag(A)) A <- outer(d, d)*C. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Dimitris Rizopoulos
2008-May-19 07:07 UTC
[R] Converting variance covariance matrix to correlation matrix
check the following: # covariance matrix V <- var(matrix(rnorm(10*4), 10, 4)) cov2cor(V) # correlation matrix R <- cov2cor(V) # vector of std. dev. sds <- rnorm(4)^2 R * sds * rep(sds, each = nrow(R)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Arun Kumar Saha" <arun.kumar.saha at gmail.com> To: "r-help at stat.math.ethz.ch" <R-help at stat.math.ethz.ch> Sent: Monday, May 19, 2008 8:32 AM Subject: [R] Converting variance covariance matrix to correlation matrix> Suppose I have a Variance-covariance matrix A. Is there any fast way > to > calculate correlation matrix from 'A' and vice-versa without > emplying any > 'for' loop? > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Seemingly Similar Threads
- Compute a correlation matrix from an existing covariance matrix
- generating random covariance matrices (with a uniform distribution of correlations)
- Weighted variance function?
- Please help me to combine two datasets.
- Covariance/Correlation matrix for repeated measures data frame