Chi-square tests for homogeneity, independence, and goodness of fit
When data come in categories, we can compare the counts we observed with the counts we’d expect under a model. Let’s look at how that comparison works and how the question we’re asking determines which chi-square test to use.
Choose the chi-square test
Use a test for homogeneity to compare a categorical distribution across separately sampled groups. Use a test for independence when you sample one group and record two categorical variables for each observation. Use a goodness-of-fit test when you have one categorical variable and want to compare its counts with specified probabilities.
Use counts in mutually exclusive categories, with one count per observation. For inference, use random sampling or an appropriate randomized design and independent observations. When sampling without replacement, the guideline helps justify treating observations as independent if the sample is at most of its population. For these examples, check that each expected count is at least so the chi-square approximation is appropriate.
Calculate expected counts, the statistic, and degrees of freedom
For homogeneity and independence, multiply the cell’s row and column totals and divide by the grand total. For goodness of fit, multiply the total count by the category’s probability under the null hypothesis. Let and be the observed and expected counts for a cell or category.
The second line is for an -by- table. Count only the rows and columns of observations. For goodness of fit with categories and probabilities specified in advance, use . The -value is the area to the right of the observed statistic under the chi-square curve for those degrees of freedom.
Homogeneity example with pet preferences
Two groups of students are surveyed about whether they prefer cats, dogs, or another pet. The null hypothesis is that the distribution of preferences is the same for both groups. For the test, treat the two samples as random and independent.
| Group and preference | Observed | Expected |
|---|---|---|
| Male · Cats | 19 | 25.00 |
| Male · Dogs | 34 | 34.44 |
| Male · Other | 22 | 15.56 |
| Female · Cats | 26 | 20.00 |
| Female · Dogs | 28 | 27.56 |
| Female · Other | 6 | 12.44 |
There are students in the male group and in the female group. The total for cats is out of students. So the first expected count is , and its contribution to the statistic is . The smallest expected count is about .
Use the unrounded expected counts to add all six contributions. For this -by- table, the degrees of freedom are .
The -value is below , so we reject the null hypothesis at that level. It’s also just below . The samples give evidence that pet preference distributions differ between the groups.
Independence example with eye color and handedness
In a random sample of of a company’s employees, record eye color and handedness for each person. The null hypothesis is that the variables are independent. The sample is less than of the population, and the smallest expected count is .
| Hand and eye color | Observed | Expected |
|---|---|---|
| Left · Brown | 72 | 74.48 |
| Left · Blue | 36 | 35.14 |
| Left · Green | 20 | 21.00 |
| Left · Hazel | 12 | 9.38 |
| Right · Brown | 460 | 457.52 |
| Right · Blue | 215 | 215.86 |
| Right · Green | 130 | 129.00 |
| Right · Hazel | 55 | 57.62 |
The first expected count is . Add the eight contributions, and use degrees of freedom.
At we fail to reject independence. The sample gives insufficient evidence of an association.
Goodness-of-fit example with monthly cupcake sales
Suppose the model assigns an equal share of the year’s cupcake sales to each of the months. There are sales, so the model gives expected sales per month. The chi-square inference here assumes sales can be treated as independent observations under that model.
| Month | Observed | Expected |
|---|---|---|
| Jan | 60 | 85 |
| Feb | 80 | 85 |
| Mar | 65 | 85 |
| Apr | 70 | 85 |
| May | 80 | 85 |
| Jun | 100 | 85 |
| Jul | 140 | 85 |
| Aug | 120 | 85 |
| Sep | 90 | 85 |
| Oct | 90 | 85 |
| Nov | 60 | 85 |
| Dec | 65 | 85 |
Use all months. For example, the September contribution is , and October gives the same contribution. The fixed probabilities give degrees of freedom.
The right-tail critical value at is about . Because , we reject the equal-month model under the stated assumptions.
Goodness-of-fit example with a dog-walking schedule
Marla’s model gives probabilities of , , , , and for seeing one through five walkers working at a sampled time. She randomly samples times. For this calculation, assume the sampling design lets us treat the observations at those times as independent.
| Walkers | Observed | Expected |
|---|---|---|
| 1 | 15 | 15 |
| 2 | 16 | 25 |
| 3 | 37 | 30 |
| 4 | 24 | 20 |
| 5 | 8 | 10 |
Every expected count is at least . For two walkers the contribution is . Using all five categories gives
At we fail to reject Marla’s model. These observations give insufficient evidence that the category probabilities differ from her model.