[R] partial correlation coefficients in R?

Martyn Plummer plummer at iarc.fr
Fri Feb 25 14:35:31 CET 2000


On 25-Feb-00 Dave Lucy wrote:
> Kaspar,
> 
>> to the list: Is there a possibility to compute partial correlation
>> coefficients between multiple variables (correlation between two paired
> 
> I use this for partialing the three variable case, if you find any which
> will handle more varibles let me know:
> 
># pcor - partial correlation routine invoked by tspcor and the like
># calculates the partial correlation coefficient between v1 and v2
># controlling for v3 - returns that value
> 
> pcor <- function(v1, v2, v3)
>       {
>       c12 <- cor(v1, v2)
>       c23 <- cor(v2, v3)
>       c13 <- cor(v1, v3)
> 
>       partial <- (c12-(c13*c23))/(sqrt(1-(c13^2)) * sqrt(1-(c23^2)))
> 
>       return(partial)
>       }

In general you invert the variance-covariance matrix and then rescale it
so the diagonal is one.  The off-diagonal elements are the negative
partial correlation coefficients given all other variables.

pcor2 <- function(x){
        conc <- solve(var(x))
        resid.sd <- 1/sqrt(diag(conc))
        pcc <- - sweep(sweep(conc, 1, resid.sd, "*"), 2, resid.sd, "*")
        return(pcc)
}

pcor2(cbind(x1,x2,x3))


J. Whittaker's book "Graphical models in applied multivariate statistics"
is a good reference for the theory behind this.

Martyn
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list