Unit 7, lesson 53

Chi-Square Goodness-of-Fit Tests

How a chi-square goodness-of-fit test compares observed counts with the expected frequencies of a multinomial model, and how parameters estimated from the data reduce the degrees of freedom.

  • Video 53
  • Chi-Square and Exact Tests

A sorting device is advertised to send objects to four channels equally often, and a run of 80 objects gives counts of 28, 22, 18 and 12. Even a device that works perfectly rarely splits 80 objects evenly, so uneven counts alone do not settle anything. The real question is whether this much imbalance is unusual for a device that works as advertised. This lesson measures the gap between observed counts and the counts a probability model predicts, turns it into a p-value, and shows how the reference distribution changes when the model is fitted to the same data.

Key terms

Multinomial model

Definition A multinomial model describes nn observations, each falling into exactly one of k≥2k \ge 2 categories. The observations are independent, and each one falls into category ii with the same probability pip_i, where every pi≥0p_i \ge 0 and p1+⋯+pk=1p_1 + \dots + p_k = 1. The category counts O1,…,OkO_1, \dots, O_k then add to nn and have the multinomial distribution

P(O1=o1,…,Ok=ok)=n!o1!⋯ok! p1o1⋯pkok\begin{aligned} &P(O_1 = o_1, \dots, O_k = o_k) \\[0.4em] &\quad = \frac{n!}{o_1! \cdots o_k!}\, p_1^{o_1} \cdots p_k^{o_k} \end{aligned}

for whole numbers oi≥0o_i \ge 0 with o1+⋯+ok=no_1 + \dots + o_k = n. Each count on its own is binomial with nn trials and success probability pip_i, so E(Oi)=npiE(O_i) = np_i.

In plain words The model is a die with kk faces of possibly unequal weights, rolled nn times. The categories must not overlap and must cover every possible outcome, or the probabilities could not add to 1. Observations that arrive in correlated batches, or probabilities that drift during the collection period, break the model, and a test built on it can then mislead.

Example If the 80 objects are routed independently and each one goes to any given channel with probability 1/41/4, the four channel counts follow a multinomial model with n=80n = 80 and p1=p2=p3=p4=1/4p_1 = p_2 = p_3 = p_4 = 1/4. Under this model the chance of exactly 20 objects in every channel is only about 0.0014, so unequal counts are the usual result even for a perfect device.

Goodness-of-fit test

Definition For counts from a multinomial model with kk categories, a goodness-of-fit test of a specified model tests

H0 ⁣:pi=pi0 for every iH_0\colon p_i = p_{i0} \text{ for every } i

against the alternative H1H_1 that pi≠pi0p_i \ne p_{i0} for at least one ii. The specified probabilities p10,…,pk0p_{10}, \dots, p_{k0} are positive, add to 1 and are fixed before the data are seen. The test measures how far the observed counts are from the counts the model predicts. A small p-value is evidence against the specified model. A large p-value means the data are compatible with the model, not that the model is true.

In plain words There is one categorical variable and one proposed distribution, and the question is whether the counts sit farther from the proposal than chance usually puts them. A test of independence asks a different question, whether two categorical variables measured on the same units are related, and it builds its expected counts from the margins of a two-way table rather than from probabilities given in advance.

Example For the sorting device, the null hypothesis H0H_0 says each channel has probability 1/41/4, and H1H_1 says at least one channel probability differs from 1/41/4. The data are the counts 28, 22, 18 and 12 from 80 objects. If the test does not reject H0H_0, it has not shown that the four probabilities are exactly equal.

Expected frequency

Definition Under a specified model the expected frequency of category ii is

Ei=n pi0,E_i = n\, p_{i0} ,

the mean of the count OiO_i when H0H_0 holds. The expected frequencies add to nn, and they need not be whole numbers.

In plain words An expected frequency is the average count the model predicts over many samples of the same size, not a count that any single sample is supposed to hit. Comparing each count with its expected frequency, rather than the counts with each other, is what brings the model into the test.

Example For the device, every channel has Ei=80⋅1/4=20E_i = 80 \cdot 1/4 = 20. For three categories with probabilities 1/41/4, 1/21/2 and 1/41/4 and a sample of 60, the expected frequencies are 15, 30 and 15. With a sample of 50 and a probability of 1/31/3, the expected frequency is 50/3≈16.750/3 \approx 16.7, and it is used without rounding.

Chi-square statistic

Definition Pearson’s chi-square statistic for observed counts OiO_i and expected frequencies EiE_i is

