Skip to content

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.

Advanced ~11 min10 steps

What runs in CodeSplash

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

Read first

01

From curves to counts

Read

The 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.

02

Lost degrees of freedom

Predict

Predict 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

10−1−2=710 - 1 - 2 = 7: 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.

03

A dial with many settings

Read

Pearson's X2X^2 and the G test are two settings of one family, indexed by λ\lambda:

2nIλ=2λ(λ+1)∑jOj[(OjEj)λ−1]2nI^\lambda = \frac{2}{\lambda(\lambda+1)} \sum_j O_j\left[\left(\frac{O_j}{E_j}\right)^{\lambda} - 1\right]

λ=1\lambda = 1 is Pearson, λ→0\lambda \to 0 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 λ=2/3\lambda = 2/3. Negative λ\lambda reacts most to cells where the observed count falls well below expected, and large λ\lambda to cells where it rises well above.

04

One setting of the dial

Explore

This view fixes λ=2/3\lambda = 2/3. 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?

120
0.40
0.050

Your data

Edit the counts
ABCD
41362419

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

If H₀ were true

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

The formula

CR=2λ(λ+1)∑jOj[(OjEj)λ−1],λ=23CR = \dfrac{2}{\lambda(\lambda+1)}\sum_j O_j\left[\left(\dfrac{O_j}{E_j}\right)^{\lambda}-1\right],\quad \lambda = \tfrac23

With your numbers

CR=10.51,ν=3CR = 10.51,\quad \nu = 3
λ\lambda
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 H0.

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₀.

05

Does it matter here?

Predict

Predict first

Mendel's 556 peas have well-filled cells; the smallest expected count is 34.8.

How do Pearson (λ=1\lambda = 1) and Freeman–Tukey (λ=−12\lambda = -\frac{1}{2}) compare?

They barely differThey differ a lot
Reveal

They barely differ. Pearson: 0.470, p = .925. Freeman–Tukey: 0.478, p = .924. The dial matters in sparse tables, not here.

06

When cells get small

Read

For 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 λ\lambda before you look. Scanning the dial for the smallest p-value is the same mistake as shopping among tests.

07

How big is the misfit?

Reflect

Eye color across 592 students, tested against equal shares, gives X2X^2 = 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?

08

Check your understanding

Checkpoint

Check 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

7−1−1=57 - 1 - 1 = 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 λ\lambda 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.

09

Do it in CodeSplash

In the app

Set 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 k−1k - 1, 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.

Python
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"))
R
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))
10

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 λ\lambda 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