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.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- 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 · Pearson χ² goodness-of-fit test
Your data
Edit the countsDashed lines: the counts H₀ expects (equal shares).
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 = .112).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- observed count in category i
- the count expected under H₀ (here equal shares)
- 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 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
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.
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))
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
- SciPy — SciPy · chisquaredocs.scipy.org
- scipy.stats.chisquaredocs.scipy.org
- 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.