DPredict · Guide test 223
Pesaran CD test
Tests cross sectional dependence using pairwise residual correlations.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Panel data models (fixed effects, random effects, first differences) — When the same units are measured repeatedly, how does the outcome change when a unit's own treatment changes?
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 · Pesaran CD test
Not 0: random effects are inconsistent, fixed effects are not.
A shock shared by every unit each period: cross-sectional dependence.
Errors carry φ × last period's error.
Your data
Simulated with known truth15 units over 6 periods. Lines offset from each other are unit effects (SD of the unit means 0.98); lines that rise and fall together share a common shock; smooth runs within a line are serial correlation.
If H₀ were true
- The reference: how CD varies across samples when H₀ is true (asymptotic).
- 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
- correlation of the within residuals of units i and j over time
The verdict
CD = 10.66, p < .001
p ≤ α = .050: reject H₀
CD averages the correlations, so positive and negative ones cancel; it suits large N with small T.
- Statistic
- 10.658
- p-value
- 0.0000
Pesaran CD test on a balanced panel of 15 units × 6 periods: CD = 10.66, p < .001. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: cross-sectional independence, or the weaker null supported by the selected CD variant.
Assumptions
Residuals from suitable panel models; sufficient time overlap and regularity. The original balanced formula assumes comparable T.
Formula
Parameters and notation
ρ̂ij is the pairwise residual correlation; unbalanced versions weight each pair by √Tij.
Structure of the method
Fit an appropriate model for each unit or a stated consistent panel model, compute residual correlations over pairwise overlapping times, and aggregate signed correlations with the CD normalization. Balanced and unbalanced formulas differ in their time-overlap weighting. This is a residual dependence diagnostic, not a variance-component test.
Estimated and nuisance parameters
Estimate upstream regression coefficients and unit residual scales/correlations. There is no learned network or standalone common-shock coefficient in the classical CD statistic. The choice of pooled, within, or heterogeneous residual model affects what dependence remains and must be identified alongside the statistic.
Settings and design choices
Choose CD versus scaled/bias-adjusted variants, global versus specified local pairs, deterministic/common time effects, and overlap inclusion rules. Signed aggregation can cancel positive and negative correlations, so also examine average absolute correlation or suitable complementary tests without silently substituting their null calibration.
Fitting and adaptation
Refit the residual model when changing controls or panel construction. Adding time effects may remove a shared observed pattern but can also alter residual dependence properties. Select a version valid for the actual asymptotic regime; a residual-correlation heatmap is a useful diagnostic but does not itself supply corrected p values.
Sensitivity and robustness
Check missingness, short pairwise overlap, high N relative to T, estimated fixed effects, and mixed-sign common-factor loadings. A rejection may signal omitted common shocks or other specification failures. Nonrejection is especially weak evidence when correlations cancel or a preliminary model has overfit shared variation.
How to use and calibrate
Use an asymptotic standard normal two sided test; report the CD variant.
Example application
Check whether panel residuals retain common shocks.
Interpretation and pitfalls
Watch out
Positive and negative correlations can cancel. Estimated fixed effects and large N/T regimes can require adjusted CD procedures.
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
Two-sided Pesaran CD tests cross-sectional residual dependence; models must be aligned across panel units.
Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: plm
# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
N<-20;TT<-25;d<-expand.grid(time=1:TT,id=1:N);d$x<-rnorm(nrow(d));a<-rnorm(N);d$y<-.5*d$x+a[d$id]+rnorm(nrow(d));pd<-plm::pdata.frame(d,index=c("id","time"))
fit<-plm::plm(y~x,pd,model="within");print(plm::pcdtest(fit,test="cd"))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
text=True, capture_output=True)
print(result.stdout)
if result.stderr:
print(result.stderr)
result.check_returncode()
Validated — Executed successfully in the validation environment · needs plm
set.seed(2026)
N<-20;TT<-25;d<-expand.grid(time=1:TT,id=1:N);d$x<-rnorm(nrow(d));a<-rnorm(N);d$y<-.5*d$x+a[d$id]+rnorm(nrow(d));pd<-plm::pdata.frame(d,index=c("id","time"))
fit<-plm::plm(y~x,pd,model="within");print(plm::pcdtest(fit,test="cd"))
Output
Pesaran CD test for cross-sectional dependence in panels data: y ~ x z = 0.47764, p-value = 0.6329 alternative hypothesis: cross-sectional dependence
Sources
- plm — plm · pcdtestAuthor documentation/source reviewed: plm 2.6-7.search.r-project.org
- pcdtestsearch.r-project.org
- pdata.framesearch.r-project.org
- plmsearch.r-project.org
- Statistics guide v4 (historical):
- Python: Executed end to end through Rscript · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 223, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT223.