X2=∑i=1k(Oi−Ei)2Ei.X^2 = \sum_{i=1}^{k} \frac{(O_i - E_i)^2}{E_i} .

It equals 00 exactly when every count equals its expected frequency, and larger values mean a larger discrepancy. If H0H_0 holds, the specified probabilities are all positive and the expected frequencies are large, then X2X^2 has approximately the chi-square distribution with k−1k - 1 degrees of freedom. The p-value is P(χk−12≥x)P(\chi^2_{k-1} \ge x) at the observed value xx, the approximate probability, computed assuming H0H_0, of a statistic at least as large as the one observed. The number k−1k - 1 reflects the constraint that the counts add to nn, so once k−1k - 1 of them are known the last one is determined. The approximation can be poor when expected frequencies are small. A common guideline asks for all of them to be at least 5, but that is a rule of thumb, not a theorem. For sparse counts the exact multinomial distribution of X2X^2 can be used instead, or a simulation that draws many samples from the specified model and recomputes the same statistic. Merging categories changes the hypothesis being tested, so a merge should follow from the question, be decided before looking at the counts and be reported.

In plain words Squaring makes excesses and shortages count alike, and dividing by EiE_i measures each gap against the size of its expectation, so a gap of 8 matters more when 20 are expected than when 200 are. For fixed proportions the statistic grows in proportion to the sample size, so a small p-value is not the same thing as a large or important discrepancy. Report the observed proportions next to the test.

Example For the counts 28, 22, 18 and 12, the differences from 20 are 88, 22, −2-2 and −8-8, and

X2=6420+420+420+6420=6.8.\begin{aligned} X^2 &= \frac{64}{20} + \frac{4}{20} + \frac{4}{20} + \frac{64}{20} \\[0.4em] &= 6.8 . \end{aligned}

The first and last channels give most of it. With 3 degrees of freedom the p-value is P(χ32≥6.8)≈0.079P(\chi^2_3 \ge 6.8) \approx 0.079. The exact multinomial probability of a statistic at least this large is about 0.078, close to the approximation because every expected frequency is 20. At a significance level of 0.05 chosen in advance, the test does not reject the model. Doubling every count to 56, 44, 36 and 24 keeps the proportions 0.35, 0.275, 0.225 and 0.15 but doubles the statistic to 13.6, with a p-value of about 0.0035.

Degrees of freedom for estimated parameters

Definition Sometimes H0H_0 says only that the probabilities belong to a family pi(θ)p_i(\theta) with rr unknown parameters, such as a binomial or Poisson family. The parameters are then estimated from the same data, and the expected frequencies become Ei=n pi(θ^)E_i = n\, p_i(\hat{\theta}). If θ^\hat{\theta} is the maximum likelihood estimate computed from the category counts and regularity conditions hold, then under H0H_0 the statistic X2X^2 has approximately the chi-square distribution with

ν=k−1−r\nu = k - 1 - r

degrees of freedom, which must be at least 1. The conditions include identifiable parameters, meaning that different parameter values give different category probabilities, a true parameter value inside the parameter range rather than on its boundary, positive category probabilities, and categories fixed before the data are seen. If the parameters are estimated from the raw, ungrouped observations instead, the large-sample distribution of X2X^2 lies between the chi-square distributions with k−1−rk - 1 - r and k−1k - 1 degrees of freedom, and in general neither one is exact.

In plain words Fitting the parameters moves the expected frequencies toward the observed counts, so X2X^2 tends to be smaller than it would be against a model fixed in advance. Each estimated parameter uses up one degree of freedom. Keeping k−1k - 1 would make the p-value too large and the test too reluctant to reject a poor model. The rule counts the parameters actually estimated from these counts, and it relies on a valid fitting method, not on counting the symbols in a formula.

Example Each of 80 observations records how many of two independent trials succeed, where every trial has the same unknown success probability, so the categories are 0, 1 and 2 successes. The counts are 25, 30 and 25. There are 30+2⋅25=8030 + 2 \cdot 25 = 80 successes in 160 trials, so the estimate is p^=0.5\hat{p} = 0.5. The fitted binomial probabilities 1/41/4, 1/21/2 and 1/41/4 give expected frequencies 20, 40 and 20, and

X2=2520+10040+2520=5.\begin{aligned} X^2 &= \frac{25}{20} + \frac{100}{40} + \frac{25}{20} \\[0.4em] &= 5 . \end{aligned}

