DPredict · Guide test 178
Davidson–MacKinnon J test
Tests one nonnested regression specification by augmenting it with fitted values from another.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Compare non-nested models (J, Cox, encompassing, PE, Vuong) — Which of two rival regression models explains the outcome better, when neither contains the other?
In Compare non-nested models (J, Cox, encompassing, PE, Vuong), choose Comparison = Two rival models of the outcome.
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 · Davidson–MacKinnon J test
0 makes M1 (y on x) the true model.
Your data
Simulated with known truthTwo rival single-regressor models for the same y. Neither is nested in the other, so an F test cannot compare them directly.
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 = .101).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the model under test
- the rival model
- fitted values of the rival model
The verdict
t(57) = 1.67, p = .101
p > α = .050: compatible with H₀
Test both directions: both models can be rejected, or neither. Rejecting M1 says it misses something M2 has, not that M2 is right.
- Statistic
- 1.668
- p-value
- 0.1008
Davidson–MacKinnon J test on 60 observations of y with rival regressors x and z: t(57) = 1.67, p = .101. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: model A encompasses the fitted predictions from nonnested model B.
Assumptions
Same response and observations; identifiable auxiliary regression and regular errors. Classical inference assumes homoskedasticity, while robust versions need valid covariance asymptotics.
Formula
Parameters and notation
ŷB is the fitted response from rival model B; γ is the added coefficient.
Structure of the method
Fit both nonnested regressions on the same outcome and rows. Add the rival's fitted response as one generated regressor to the focal model and test its coefficient. Reverse the construction to test the other direction. This differs from adding all rival regressors in an encompassing model.
Estimated and nuisance parameters
Each model's regression coefficients and residual covariance are estimated, then an auxiliary coefficient measures information in the rival prediction beyond the focal design. Generated fitted values are dependent on the same sample; the test uses the established asymptotic construction rather than treating them as independent new data.
Settings and design choices
Define the rival specifications, tested direction, covariance estimator, observation set, and any transformation beforehand. Multiple rivals and bidirectional tests create a family of comparisons requiring a reporting or multiplicity plan.
Fitting and adaptation
Refit both candidate models whenever preprocessing or the shared sample changes, and use a supported robust covariance where the relevant asymptotics justify it. Do not replace in-sample rival fits by arbitrary machine-learning predictions while retaining the original calibration without justification. Subsequent model revision should be validated independently.
Sensitivity and robustness
Near-collinearity between rival predictions and focal regressors weakens identification of the auxiliary coefficient. Heteroskedasticity, dependence, and misspecification in both models complicate conclusions. Both directions may reject, or neither may reject; neither outcome automatically ranks predictive accuracy or establishes causality. Compare scientific interpretability and held-out performance separately.
How to use and calibrate
Test γ using the documented t/Wald procedure; repeat with A and B reversed if comparing both encompassing claims.
Example application
Compare two different sets of explanatory variables for the same response.
Interpretation and pitfalls
Watch out
Both models can be rejected or neither rejected. It is not a rule guaranteeing one model wins.
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
Read the directional model comparison carefully: nonnested procedures can reject neither, one, or both models. A specification rejection does not identify the correct replacement model.
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()
from statsmodels.stats.diagnostic import compare_j
print(compare_j(sm.OLS(y,sm.add_constant(x)).fit(),sm.OLS(y,sm.add_constant(z)).fit()))
Output
(np.float64(5.722589247162545), np.float64(1.167555024637619e-07))
Validated — Executed successfully in the validation environment · needs lmtest
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(lmtest::jtest(y~x,y~z,data=d))
Output
J test
Model 1: y ~ x
Model 2: y ~ z
Estimate Std. Error t value Pr(>|t|)
M1 + fitted(M2) 1.0965 0.29079 3.7706 0.0002799 ***
M2 + fitted(M1) 1.0413 0.18530 5.6191 1.837e-07 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Sources
- lmtest — lmtest · jtestAuthor documentation/source reviewed: lmtest 0.9-40.search.r-project.org
- jtestsearch.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 178, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT178.