Chi-square goodness-of-fit test: formula and worked example
The chi-square goodness-of-fit test checks whether the counts in the categories of one variable match a set of proportions you specify in advance: equal shares, historical shares or shares from a model. It compares each observed count with its expected count and adds up the squared differences. A small p-value says the counts do not follow the stated proportions.
When to use it
- Checking whether defects are spread evenly across cavities, nozzles, spindles, fill heads or shifts.
- Checking whether this month's mix of defect types or return reasons matches last year's mix.
- Checking whether counts fit a distribution, such as the Poisson (see the Poisson goodness-of-fit test).
- For two categorical variables at once, use cross tabulation and chi-square.
How it works
Suppose a variable has k categories, the observed counts are O₁ … Oₖ with total N, and the proportions to test are p₁ … pₖ (they must add up to 1).
- H₀: the true proportions are p₁ … pₖ.
- H₁: at least one proportion differs.
The expected counts are Eᵢ = N × pᵢ. The test statistic is:
χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ, df = k − 1The p-value is the upper tail of the chi-square distribution with k − 1 degrees of freedom. If you estimate any parameters of the proportions from the same data (as with the Poisson mean), subtract one more degree of freedom for each.
Each term (Oᵢ − Eᵢ)²/Eᵢ is that category's contribution to χ². The category with the largest contribution is where the data depart most from the stated proportions. The signed version, (Oᵢ − Eᵢ)/√Eᵢ, shows the direction.
Assumptions: each unit falls in exactly one category, units are independent, and the expected counts are large enough, usually at least 5 in every category. With smaller expected counts, combine categories or use an exact multinomial test.
Example
An injection molding cell runs a 4-cavity tool for a clip. Over a week, the line inspector removes 80 parts with short shots (incomplete filling) and records the cavity number molded into each part:
| Cavity | 1 | 2 | 3 | 4 | Total |
|---|---|---|---|---|---|
| Short shots | 18 | 14 | 31 | 17 | 80 |
All four cavities make the same number of parts, so if the short shots had nothing to do with the cavity, each would get a quarter of them. H₀: p₁ = p₂ = p₃ = p₄ = 0.25.
- Expected count per cavity: 80 × 0.25 = 20.
- Contributions: (18 − 20)²/20 = 0.20; (14 − 20)²/20 = 1.80; (31 − 20)²/20 = 6.05; (17 − 20)²/20 = 0.45.
- χ² = 0.20 + 1.80 + 6.05 + 0.45 = 8.50, with 4 − 1 = 3 degrees of freedom.
- p-value = P(χ²₃ ≥ 8.50) = 0.037. The 5% critical value is 7.81.
| Cavity | Observed | Expected | Contribution |
|---|---|---|---|
| 1 | 18 | 20 | 0.20 |
| 2 | 14 | 20 | 1.80 |
| 3 | 31 | 20 | 6.05 |
| 4 | 17 | 20 | 0.45 |
How to read the results
- The p-value of 0.037 is below 0.05: short shots are not spread evenly across the cavities.
- The contributions show where. Cavity 3 alone contributes 6.05 of the 8.50, with 31 short shots against 20 expected. The other three cavities are close to an even share; a test on cavities 1, 2 and 4 alone gives χ² = 0.53 with p = 0.77.
- In tooling terms, a single cavity that fills poorly usually has a local cause: a partly blocked gate, a vent clogged with residue, a cooling circuit that runs cold, or a runner that is longer than the others. Inspect cavity 3's gate and vents first.
- The evidence is moderate. p = 0.037 is not overwhelming. Confirm with another week of data or a short study before pulling the tool, and remember that singling out the largest category after looking at the data makes its difference look more significant than it is.
Common mistakes
- Using percentages instead of counts. The test needs the raw counts; the same percentages from 80 parts or 8,000 parts carry very different evidence.
- Choosing the proportions after seeing the data. The stated proportions must come from the design (equal cavities), history or a model, not from the counts being tested.
- Assuming equal shares when the exposure differs. If one shift runs 10 hours and another 6, equal defect counts are not the right expectation. Make the proportions match the exposure.
- Ignoring small expected counts. A category with an expected count of 1 can produce a large χ² from a single unit. Combine sparse categories.
Frequently asked questions
What are the degrees of freedom in a chi-square goodness-of-fit test?
The number of categories minus 1, because the counts must add up to the total. If you estimate parameters from the data to set the proportions, subtract one more for each parameter.
Can I test against unequal proportions?
Yes. Enter any proportions that add up to 1, such as last year's mix of defect types: 50%, 30%, 20%. The expected counts are then N times each proportion.
What is the difference between goodness-of-fit and the test of independence?
Goodness-of-fit compares one variable with proportions you supply. The test of independence compares two variables with each other in a cross tabulation, with expected counts computed from the table's totals.
Is a chi-square test with two categories the same as a proportion test?
Yes. With two categories, the chi-square goodness-of-fit test is equivalent to the normal-approximation 1 proportion test: χ² equals z², and the p-values match. For small counts, use the exact binomial test instead.