6

For instance if I were trying to relate Age and Height, I would do something like this:

df <- data.frame(Age, Height)
cor(df)

How could I get Pearson's r value and the p-value for the relationship between Age and Height while accounting for the effect of Gender?

Tim
  • 108,699
  • 20
  • 212
  • 390
Rilcon42
  • 739
  • 2
  • 9
  • 18
  • That was exactly what I needed. I had never heard the phrase "partial correlation" before. If you will propose that as the answer (preferably with an example of usage) I will accept it. – Rilcon42 Sep 24 '15 at 17:54
  • 2
    The problem here was not so much "how to use R" but how to find a correlation coefficient while taking another variable into account, which suggests it ought to be on-topic. (I wonder whether we have another thread on partial correlation that this duplicates.) – Silverfish Sep 24 '15 at 21:49

1 Answers1

8

This is called partial correlation, basically it, as Wikipedia notices,

measures the degree of association between two random variables, with the effect of a set of controlling random variables removed

Having correlation coefficients of three variables $X$, $Y$ and $Z$ we can correct correlation $\rho_{XY}$ by controlling correlations of $X$ and $Y$ with $Z$:

$$ \rho_{XY \cdot Z } = \frac{\rho_{XY} - \rho_{XZ}\rho_{ZY}} {\sqrt{(1-\rho_{XZ}^2) (1-\rho_{ZY}^2)}} $$

This is related to multiple linear regression. To test the hypothesis about partial correlation you use $t$-statistic in similar fashion as with regular correlation but with $N-3$ degrees of freedom. The same formula can be used for Pearson's or Spearman's correlation coefficients (for more see Altman, 1991 or Revelle, n.d.).

There are multiple R functions and libraries that enable you to calculate partial correlations like pcor, ggm or psych.


Altman, D.G. (1991). Practical Statistics for Medical Research. Chapman and Hall, pp. 288-299.

Revelle, W. (n.d.). Multiple Correlation and Multiple Regression In: An introduction to psychometric theory with applications in R.

Tim
  • 108,699
  • 20
  • 212
  • 390
  • I think this *could* be on topic if it is more about how this can be done / what it means, as opposed to what R code to use. Can you say more about what kind of thing a partial correlation is & how it works? – gung - Reinstate Monica Sep 24 '15 at 18:10