Skip to content

DPredict · Guide test 182

Hausman specification test

Compares estimators that should agree under the null but differ under model misspecification.

Runs in CodeSplashPython (calls R) example not runR example validated

Do it in CodeSplash

In the app, set it up in Analyze with one of these methods:

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

40
6
0.50
0.5
0.40

Not 0: random effects are inconsistent, fixed effects are not.

0.00

A shock shared by every unit each period: cross-sectional dependence.

0.00

Errors carry φ × last period's error.

0.050

Your data

Simulated with known truth

40 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

χ² = 14.30
  1. The reference: how χ² varies across samples when H₀ is true (asymptotic).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = < .001).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

H=(β^FE−β^RE)2V^FE−V^RE∼χ12H = \dfrac{(\hat\beta_{FE} - \hat\beta_{RE})^2}{\widehat V_{FE} - \widehat V_{RE}} \sim \chi^2_1

With your numbers

β^FE=0.37,β^RE=0.45,H=14.30\hat\beta_{FE} = 0.37,\quad \hat\beta_{RE} = 0.45,\quad H = 14.30
β^FE\hat\beta_{FE}
within (fixed-effects) estimate: consistent either way
β^RE\hat\beta_{RE}
random-effects estimate: efficient, but biased if x tracks the unit effect
θ\theta
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 H0.

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

H=(β^C−β^E)T[V^C−V^E]−1(β^C−β^E)H=(\widehat\beta_C-\widehat\beta_E)^T[\widehat V_C-\widehat V_E]^{-1}(\widehat\beta_C-\widehat\beta_E)

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.

Python · invokes R

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()
R

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

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.