CAssociation · Guide test 080
Partial correlation test
Tests correlation between two variables after accounting for specified covariates.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Partial and semipartial correlation — Are two numeric variables still associated once covariates are held constant?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Partial correlation test
Your data
Simulated with known correlations| # | x | y | z |
|---|---|---|---|
| 1.000 | −0.916 | −0.647 | −1.232 |
| 2.000 | −0.033 | 0.767 | 0.738 |
| 3.000 | 0.201 | −0.080 | 0.304 |
| 4.000 | 0.307 | 0.550 | −0.976 |
| 5.000 | 0.762 | 0.801 | 1.769 |
| 6.000 | 0.442 | 0.315 | −1.197 |
| 7.000 | 0.207 | −0.628 | −0.497 |
| 8.000 | 2.330 | 0.517 | 0.813 |
| 9.000 | −0.092 | 0.468 | −0.162 |
| 10.000 | 1.696 | 1.219 | 0.293 |
| 11.000 | −0.139 | −0.809 | 0.222 |
| 12.000 | −0.305 | −0.765 | −0.061 |
40 people measured on x, y and z · first 12 of 40 rows
If H₀ were true
- The reference: how t varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .001).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the three pairwise Pearson correlations
- df: one more lost for the covariate
The verdict
t(37) = 3.53, p = .001, r_xy·z = 0.50
p ≤ α = .050: reject H₀
Effect size: partial correlation r_xy·z = 0.502
The correlation of what is left of x and y after regressing each on z. A partial correlation is about linear adjustment; it cannot fix a covariate measured with error.
- Statistic
- 3.529
- p-value
- 0.0011
Partial correlation test on 40 people measured on x, y and z: t(37) = 3.53, p = .001, r_xy·z = 0.50. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: partial correlation between X,Y controlling c variables equals zero.
Assumptions
Independent multivariate observations; multivariate normality or a correct linear error model for the classical t test; full rank covariate design.
Formula
Parameters and notation
eX,eY are residuals after regressing each variable on the same c controls and an intercept; df=n−c−2.
Structure of the method
Regress each target variable on the same control design with an intercept, correlate the two residual vectors, and use a t reference with the number of fitted control directions deducted. The equivalent regression coefficient test requires the same observations and design matrix.
Estimated and nuisance parameters
Estimate two nuisance regression coefficient vectors, residual variation, and their correlation. The number c in n-c-2 denotes independent control variables beyond the intercept; redundant columns reduce effective rank. Penalized or nonlinear residual models do not inherit this simple degrees-of-freedom formula.
Settings and design choices
Prespecify controls, transformations, missing-data policy, and the linear partial-correlation target. Avoid choosing covariates by which set produces a significant association. Check design rank and residual variance before calculating the statistic; report the actual controls alongside the effect estimate.
Fitting and adaptation
Every new sample requires refitting both nuisance regressions before residual correlation. Naively permuting fitted residuals can be invalid unless the residual-permutation scheme matches the model assumptions. Fine-tuning is inapplicable; flexible nuisance learning requires a separately justified conditional-independence or orthogonalization framework.
Sensitivity and robustness
Multicollinearity and many controls consume information and destabilize estimates. Collider adjustment can create association, and zero partial correlation does not generally establish conditional independence. Heteroskedasticity or nonnormal dependence may invalidate the classical reference even when residualization is computationally correct.
How to use and calibrate
Compute residual correlation and its t tail; report the conditional association and covariates.
Example application
Examine the association of two measurements after linear adjustment for age.
Interpretation and pitfalls
Watch out
Controlling a collider can introduce bias. Zero partial correlation generally does not imply conditional independence outside Gaussian models.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
One control variable; degrees of freedom n−3. Residual correlation alone with an ordinary Pearson p-value would use the wrong degrees of freedom.
Validated — Executed successfully in the validation environment · needs numpy, scipy, statsmodels
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
import statsmodels.api as sm
n=100
x=rng.normal(size=n); z=rng.normal(size=n); y=1+.5*x+.3*z+rng.normal(size=n)
X=sm.add_constant(np.column_stack([x,z])); fit=sm.OLS(y,X).fit(); reduced=sm.OLS(y,X[:,:2]).fit()
rx=sm.OLS(x,sm.add_constant(z)).fit().resid
ry=sm.OLS(y,sm.add_constant(z)).fit().resid
r=np.corrcoef(rx,ry)[0,1]; df=n-3
t=r*np.sqrt(df/(1-r*r))
print(r,t,2*stats.t.sf(abs(t),df))
Output
0.5023913948848578 5.722589247162542 1.1675550246376308e-07
Validated — Executed successfully in the validation environment · needs ppcor
set.seed(2026)
n <- 100; x <- rnorm(n); z <- rnorm(n); y <- 1+.5*x+.3*z+rnorm(n)
d <- data.frame(y,x,z); fit <- lm(y~x+z,data=d); reduced <- lm(y~x,data=d)
print(ppcor::pcor.test(x,y,z))
Output
estimate p.value statistic n gp Method 1 0.4955556 1.8371e-07 5.619145 100 1 pearson
Sources
- ppcor — ppcor · pcor.testAuthor documentation/source reviewed: ppcor 1.1.search.r-project.org
- pcor.testsearch.r-project.org
- Statistics guide v4 (historical):
- Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 080, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT080.