Chi‑square goodness‑of‑fit test is a statistical tool used to determine whether observed categorical data follow a expected distribution. Researchers, quality‑control analysts, and educators often apply this test to answer questions such as “Is a die fair?” or “Do customer preferences match the proportions predicted by a marketing model?” In the following sections we walk through a concrete example, show every calculation step, and explain how to interpret the result so you can confidently apply the test to your own data.
Introduction
The chi‑square goodness‑of‑fit test evaluates the discrepancy between observed frequencies in categories and expected frequencies derived from a hypothesized distribution. Here's the thing — if the discrepancy is too large, we reject the null hypothesis that the data come from that distribution. This article provides a detailed, step‑by‑step illustration of the test using a classic scenario: testing whether a six‑sided die is fair. By the end, you will know how to set up the hypothesis, compute the test statistic, determine the p‑value, and draw a meaningful conclusion.
Understanding the Chi‑Square Goodness‑of‑Fit Test
Core Idea
- Null hypothesis (H₀): The observed counts follow the specified theoretical distribution.
- Alternative hypothesis (H₁): At least one category’s observed count deviates from what the distribution predicts.
The test statistic is
[ \chi^{2} = \sum_{i=1}^{k} \frac{(O_i - E_i)^2}{E_i} ]
where
- (O_i) = observed frequency in category i
- (E_i) = expected frequency in category i under H₀
- k = number of categories
Under H₀ and with sufficiently large expected counts (usually (E_i \ge 5)), (\chi^{2}) follows a chi‑square distribution with (df = k - 1) degrees of freedom Still holds up..
When to Use It
- Data are categorical (nominal or ordinal).
- You have a single sample and a pre‑specified distribution (e.g., uniform, binomial, Poisson).
- Expected frequencies are not too small; otherwise, combine categories or use an exact test.
Step‑by‑Step Example: Testing a Die for Fairness
Suppose you roll a six‑sided die 60 times and record the outcomes:
| Face (i) | Observed (O_i) |
|---|---|
| 1 | 8 |
| 2 | 12 |
| 3 | 9 |
| 4 | 11 |
| 5 | 10 |
| 6 | 10 |
| Total | 60 |
We want to know if the die is fair, i.e., each face has probability (p = 1/6) Most people skip this — try not to..
1. State the Hypotheses
- H₀: The die is fair; each face appears with probability (1/6).
- H₁: The die is not fair; at least one face has a different probability.
2. Compute Expected Frequencies
Under H₀, expected count for each face:
[ E_i = n \times p = 60 \times \frac{1}{6} = 10 ]
Thus, (E_i = 10) for all six faces That's the part that actually makes a difference..
3. Verify the Expected‑Count Condition
All (E_i = 10 \ge 5); the chi‑square approximation is appropriate.
4. Calculate the Chi‑Square Statistic
Create a table of ((O_i - E_i)^2 / E_i):
| Face | (O_i) | (E_i) | (O_i - E_i) | ((O_i - E_i)^2) | ((O_i - E_i)^2 / E_i) |
|---|---|---|---|---|---|
| 1 | 8 | 10 | -2 | 4 | 0.Day to day, 40 |
| 2 | 12 | 10 | 2 | 4 | 0. 40 |
| 3 | 9 | 10 | -1 | 1 | 0.10 |
| 4 | 11 | 10 | 1 | 1 | 0.10 |
| 5 | 10 | 10 | 0 | 0 | 0.00 |
| 6 | 10 | 10 | 0 | 0 | 0.00 |
| Sum | **1. |
[ \chi^{2}_{\text{calc}} = 1.00 ]
5. Determine Degrees of Freedom
[ df = k - 1 = 6 - 1 = 5 ]
6. Find the Critical Value or p‑Value
Using a chi‑square table or calculator for (df = 5):
- The critical value at (\alpha = 0.05) is 11.07.
- Our calculated (\chi^{2} = 1.00) is far below this threshold.
Alternatively, the p‑value is:
[ p = P(\chi^{2}_{5} \ge 1.00) \approx 0.96 ]
7. Make a Decision
Since (p > 0.05) (or (\chi^{2}{\text{calc}} < \chi^{2}{\text{critical}})), we fail to reject H₀. There is insufficient evidence to conclude that the die is unfair; the observed distribution is compatible with a fair die.
8. Interpret the Result in Context
Even though some faces appeared slightly more or less often than the expected 10 rolls, the deviations are small relative to sampling variability. In practical terms, if you were using this die in a game, you could treat it as fair without concern.
Scientific Explanation: Why the Chi‑Square Works
The chi‑square goodness‑of‑fit test stems from the multinomial distribution. When the sample size is large, the standardized differences ((O_i - E_i)/\sqrt{E_i}) approximate independent standard normal variables. Here's the thing — squaring and summing them yields a chi‑square variable. The test essentially measures how far the observed vector deviates from the expected vector, weighted by the expected counts (the variance of each count under the multinomial model).
Limitations and Practical Tips
| Issue | Why It Matters | Remedy |
|---|---|---|
| Small expected frequencies ((E_i < 5)) | Chi‑square approximation becomes inaccurate. Also, | Combine adjacent categories or use Fisher’s exact test / Monte‑Carlo simulation. |
| Only tests overall fit | Does not tell which categories cause misfit. |
You'll probably want to bookmark this section.
individual cells that contribute most to the discrepancy, guiding post‑hoc investigation.
Another practical consideration is the independence of observations. The chi‑square test assumes each trial or subject contributes to only one category and that outcomes are not influenced by previous trials. On the flip side, violations—such as repeated measures on the same units or clustered data—can inflate the test statistic and lead to spurious significance. In such cases, consider mixed‑effects models or generalized estimating equations instead It's one of those things that adds up..
When expected counts are adequate and observations are independent, the chi‑square goodness‑of‑fit test remains a versatile, widely used tool for comparing categorical data to a theoretical distribution Simple, but easy to overlook..
Conclusion
In this example, rolling a die 60 times produced observed frequencies close enough to the expected uniform distribution that the chi‑square statistic (1.00) fell well below the critical value (11.07) for 5 degrees of freedom. With a p‑value of approximately 0.96, we fail to reject the null hypothesis and conclude that the die is consistent with being fair.
Beyond the die example, the chi‑square goodness‑of‑fit test offers a straightforward way to assess whether observed categorical counts match expected proportions. Its validity, however, depends on adequate expected frequencies, independent observations, and an understanding that a non‑significant result does not prove the null hypothesis—it merely indicates insufficient evidence against it. By checking assumptions, inspecting standardized residuals, and interpreting results in context, researchers can use the test responsibly to draw meaningful conclusions about categorical data.
The Chi‑Square Test of Independence
While the goodness‑of‑fit test compares a single categorical variable to a theoretical distribution, the chi‑square test of independence asks whether two categorical variables are related. The logic is identical: we compare observed counts in a contingency table to the counts we would expect if the variables were truly independent The details matter here. But it adds up..
Setting Up the Contingency Table
Suppose we survey 200 voters on their preferred policy (Economy, Healthcare, Environment) and record their age group (Under 40, 40+). The observed frequencies form an (r \times c) table:
| Economy | Healthcare | Environment | Row Total | |
|---|---|---|---|---|
| Under 40 | 30 | 50 | 20 | 100 |
| 40+ | 50 | 30 | 20 | 100 |
| Col Total | 80 | 80 | 40 | 200 |
Expected Frequencies Under Independence
If policy preference is independent of age, the probability of preferring “Economy” is (80/200 = 0.40) for both age groups. The expected count for the cell (Under 40, Economy) is therefore
[
E_{11} = \frac{(\text{Row Total}_1) \times (\text{Col Total}_1)}{N} = \frac{100 \times 80}{200} = 40.
]
Repeating this for all six cells yields the expected table:
| Economy | Healthcare | Environment | |
|---|---|---|---|
| Under 40 | 40 | 40 | 20 |
| 40+ | 40 | 40 | 20 |
The Test Statistic and Degrees of Freedom
The chi‑square statistic is calculated exactly as before: [ \chi^2 = \sum \frac{(O_{ij} - E_{ij})^2}{E_{ij}}. ] For the table above: [ \chi^2 = \frac{(30-40)^2}{40} + \frac{(50-40)^2}{40} + \frac{(20-20)^2}{20} + \frac{(50-40)^2}{40} + \frac{(30-40)^2}{40} + \frac{(20-20)^2}{20} = 2.5 + 2.5 + 0 + 2.5 + 2.5 + 0 = 10.0. ] Degrees of freedom for an (r \times c) table are ((r-1)(c-1)). Here, ((2-1)(3-1) = 2) No workaround needed..
With (\chi^2 = 10.At (\alpha = 0.Worth adding: 0) and (df = 2), the p‑value is approximately 0. 0067. 05), we reject the null hypothesis of independence: policy preference appears to vary by age group.
Effect Size: Beyond the p‑Value
A significant chi‑square only tells us an association exists, not its strength. For (2 \times 2) tables, Phi ((\phi)) is used; for larger tables, Cramér’s V is the standard: [ V = \sqrt{\frac{\chi^2}{N \times \min(r-1, c-1)}}. ] In our example: (V = \sqrt{10.0 / (200 \times 1)} \approx 0.22). Using conventional benchmarks (0.1 = small, 0.3 = medium, 0.5 = large), this suggests a small-to-medium association—statistically detectable but not overwhelming in practical
significance. Still, a V of 0. Even so, 22 indicates that while age and policy preference are associated, age accounts for only a modest portion of the variation in preferences. Researchers should therefore avoid overstating the result; with a sufficiently large sample, even trivial associations can become statistically significant And it works..
Before trusting the p‑value, it is important to verify the test’s assumptions. The chi‑square approximation is reliable only when expected frequencies are adequate: no more than 20% of cells should have expected counts below 5, and all expected counts should be at least 1. If this condition fails—common in small samples or sparse contingency tables—consider Fisher’s exact test (for (2 \times 2) tables) or combine categories to increase cell counts.
When the null hypothesis is rejected, follow‑up analysis can reveal which cells drive the association. Standardized residuals, calculated as
[
\frac{O_{ij} - E_{ij}}{\sqrt{E_{ij}}},
]
highlight cells with substantially more or fewer observations than expected under independence; residuals with absolute values greater than roughly 2 or 3 merit closer inspection.
Simply put, the chi‑square test of independence offers a straightforward way to detect relationships between categorical variables. By pairing the hypothesis test with an effect‑size measure, checking assumptions, and examining standardized residuals when appropriate, analysts can distinguish meaningful patterns from random noise and draw more nuanced, defensible conclusions about their data.
People argue about this. Here's where I land on it.