EProportions · Guide test 058
Pearson chi square homogeneity test
Tests whether categorical outcome proportions are equal across independently sampled populations.
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?
- Two independent proportions (z, Fisher, Boschloo, Barnard, non-inferiority) — Does the share of an outcome differ between two independent groups?
In χ² test of independence, choose Question = Homogeneity (groups compared).
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 · Pearson chi square homogeneity test
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 χ² varies across samples when H₀ is true (χ² approximation).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .031).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- observed count
- expected count from the margins
The verdict
χ²(2) = 6.98, p = .031, V = 0.22
p ≤ α = .050: reject H₀
Effect size: Cramér's V V = 0.223
Same arithmetic as the test of independence; the difference is the design — here row totals were fixed by sampling each group.
- Statistic
- 6.977
- p-value
- 0.0305
Pearson chi square homogeneity test on a 2 × 3 table of 140 counts: χ²(2) = 6.98, p = .031, V = 0.22. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: multiple populations have the same categorical probability vector.
Assumptions
Independent multinomial samples with fixed group sizes; adequate expected cell counts.
Formula
Parameters and notation
O is an r×c table; N total count; df=(r−1)(c−1) for an unrestricted table.
Structure of the method
Stack categorical counts from separately sampled populations into rows. Estimate one common response-probability vector under homogeneity and compare each row with its fixed-size multinomial expectation. The Pearson statistic matches an independence table algebraically, while the sampling scheme differs.
Estimated and nuisance parameters
The null common category probabilities are pooled column totals divided by the overall sample size. Each population has an unrestricted category vector under the alternative. Row sample sizes are sampling-design constants; population means or continuous variances are not fitted here.
Settings and design choices
Fix the population definitions, common category dictionary, missing-response policy, and significance level. Decide whether sparse categories are scientifically combinable before outcome inspection. If simulating, preserve the separate population sample sizes and explicitly distinguish additional conditioning on pooled category totals.
Fitting and adaptation
For new population samples, re-estimate the pooled category vector and recompute expected counts. A multinomial bootstrap draws each row at its own fixed size from that common vector, then refits it. There is no transferable fitted network or fine-tuning stage.
Sensitivity and robustness
One large population can dominate the pooled estimates while small rows remain sparse. Zero expected probabilities and structural exclusions require a revised model. Nonresponse differing across populations can affect scientific interpretation even if the numerical test is valid for observed responses.
How to use and calibrate
Use the upper chi square tail or a sampling design appropriate exact/Monte Carlo test.
Example application
Compare category proportions across independently sampled populations.
Interpretation and pitfalls
Watch out
Structural zeros and fitted constraints change degrees of freedom. Expected, not observed, counts govern approximation quality.
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))
Output
Chi2ContingencyResult(statistic=np.float64(5.454545454545455), pvalue=np.float64(0.01951748118217779), dof=1, expected_freq=array([[13.5, 16.5],
[13.5, 16.5]]))Validated — Executed successfully in the validation environment · needs Base/standard library
tab <- matrix(c(18,12,9,21),nrow=2,byrow=TRUE)
print(chisq.test(tab,correct=FALSE))
Output
Pearson's Chi-squared test data: tab X-squared = 5.4545, df = 1, p-value = 0.01952
Sources
- R Pearson chi square testsstat.ethz.ch
- chisq.teststat.ethz.ch
- Statistics guide v4 (historical):
- R: Executed successfully in the validation 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 058, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT058.