With k=3k = 3 categories and r=1r = 1 estimated parameter, ν=1\nu = 1 and the p-value is P(χ12≥5)≈0.025P(\chi^2_1 \ge 5) \approx 0.025. Using k−1=2k - 1 = 2 degrees of freedom by mistake would give about 0.082.

Check yourself

  1. A process is supposed to assign items to three categories with probabilities 1/41/4, 1/21/2 and 1/41/4. In 60 independent assignments the counts are 18, 27 and 15. Find the expected frequencies, the chi-square statistic, the degrees of freedom and the approximate p-value.
Show the answer

The expected frequencies are 60⋅1/4=1560 \cdot 1/4 = 15, 60⋅1/2=3060 \cdot 1/2 = 30 and 15. The differences are 3, −3-3 and 0, so

X2=915+930+0=0.9.\begin{aligned} X^2 &= \frac{9}{15} + \frac{9}{30} + 0 \\[0.4em] &= 0.9 . \end{aligned}

No probability was estimated from the data, so there are 3−1=23 - 1 = 2 degrees of freedom. For 2 degrees of freedom the upper tail beyond xx is exactly e−x/2e^{-x/2}, so the p-value is e−0.45≈0.64e^{-0.45} \approx 0.64. The sample gives little evidence against the specified probabilities, which is not the same as showing that they are correct.

  1. A spinner is advertised to land on red, blue and green with probabilities 0.5, 0.3 and 0.2. In 40 spins it lands on red 26 times, on blue 6 times and on green 8 times. A classmate converts the counts to the percentages 65, 15 and 20, compares them with 50, 30 and 20, and gets a statistic of 12. Find the correct statistic and p-value, and explain the mistake.
Show the answer

The expected frequencies are 40⋅0.5=2040 \cdot 0.5 = 20, 40⋅0.3=1240 \cdot 0.3 = 12 and 40⋅0.2=840 \cdot 0.2 = 8, so

X2=3620+3612+0=4.8,\begin{aligned} X^2 &= \frac{36}{20} + \frac{36}{12} + 0 \\[0.4em] &= 4.8 , \end{aligned}

with 2 degrees of freedom and a p-value of e−2.4≈0.091e^{-2.4} \approx 0.091. At the 0.05 level the data are compatible with the advertised probabilities. All three expected frequencies are at least 8, so the chi-square approximation is reasonable here.

The statistic needs counts. Percentages treat the data as if there had been 100 spins, and since the statistic grows in proportion to the sample size for fixed proportions, the classmate’s value is 100/40=2.5100/40 = 2.5 times too large. Its p-value of about 0.0025 would wrongly suggest strong evidence against the spinner.

  1. Fifty pairs of seeds are planted, and for each pair the number of seeds that germinate is recorded. The numbers of pairs with 0, 1 and 2 germinated seeds are 12, 16 and 22. Test whether a binomial model with two trials and a common germination probability fits, with the probability estimated from these counts. How many degrees of freedom does the test have, and what pattern do the counts show?
Show the answer

There are 16+2⋅22=6016 + 2 \cdot 22 = 60 germinated seeds out of 100, so the estimate is p^=0.6\hat{p} = 0.6. The fitted probabilities are 0.42=0.160.4^2 = 0.16, 2(0.6)(0.4)=0.482(0.6)(0.4) = 0.48 and 0.62=0.360.6^2 = 0.36, so the expected frequencies are 8, 24 and 18, and

X2=168+6424+1618≈5.56.\begin{aligned} X^2 &= \frac{16}{8} + \frac{64}{24} + \frac{16}{18} \\[0.4em] &\approx 5.56 . \end{aligned}

One parameter was estimated by maximum likelihood from the category counts, so ν=3−1−1=1\nu = 3 - 1 - 1 = 1, and the p-value is P(χ12≥5.56)≈0.018P(\chi^2_1 \ge 5.56) \approx 0.018. Every expected frequency is at least 8, so the approximation is reasonable, and at the 0.05 level the test rejects the binomial model. Using 2 degrees of freedom by mistake would give about 0.062, and the test would not reject at that level.

More pairs than expected have no germinated seed or two germinated seeds, and fewer have exactly one, the category with the largest contribution, 64/24≈2.6764/24 \approx 2.67. That pattern fits seeds in the same pair sharing growing conditions, or a germination probability that varies from pair to pair. Either would break the assumption of independent trials with a common probability, and the test alone cannot say which one is happening.

How this lesson connects