EProportions · Guide test 059
Likelihood ratio G test for tables
Tests independence or specified frequencies using observed versus expected counts in a likelihood ratio statistic.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- χ² test of independence — Are two categorical variables associated?
- Log-linear models for multiway tables — Which associations among three or more categorical variables are needed to explain their joint counts?
- Goodness of fit (χ² / G test) — Do the observed category frequencies match expected proportions?
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 G test for tables
0 = rows and columns independent.
Your data
Edit the counts| Low | Mid | High | Total | |
|---|---|---|---|---|
| Row A | 25E = 18.5 | 18E = 23.3 | 25E = 26.2 | 68 |
| Row B | 13E = 19.5 | 30E = 24.7 | 29E = 27.8 | 72 |
| Total | 38 | 48 | 54 | 140 |
E is the count expected if rows and columns were unrelated.
If H₀ were true
- The reference: how G 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 = .029).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- observed and expected counts
The verdict
G(2) = 7.07, p = .029
p ≤ α = .050: reject H₀
Asymptotically identical to Pearson's χ²; G is additive across nested tables, which makes it the statistic of log-linear models.
- Statistic
- 7.069
- p-value
- 0.0292
Likelihood ratio G test for tables on a 2 × 3 table of 140 counts: G(2) = 7.07, p = .029. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: a specified categorical independence or homogeneity model holds.
Assumptions
Independent counts under the relevant multinomial/Poisson sampling model; adequate sample size and regular fitted expectations.
Formula
Parameters and notation
E are expected counts fitted under H0; zero observed cells contribute zero; df is the difference in model dimensions.
Structure of the method
Fit a categorical null model, compare its log likelihood with the saturated table likelihood, and multiply the difference by two. Under independence, fitted margins supply the expected cells. Zero observed counts contribute zero through the limiting value of x log x.
Estimated and nuisance parameters
Estimated parameters are the null-model cell probabilities or loglinear coefficients; the saturated alternative has one probability per admissible cell subject to normalization. Degrees of freedom count the actual difference in identifiable dimensions, including any structural-zero or fitted-model restrictions.
Settings and design choices
Specify the null loglinear terms, sample space, tail calibration, and any correction. The log-likelihood member of the power-divergence family is a method choice, not an automatically superior setting. Match the model and constraints when comparing implementations.
Fitting and adaptation
Refit the null model whenever the table changes. In a parametric bootstrap, simulate from fitted null cell probabilities and repeat the complete null fit and likelihood comparison. Conditioning on margins is an alternative calibration choice; no neural fine-tuning is involved.
Sensitivity and robustness
Sparse expected cells can undermine the chi-square reference even when the logarithmic statistic is finite. An observed count in a truly zero-probability null cell is incompatible with that null. Inspect goodness-of-fit residuals and effect sizes rather than using G alone as an explanation.
How to use and calibrate
Compare with χ²df asymptotically, or simulate tables under the null. This is a likelihood ratio instance of entry 1.
Example application
Compare an independence loglinear model to the saturated table model.
Interpretation and pitfalls
Watch out
The likelihood ratio approximation also fails with sparse counts; G is not automatically better than Pearson in small tables.
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
For McNemar the table represents paired before/after categories; other entries use independent observations. The same numbers illustrate different sampling designs.
Validated — Executed successfully in the validation environment · needs numpy, scipy
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
tab = np.array([[18,12],[9,21]])
print(stats.chi2_contingency(tab,correction=False,lambda_="log-likelihood"))
Output
Chi2ContingencyResult(statistic=np.float64(5.544099501781618), pvalue=np.float64(0.018543095124077178), dof=1, expected_freq=array([[13.5, 16.5],
[13.5, 16.5]]))Not run — Syntax checked; runtime not verified in the final environment · needs DescTools
tab <- matrix(c(18,12,9,21),nrow=2,byrow=TRUE)
print(DescTools::GTest(tab,correct="none"))
Sources
- DescTools — DescTools · GTestAuthor documentation/source reviewed: DescTools 0.99.60.search.r-project.org
- GTestsearch.r-project.org
- Statistics guide v4 (historical):
- R: Syntax checked; runtime not verified in the final environment
- Reference's record:
- Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 059, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT059.