Skip to content

EProportions · Guide test 058

Pearson chi square homogeneity test

Tests whether categorical outcome proportions are equal across independently sampled populations.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with one of these methods:

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

140
0.35

0 = rows and columns independent.

0.050

Your data

Edit the counts
LowMidHighTotal
Row A
25E = 18.5
18E = 23.3
25E = 26.2
68
Row B
13E = 19.5
30E = 24.7
29E = 27.8
72
Total384854140

E is the count expected if rows and columns were unrelated.

If H₀ were true

χ² = 6.98
  1. The reference: how χ² varies across samples when H₀ is true (χ² approximation).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .031).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

χ2=∑ij(Oij−Eij)2Eij,ν=(r−1)(c−1)\chi^2 = \sum_{ij}\dfrac{(O_{ij}-E_{ij})^2}{E_{ij}},\qquad \nu = (r-1)(c-1)

With your numbers

χ2=6.98,ν=2\chi^2 = 6.98,\quad \nu = 2
OijO_{ij}
observed count
EijE_{ij}
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 H0.

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

X2=∑ij(Oij−Eij)2EijEij=Oi+O+jN\begin{gathered}X^2=\sum_{ij}\frac{(O_{ij}-E_{ij})^2}{E_{ij}}\\[5pt] E_{ij}=\frac{O_{i+}O_{+j}}N\end{gathered}

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.

Python · native

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]]))
R

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

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.