DPredict · Guide test 182
Hausman specification test
Compares estimators that should agree under the null but differ under model misspecification.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- Instrumental variables (2SLS / LIML / GMM) — What is the effect of a treatment that is not randomly assigned, using a variable that shifts the treatment but affects the outcome only through it?
- 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 · Hausman specification 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 truth40 units over 6 periods. Lines offset from each other are unit effects (SD of the unit means 0.84); 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 χ² 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
- within (fixed-effects) estimate: consistent either way
- random-effects estimate: efficient, but biased if x tracks the unit effect
- the share of the unit mean random effects subtracts
The verdict
χ²(1) = 14.30, p < .001
p ≤ α = .050: reject H₀
Not rejecting does not make random effects right. In small panels with large unit effects, V̂_FE − V̂_RE is estimated with noise and the test over-rejects; a Mundlak (correlated random effects) version is the robust alternative.
- Statistic
- 14.300
- p-value
- 0.0002
Hausman specification test on a balanced panel of 40 units × 6 periods: χ²(1) = 14.30, p < .001. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: the efficient estimator is consistent; both estimators estimate the same parameter.
Assumptions
One estimator is consistent under both hypotheses; the other efficient under H0 but inconsistent under H1; covariance difference valid and estimable.
Formula
Parameters and notation
C labels the robustly consistent estimator; E the null-efficient estimator. q is rank of the tested difference.
Structure of the method
Compute two estimators of the same parameter vector, take their coefficient difference, and scale it by the covariance of that difference. The classical covariance subtraction relies on the null-efficient estimator's special relationship to the consistent estimator. An auxiliary-regression version offers a different route to the same specification question under its assumptions.
Estimated and nuisance parameters
Both coefficient vectors and the relevant covariance/difference covariance are estimated. In a panel example, within and random-effects estimators also involve different handling of individual effects and variance components. Tested rank excludes coefficients not jointly identified in both models.
Settings and design choices
Define the estimand, overlapping coefficient set, panel indexing, effects structure, estimator pair, and classical versus robust auxiliary formulation. The plm documentation specifically limits its robust covariance option to the appropriate auxiliary-regression route.
Fitting and adaptation
Fit both estimators on the same sample and align coefficients explicitly. If the covariance difference is indefinite, diagnose the assumptions/numerics or use a justified alternative formulation; taking an absolute value or arbitrary inverse is not a correction. Refitting after covariate selection requires acknowledging the selection process.
Sensitivity and robustness
Weak identification, short panels, heteroskedasticity, serial correlation, and different samples can make the classical comparison misleading. A negative quadratic result reflects a problematic covariance formulation, not unusually strong evidence for consistency. Nonrejection does not establish that random effects are independent of regressors or that either estimator identifies a causal effect.
How to use and calibrate
Use χ²q asymptotically. Robust regression-based formulations may be preferable when the covariance difference is not positive semidefinite.
Example application
Compare fixed and random effects under their stated assumptions.
Interpretation and pitfalls
Watch out
A negative classical statistic indicates covariance/formulation problems, not evidence for H0. Compare the same estimand.
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
Classical fixed-versus-random effects Hausman test; covariance differences can be indefinite. Robust auxiliary-regression versions are separate choices.
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"))
fe<-plm::plm(y~x,pd,model="within");re<-plm::plm(y~x,pd,model="random");print(plm::phtest(fe,re))
"""
# 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"))
fe<-plm::plm(y~x,pd,model="within");re<-plm::plm(y~x,pd,model="random");print(plm::phtest(fe,re))
Output
Hausman Test data: y ~ x chisq = 2.1877, df = 1, p-value = 0.1391 alternative hypothesis: one model is inconsistent
Sources
- plm — plm · phtestAuthor documentation/source reviewed: plm 2.6-7.search.r-project.org
- phtestsearch.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 182, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT182.