DPredict · Guide test 001
Likelihood ratio test
Compares maximized likelihoods under a restricted null and a larger model; calibration depends on regularity conditions.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- Log-linear models for multiway tables — Which associations among three or more categorical variables are needed to explain their joint counts?
- Linear mixed model (random intercepts and slopes) — How does a numeric outcome depend on predictors when rows are grouped (repeated measures, clusters)?
- Compare nested models (LR, Wald, score, partial F) — Do the added predictors improve the model significantly?
- Count regression (Poisson / negative binomial) — How do predictors change the expected count (or rate) of events?
- Logistic regression (binary outcome) — How do predictors change the odds of a yes/no outcome?
- Cox proportional hazards regression — Which factors are associated with the hazard of the event?
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 · Likelihood ratio test
Adds curve × (x − 5)².
Error SD grows like exp(h · (x − 5)/5).
Errors in collection order carry φ × the previous error.
Your data
Simulated with a known ρIf H₀ were true
- The reference: how LR 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 = < .001).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- log-likelihoods without and with the slope
- residual sums of squares of the two fits
- one restriction tested
The verdict
LR(1) = 14.81, p < .001
p ≤ α = .050: reject H₀
Compare how well the two models explain the data on the likelihood scale. Wald, LR and score tests agree in large samples; in small ones they differ in a fixed order (W ≥ LR ≥ LM).
- Statistic
- 14.809
- p-value
- 0.0001
Likelihood ratio test on 40 (x, y) pairs: LR(1) = 14.81, p < .001. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: θ belongs to a restricted parameter set Θ0; H1: θ is in the larger model but outside Θ0.
Assumptions
The likelihood must represent the sampling process. The models are nested. The usual chi square approximation requires identifiable, interior parameters, smooth likelihood, nonsingular information, and a sufficiently large sample.
Formula
Parameters and notation
ℓ is log likelihood; θ̂ and θ̂0 maximize it in the full and null models; q is the difference in free parameter dimensions.
Structure of the method
A likelihood engine fits the restricted and unrestricted parameter spaces to identical observations, then compares twice the optimized log-likelihood difference. Numerical optimization is part of the computation: check convergence, shared likelihood constants, and whether the restricted optimum is genuinely nested. Regular cases use a chi-square reference with the number of independent restrictions.
Estimated and nuisance parameters
Both models estimate their own nuisance parameters, including residual scale or dispersion when the likelihood requires it. The target restriction and its dimension are not estimated by the test. Record model ranks rather than counting nominal coefficients when the design is aliased.
Settings and design choices
Choose the null restrictions, sampling distribution, significance level, and asymptotic versus null-bootstrap calibration before inspecting significance. Solver tolerances and restart counts are numerical controls. A bootstrap replication budget governs Monte Carlo precision, not model complexity or a learned parameter.
Fitting and adaptation
Adaptation means refitting both models under the same likelihood and observation mask. A null parametric bootstrap simulates from the restricted fit and refits both models in every replicate; keeping alternative-model parameters fixed fails to reproduce the statistic's sampling distribution.
Sensitivity and robustness
Boundary parameters, weak identification, separation, and mixture-component testing can invalidate Wilks' ordinary chi-square law. ML and REML likelihoods cannot be interchanged casually when fixed effects differ. Inspect optimization failures in bootstrap replicates and report Monte Carlo uncertainty for small tail probabilities.
How to use and calibrate
Fit both models to exactly the same observations, compute Λ, and use the upper chi square tail. For boundary or small sample problems use an appropriate mixture law, exact calculation, or null parametric bootstrap.
Example application
To test whether three added predictors improve a logistic model, fit the restricted and expanded models and compare twice their log likelihood difference with χ²3.
Interpretation and pitfalls
Watch out
A variance component equal to zero is a boundary problem. Nonidentifiable mixture components and nonnested models invalidate ordinary Wilks calibration. A large statistic indicates disagreement with restrictions, not a large practical effect.
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
Nested Gaussian regression example; chi-square calibration is asymptotic. Use the same observations in both fits.
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()
print(fit.compare_lr_test(reduced))
Output
CompareLRTestResult(statistic=np.float64(5.360927752495741), pvalue=np.float64(0.020592815666549854), df_diff=np.float64(1.0))
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::lrtest(reduced,fit))
Output
Likelihood ratio test Model 1: y ~ x Model 2: y ~ x + z #Df LogLik Df Chisq Pr(>Chisq) 1 3 -135.58 2 4 -128.74 1 13.678 0.000217 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Sources
- lmtest — lmtest · lrtestAuthor documentation/source reviewed: lmtest 0.9-40.search.r-project.org
- lrtestsearch.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 001, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT001.