[R] partial correlation coefficients in R?

Marcel Wolbers wolbers at stat.math.ethz.ch
Fri Feb 25 15:53:06 CET 2000


Hi Kaspar,

> to the list: Is there a possibility to compute partial correlation
> coefficients between multiple variables 

pcor2 works also generrally (not only for 3 vars):

#pcor from David Lucy (only for 3 vars)
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)
	}

#pcor2
pcor2 <- function(x1,x2){
  #canonical correlation matrix of x1 given x2
  S1.2 <- cov(x1)-cov(x1,x2)%*%solve(cov(x2))%*%cov(x2,x1)
    #partial covariance of x1 given x2
  D1.2.Inv <- diag(1/sqrt(diag(S1.2)))
  partial <- D1.2.Inv%*%S1.2%*%D1.2.Inv #partial correlations
  return(partial)
}

v1 <- rnorm(10)
v2 <- rnorm(10)
v3 <- rnorm(10)
pcor(v1,v2,v3)
> [1] 0.08953366
pcor2(cbind(v1,v2),v3)
>   
             [,1]       [,2]
  [1,] 1.00000000 0.08953366
  [2,] 0.08953366 1.00000000

Best wishes
Marcel

-------------------------------------------------------------------------------
Marcel Wolbers, LEO C14, Seminar for Statistics, ETH Zurich
CH-8092 Zurich, Switzerland; phone: +41 1 632 2252	
http://stat.ethz.ch/~wolbers/
-------------------------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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