Skip to content

ADescribe · Guide test 115

Pearson chi square goodness of fit test

Compares observed category counts with a specified probability model, adjusting degrees of freedom for fitted parameters when appropriate.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

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 χ² goodness-of-fit test

100
0.40
0.050

Your data

Edit the counts
ABCD
34271821

Dashed lines: the counts H₀ expects (equal shares).

If H₀ were true

χ² = 6.00
  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 = .112).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

χ2=∑i=1k(Oi−Npi)2Npi,ν=k−1\chi^2 = \sum_{i=1}^{k} \dfrac{(O_i - N p_i)^2}{N p_i},\qquad \nu = k - 1

With your numbers

χ2=6,ν=3,N=100\chi^2 = 6,\qquad \nu = 3,\qquad N = 100
OiO_i
observed count in category i
NpiN p_i
the count expected under H₀ (here equal shares)
kk
number of categories

The verdict

χ²(3) = 6.00, p = .112, w = 0.24

p > α = .050: compatible with H₀

Effect size: Cohen's w w = 0.245

Large contributions point at the categories that disagree most with your expectation.

Statistic
6.000
p-value
0.1116
Pearson χ² goodness-of-fit test on 34, 27, 18, 21 across 4 categories: χ²(3) = 6.00, p = .112, w = 0.24. The p-value is above α = .050, so the data are compatible with H0.

Pearson χ² goodness-of-fit test on 34, 27, 18, 21 across 4 categories: χ²(3) = 6.00, p = .112, w = 0.24. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: category probabilities equal a specified or fitted vector.

Assumptions

Independent multinomial counts; mutually exclusive exhaustive bins; adequate expected counts.

Formula

X2=∑j=1k(Oj−npj)2npjX^2=\sum_{j=1}^{k}\frac{(O_j-np_j)^2}{np_j}

Parameters and notation

pj are null probabilities. With no fitting df=k−1; estimating q regular parameters from counts usually gives k−1−q.

Structure of the method

Form a multinomial count vector over exhaustive categories, obtain specified or fitted expected counts and sum squared standardized discrepancies. The classical chi-square limit results from the constrained covariance of category counts, not from assuming independent category totals.

Estimated and nuisance parameters

Category probabilities are fixed under a simple null or estimated under a parametric composite null. The total n is observed; regular fitted probability parameters consume degrees of freedom when fitted by the appropriate procedure.

Settings and design choices

Specify categories, pooling rules, estimated-parameter adjustment, significance level and exact/simulation alternatives. Categories cannot be selected retrospectively to maximize significance while retaining the original reference distribution.

Fitting and adaptation

Fit a categorical null model only using the intended likelihood or justified estimating procedure. In simulation, generate multinomial counts and repeat any parameter estimation and declared pooling. For a new sample refit nuisance probabilities when the null is composite. Fine-tuning terminology is inappropriate: this is count-model fitting and null calibration.

Sensitivity and robustness

Small expected counts, structural zeros, overdispersion, clustering and unequal exposure can invalidate a simple multinomial reference. A large statistic does not identify the responsible category without examining signed residuals; multiple residual inspections require their own simultaneous interpretation.

How to use and calibrate

Use χ²df or exact/null simulation. Decide categories and fitting rules before testing.

Example application

Test whether a die’s six face probabilities are all 1/6.

Interpretation and pitfalls

Watch out

Tiny expected cells and adaptive binning can invalidate chi square calibration.

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

Five categories with fully specified null probabilities; fitted parameters would change degrees of freedom.

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)
print(stats.chisquare([18,22,17,23,20],f_exp=[20]*5))
Output
Power_divergenceResult(statistic=np.float64(1.3), pvalue=np.float64(0.8613755316556765))
R

Validated — Executed successfully in the validation environment · needs Base/standard library

obs<-c(18,22,17,23,20);print(chisq.test(obs,p=rep(.2,5)))
Output
	Chi-squared test for given probabilities

data:  obs
X-squared = 1.3, df = 4, p-value = 0.8614

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 115, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT115.