Counts against a theory
Use Pearson's chi-square and the Cressie–Read family for continuous data in bins, take off the right degrees of freedom, and know when simulated p-values help.
What runs in CodeSplash
- STAT115Pearson chi square goodness of fit test Runs in CodeSplash
- STAT124Cressie–Read power divergence tests Runs in CodeSplash
- Goodness of fit (χ² / G test) Verified in Python and R
You will be able to
- Count degrees of freedom when parameters come from the data
- Say when the choice of lambda matters
- Explain what binning continuous data costs
From curves to counts
ReadThe Proportions track showed goodness of fit for categories. You can also bin a continuous variable and compare counts with a model's expected counts.
It's easy to explain, and it works on anything. But it throws away the order inside each bin, and the answer depends on where you draw the bins. The ECDF tests from the last lesson usually have more power.
Bins are the natural choice when your data already come that way: age groups, counts per day or rounded readings.
Lost degrees of freedom
PredictPredict first
You bin a variable into 10 bins and compare it with a normal whose mean and SD you estimated from the same data.
How many degrees of freedom does the chi-square test have?
Make your guess between 0 and 10 df, then reveal.
Reveal
: one for the fixed total and two for the estimated parameters. Strictly, this needs estimates from the binned counts. Using the raw data's mean and SD gives a slightly different null.
A dial with many settings
ReadPearson's and the G test are two settings of one family, indexed by :
is Pearson, is G, and Cressie–ReadPower-divergence statisticsOne family of goodness-of-fit statistics indexed by λ — Pearson's χ² (λ = 1), G² (λ = 0), Freeman–Tukey (−½), Neyman (−2). recommended . Negative reacts most to cells where the observed count falls well below expected, and large to cells where it rises well above.
One setting of the dial
ExploreThis view fixes . Change the sample size and how far the counts depart from the stated shares, and watch how quickly the statistic grows.
BExplorable · Cressie–Read power divergence tests
Do category counts match the hypothesized shares, by the power-divergence family?
Your data
Edit the countsDashed lines: the counts H₀ expects (equal shares).
If H₀ were true
- The reference: how CR varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .015).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- family index: 1 gives Pearson's χ², 0 the G test, ⅔ Cressie and Read's compromise
The verdict
CR(3) = 10.51, p = .015
p ≤ α = .050: reject H₀
λ = ⅔ keeps the χ² reference accurate in smaller samples than either Pearson or G.
- Statistic
- 10.512
- p-value
- 0.0147
Cressie–Read power divergence tests on 41, 36, 24, 19 across 4 categories: CR(3) = 10.51, p = .015. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Does it matter here?
PredictPredict first
Mendel's 556 peas have well-filled cells; the smallest expected count is 34.8.
How do Pearson () and Freeman–Tukey () compare?
Reveal
They barely differ. Pearson: 0.470, p = .925. Freeman–Tukey: 0.478, p = .924. The dial matters in sparse tables, not here.
When cells get small
ReadFor a small table of 32 observations in 3 cells, the G test's chi-square p-value is 0.302, and its Monte Carlo version gives 0.330. They're close here, since the expected counts are about 10.7.
The simulated p-value is a cheap check when any expected count is small, and a safer reading when the two disagree.
Choose before you look. Scanning the dial for the smallest p-value is the same mistake as shopping among tests.
How big is the misfit?
ReflectEye color across 592 students, tested against equal shares, gives = 133.5, p < .001, and Cohen's w = 0.47.
Mendel's peas give w = 0.03.
With a million observations, a real but tiny departure gets a very small p-value. Which number would you report to say whether the theory is useful, and why?
Check your understanding
CheckpointCheck yourself
A Poisson model with its rate estimated from the same counts is compared with data in 7 bins. How many degrees of freedom remain?
Show the answer
Answer: 5
: one for the fixed total and one for the estimated rate.
Check yourself
Your table has all expected counts above 30. What does changing do?
Show the answer
Answer: a
The family's members share the same large-sample chi-square reference. Differences show up in sparse tables.
Check yourself
Why might you prefer an ECDF test to binning a continuous variable?
Show the answer
Answer: b
Binned tests are flexible but give up information. If you do bin, fix the bins before you look at the data.
Do it in CodeSplash
In the appSet Expected proportions to your theory, and choose a Statistic. Turn on Monte Carlo p-value if any expected count is small.
This method's degrees of freedom are always , because it takes the proportions as stated. If you estimated parameters from the same data, take them off by hand, or use the distribution fit method instead.
This script reproduces the Freeman–Tukey statistic for Mendel's counts, 0.4783.
import numpy as np
from scipy import stats
obs = np.array([315, 101, 108, 32])
exp = obs.sum() * np.array([9, 3, 3, 1]) / 16
print(stats.power_divergence(obs, exp, lambda_="freeman-tukey"))
print(stats.power_divergence(obs, exp, lambda_="pearson"))obs <- c(315, 101, 108, 32)
exp <- sum(obs) * c(9, 3, 3, 1) / 16
pd <- function(lambda) 2 / (lambda * (lambda + 1)) * sum(obs * ((obs / exp)^lambda - 1))
stat <- c(pd(-1 / 2), pd(1))
rbind(stat, p = pchisq(stat, length(obs) - 1, lower.tail = FALSE))Recap
Recap- Binning makes any variable testable, at the cost of order within bins and a choice of bins.
- Take one degree of freedom off for every parameter you estimate.
- The Cressie–Read dial matters in sparse tables. Choose first, and use a simulated p-value when expected counts are small.
- Report the size of the misfit along with the p-value.
Concepts in this lesson