G-Test of Independence Calculator
Inputs
- Inputs: the 2 x 2 table (42, 18 / 25, 35), 120 observations in all.
- Expected counts under independence come from the margins: 33.50, 26.50, 33.50, 26.50. The G statistic sums 2 x O x ln(O / E) over the four cells.
- G = 9.9114 on 1 degree of freedom, giving p = 0.0016; Pearson's chi-square on the same table is 9.7663 - the two statistics converge as samples grow.
- G-test result: 9.9114 - significant at the 5% level for the independence hypothesis.
The G-test is the likelihood-ratio twin of Pearson's chi-square test. Where chi-square sums squared standardised residuals, G sums observed counts times the log of observed over expected - the same comparison, derived from the likelihood principle instead of the quadratic approximation. Enter the four cells of a 2-by-2 table and the calculator returns both statistics and the p-value.
For large samples the two tests agree closely, as they should: both are approximations to the same underlying likelihood comparison. Sokal and Rohlf's Biometry made G the standard in biology; most other fields default to chi-square out of habit rather than principle.
Worked examples
Significant association, 120 cases
(42 · 18 · 25 · 35)
| G statistic | 9.9114 |
| Chi-square statistic | 9.7663 |
| p-value (1 df) | 0.0016 |
| Total observations | 120 |
| Verdict | Significant at the 5% level |
Compatible with independence
(15 · 25 · 20 · 20)
| G statistic | 1.2734 |
| Chi-square statistic | 1.2698 |
| p-value (1 df) | 0.2591 |
| Total observations | 80 |
| Verdict | Not significant at the 5% level |
Decisive on 240 cases
(88 · 32 · 54 · 66)
| G statistic | 20.2652 |
| Chi-square statistic | 19.9368 |
| p-value (1 df) | 6.742e-06 |
| Total observations | 240 |
| Verdict | Significant at the 5% level |
Likelihood ratios against Pearson residuals
The first worked example - cells 42, 18, 25, 35 - gives G = 9.9114 against a Pearson chi-square of 9.7663 on the same table, with a p-value of 0.0016 at 1 degree of freedom. The two statistics differ by about one and a half percent of their value, a typical gap at this sample size of 120.
G's construction is additive in a way chi-square is not: each cell contributes a term, and the terms of a large table partition exactly into the G values of its sub-tables plus an interaction term. That property makes G the natural tool for multi-way tables and log-linear models, where the analyst wants to attribute association to specific margins.
The second example shows the quiet case: G = 1.2734, p = 0.2591 - counts entirely compatible with independence, with chi-square at 1.2698 telling the same story.
- G = 2 x sum over cells of O ln(O / E), with E from the row and column margins.
- Degrees of freedom for a 2-by-2 table: 1.
- G partitions exactly across nested tables; chi-square only approximately.
Expected counts and degrees of freedom
Expected counts come from the margins under independence: each cell's expectation is its row total times its column total divided by the grand total. In the first example the margins imply expectations near 33.5 and 26.5 per cell, and the observed departures from them - 42 against 33.5 in the top-left cell is the largest - are what both statistics price.
The approximation behind the p-value needs those expectations to be reasonably large; the usual working rule wants all expected counts at least 5. Below that, both G and chi-square p-values drift, and Fisher's exact test or a Williams-corrected G is the safer instrument for a 2-by-2 table.
The third example, with 240 observations, has comfortable expected counts throughout and a decisive result: G = 20.2652, chi-square 19.9368, p = 6.742e-06. At this sample size the choice between the two statistics is purely a matter of taste.
| Example | G | Chi-square | p-value | Verdict |
|---|---|---|---|---|
| First | 9.9114 | 9.7663 | 0.0016 | Significant at the 5% level |
| Second | 1.2734 | 1.2698 | 0.2591 | Not significant at the 5% level |
| Third | 20.2652 | 19.9368 | 6.742e-06 | Significant at the 5% level |
Worked examples
Example 1: Cell a (row 1, col 1) 42; Cell b (row 1, col 2) 18; Cell c (row 2, col 1) 25; Cell d (row 2, col 2) 35. G statistic = 9.9114. Supporting values: Chi-square statistic 9.7663; p-value (1 df) 0.0016; Total observations 120; Verdict Significant at the 5% level. Inputs: the 2 x 2 table (42, 18 / 25, 35), 120 observations in all. Expected counts under independence come from the margins: 33.50, 26.50, 33.50, 26.50. The G statistic sums 2 x O x ln(O / E) over the four cells. G = 9.9114 on 1 degree of freedom, giving p = 0.0016; Pearson's chi-square on the same table is 9.7663 - the two statistics converge as samples grow. G-test result: 9.9114 - significant at the 5% level for the independence hypothesis.
Example 2: Cell a (row 1, col 1) 15; Cell b (row 1, col 2) 25; Cell c (row 2, col 1) 20; Cell d (row 2, col 2) 20. G statistic = 1.2734. Supporting values: Chi-square statistic 1.2698; p-value (1 df) 0.2591; Total observations 80; Verdict Not significant at the 5% level. Inputs: the 2 x 2 table (15, 25 / 20, 20), 80 observations in all. Expected counts under independence come from the margins: 17.50, 22.50, 17.50, 22.50. The G statistic sums 2 x O x ln(O / E) over the four cells. G = 1.2734 on 1 degree of freedom, giving p = 0.2591; Pearson's chi-square on the same table is 1.2698 - the two statistics converge as samples grow. G-test result: 1.2734 - not significant at the 5% level for the independence hypothesis.
Example 3: Cell a (row 1, col 1) 88; Cell b (row 1, col 2) 32; Cell c (row 2, col 1) 54; Cell d (row 2, col 2) 66. G statistic = 20.2652. Supporting values: Chi-square statistic 19.9368; p-value (1 df) 6.742e-06; Total observations 240; Verdict Significant at the 5% level. Inputs: the 2 x 2 table (88, 32 / 54, 66), 240 observations in all. Expected counts under independence come from the margins: 71.00, 49.00, 71.00, 49.00. The G statistic sums 2 x O x ln(O / E) over the four cells. G = 20.2652 on 1 degree of freedom, giving p = 6.742e-06; Pearson's chi-square on the same table is 19.9368 - the two statistics converge as samples grow. G-test result: 20.2652 - significant at the 5% level for the independence hypothesis.
When G and chi-square part company
Small samples separate them: G tends to run slightly liberal, chi-square slightly conservative, and the Williams correction - dividing G by a factor close to 1 that depends on the table size and n - brings G's behaviour close to exact. For a single 2-by-2 table with small counts, Fisher's exact test sidesteps the argument entirely.
Zero cells hurt G more: a zero observed count contributes 0 to G by the limit convention, which is mathematically tidy but can mask a structurally impossible cell that ought to change the model rather than the arithmetic. Chi-square's residual for such a cell at least shows up in the sum.
Partitioning G across sub-tables
The practical payoff of G appears in larger analyses: a three-way table's total G splits into components for each two-way association and the three-way interaction, each with its own degrees of freedom, and the components sum exactly. Analysts use this to test whether an association is uniform across strata or concentrated in one - the homogeneity question that a stack of separate chi-square tests answers only awkwardly.
That is also why G is the deviance of logistic and log-linear models: the same likelihood-ratio machinery, wearing a regression's clothes. Learning to read G on a 2-by-2 table is learning to read model comparisons everywhere else in categorical data analysis.
Entering your table
Enter raw counts, never percentages or rates - both statistics are built from counts and their margins, and a table of percentages has lost the sample size that significance depends on. The arrangement of rows and columns does not matter for the result: swapping them leaves G, chi-square and the p-value unchanged.
Check the total: the worked examples use totals of 120, 80 and 240, and the p-value's credibility rests on expected counts, which you can estimate from the margins before running the test. If any expected count will fall under 5, plan on an exact test instead.
The G statistic has a second identity worth knowing: divide G by twice the sample size and you have the mutual information between the two variables, in natural-log units, as estimated from the table. The test of independence and the information measure are the same computation read two ways - one as evidence against a hypothesis, the other as the strength of what the data say. It is the deep reason G, rather than chi-square, is the statistic that generalises to model selection criteria built on log-likelihoods.
Textbook practice adds one refinement for small tables: the Williams correction divides G by a factor slightly above 1 that shrinks as the sample grows, pulling its null distribution closer to the chi-square curve it is compared against. At the sample sizes of the worked examples the correction is a few percent at most; at the second example's size it is visible but changes no verdict. Knowing it exists is enough for most work - and knowing when a borderline p-value sits within correction distance of the line is a mark of care.
Software naming causes a recurring confusion worth pre-empting: several packages label this statistic the 'likelihood ratio chi-square', print it next to Pearson's, and leave the user to guess which is which. The giveaway is the formula - if the output's method note mentions log-likelihood, deviance or 'LR', it is G. In borderline cases the two can straddle a significance threshold in opposite directions, which is precisely when the label matters most; a methods section that names the statistic, as yours should, removes the ambiguity for every reader who follows.
FAQs
What G does the first example give?
The first example gives G = 9.9114, with a p-value of 0.0016 - strong evidence against independence in the 120-observation table.
What p-value does the first example give?
The p-value is 0.0016 in the first example, from the chi-square distribution at 1 degree of freedom applied to G = 9.9114.
What chi-square does the first example give?
Pearson's chi-square is 9.7663 on the same table - close to G, as it should be at this sample size. The two statistics are approximations of the same likelihood comparison.
What G does the second example give?
The second example gives G = 1.2734 with p = 0.2591: the 80 observations are entirely compatible with the two variables being independent.
What G does the third example give?
The third example gives G = 20.2652, the largest of the three, on 240 observations - a decisive rejection of independence.
What p-value does the second example give?
The second example's p-value is 0.2591, far above the 5% level. Nothing in that table requires an association to explain it.