[R] Efficient calculation of partial correlations in R

John Fox jfox at mcmaster.ca
Tue Mar 31 14:06:58 CEST 2009


Dear Schragi,

There's a function named partial.cor() in the Rcmdr package, but it's so
simple that I'll just reproduce it here:

partial.cor <- function (X, ...) 
{
    R <- cor(X, ...)
    RI <- solve(R)
    D <- 1/sqrt(diag(RI))
    R <- -RI * (D %o% D)
    diag(R) <- 0
    rownames(R) <- colnames(R) <- colnames(X)
    R
}

Of course, this gives you the partial correlation between each pair of
variables controlling for all others, which is I assume what you want.

I hope this helps,
 John


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
> Behalf Of Schragi Schwartz
> Sent: March-31-09 5:21 AM
> To: r-help at r-project.org
> Cc: 'Dror Hollander'
> Subject: [R] Efficient calculation of partial correlations in R
> 
> Hello,
> 
> I'm looking for an efficient function for calculating partial
correlations.
> I'm currently using the pcor.test () function, which is equivalent to the
> cor.test() function, and can receive only single vectors as input. I'm
> looking for something which is equivalent to the cor() function, and can
> receive matrixes as input (which should make the calculations much more
> efficient).
> 
> 
> 
> Thanks,
> 
> Schragi
> 
> 
> 
> 
> 	[[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.




More information about the R-help mailing list