Converting variance covariance matrix to correlation matrix

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting variance covariance matrix to correlation matrix

Arun.stat
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting variance covariance matrix to correlation matrix

Peter Dalgaard
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
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting variance covariance matrix to correlation matrix

Dimitris Rizopoulos
In reply to this post by Arun.stat
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" <[hidden email]>
To: "[hidden email]" <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting variance covariance matrix to correlation matrix

Peter Dalgaard
In reply to this post by Peter Dalgaard
Peter Dalgaard wrote:
> 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,
Doh. Edited code, but not text... Vector of standard deviations, of course.
>
> 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
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] 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.