DPredict · Guide test 224
Breusch–Pagan LM test for cross-sectional dependence
Tests cross-sectional residual independence using a sum of squared pairwise residual correlations. This is distinct from the Breusch–Pagan test of panel random-effect variances and from the heteroskedasticity test.
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 · Breusch–Pagan LM test for cross-sectional dependence
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 truth8 units over 12 periods. Lines offset from each other are unit effects (SD of the unit means 0.86); 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 LM 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 = .002).
- 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
The verdict
LM(28) = 53.78, p = .002
p ≤ α = .050: reject H₀
Squares do not cancel, but the χ² approximation needs T large relative to N; with many units and short panels it over-rejects (use CD).
- Statistic
- 53.775
- p-value
- 0.0024
Breusch–Pagan LM test for cross-sectional dependence on a balanced panel of 8 units × 12 periods: LM(28) = 53.78, p = .002. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: no pairwise cross-sectional residual correlations.
Assumptions
Classical LM asymptotics take fixed N and large T; model residual assumptions and time overlap must be appropriate.
Formula
Parameters and notation
N panel units,T time periods. Scaled/bias-corrected variants target other panel asymptotic regimes.
Structure of the method
The formula and code in the baseline are the Breusch–Pagan LM test of cross-sectional residual independence: square each pairwise correlation, sum, and multiply by the common time length in a balanced panel. This prevents positive and negative correlations canceling, unlike the signed CD statistic.
Estimated and nuisance parameters
Estimate panel/time-series regression coefficients and residual correlations. The statistic does not estimate or test a random-intercept variance in this implementation. A different Breusch–Pagan procedure for individual/time effects uses pooled-model residuals and is available through plmtest(type="bp"), rather than pcdtest(test="lm").
Settings and design choices
Choose the residual model, classical versus scaled/bias-corrected LM variant, pairwise overlap treatment, and any local-pair restriction. Classical chi-square calibration targets fixed cross-sectional dimension and large time length. With many units, the number of squared correlations grows rapidly and asymptotic bias can dominate.
Fitting and adaptation
Refit the relevant mean model and recompute residual correlations after changing covariates or panel membership. Do not select a random-effects model based on this residual-independence test alone. If the intended question is a zero variance component, explicitly use the separate panel-effects procedure with its own null, statistic, and reference.
Sensitivity and robustness
Review short T, large N, heteroskedastic innovations, unbalanced overlaps, and residual-estimation effects. Squaring correlations avoids sign cancellation but does not identify the dependence mechanism. Preserve the supplied pcdtest code only after relabeling the entry consistently; otherwise the title and analysis would answer different questions.
How to use and calibrate
Use the upper chi square tail for the classical balanced test; specify any large-panel correction.
Example application
Detect panel dependence even when correlation signs differ.
Interpretation and pitfalls
Watch out
This Breusch–Pagan LM is not the heteroskedasticity test in entry 188. Large N can invalidate the classical reference.
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.
- Rewritten in the review: name, purpose, formula.
Current H0, statistic, and pcdtest(test="lm") code all test residual cross-sectional dependence, whereas the inherited title names a different random-effects variance-component procedure.
Corrected text
Breusch–Pagan LM test for cross-sectional dependence
Purposesearch.r-project.org
Align purpose with the formula and preserved code; panel variance effects use plmtest(type="bp").
Corrected text
Tests cross-sectional residual independence using a sum of squared pairwise residual correlations. This is distinct from the Breusch–Pagan test of panel random-effect variances and from the heteroskedasticity test.
Formulasearch.r-project.org
Remove stray comma in squared-correlation exponent while preserving the classical balanced-panel dependence statistic.
Corrected text
LM=T\sum_{i<j}\widehat\rho_{ij}^{2}\approx\chi^2_{N(N-1)/2}
Python and R
This example follows the entry’s formula: cross-sectional residual dependence. The original entry 224 title/purpose says “panel effects,” which is a different Breusch–Pagan test; use plm::plmtest(...,type="bp") for random-effects variance testing.
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="lm"))
"""
# 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="lm"))
Output
Breusch-Pagan LM test for cross-sectional dependence in panels data: y ~ x chisq = 190.99, df = 190, p-value = 0.4661 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 224, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT224.