Chi-Square Goodness-of-Fit Test Explained (With Example)

TL;DR: The chi-square goodness-of-fit test checks whether the counts you observed match the counts you expected from a claimed pattern. In our worked example — a die rolled 60 times — the chi-square value is 2.80 with 5 degrees of freedom, well below the 5% critical value of 11.07, so there is no evidence the die is unfair. The test needs one categorical variable, raw counts (not percentages), and expected counts that are mostly 5 or more.

Is this die fair? Do customers really choose between four flavours equally? Did the survey answers split 50/50 the way the plan assumed? These are all goodness-of-fit questions: you have a claim about how counts should be spread across categories, and you want to know whether the data you collected fits that claim. The chi-square goodness-of-fit test is the tool for the job. If you are new to the test itself, start with what the chi-square test is; this guide explains the goodness-of-fit version step by step, with a full worked example. You can check the result with our free Chi-Square Calculator.

What is a goodness-of-fit test?

A goodness-of-fit test compares the counts you observed with the counts you expected, and asks whether the gap between them is bigger than chance would explain. “Fit” is the key word: how well does the pattern you claimed fit the data you actually got?

The expected counts come from a claim you state before looking at the data. For a fair die, the claim is that each face is equally likely. For a survey with four answer options, the claim might be that the answers split equally. The claim can also be an uneven split: a shop might expect its four snack flavours to sell in the proportions 40%, 30%, 20% and 10%, and the test can check that claim just as easily.

If the observed counts are close to the expected ones, the test says the claim fits. If they are far off, the test says the claim does not fit — something about the claim is wrong. What it never says is which part of the claim is wrong, or why; that is for you to work out from the counts.

Goodness of fit vs test of independence

The chi-square test has two main forms, and it is worth keeping them straight. Goodness of fit uses one variable and compares it with proportions you state in advance. The test of independence uses two variables and checks whether they are related to each other, with the expected counts worked out from the table totals instead of a stated claim.

Goodness of fitTest of independence
VariablesOneTwo
QuestionDo the counts match a claimed split?Are the two variables related?
Expected counts fromThe claimed proportionsThe row and column totals
Degrees of freedomCategories − 1(Rows − 1) × (Columns − 1)

Both use the same statistic, χ² = Σ (O − E)² / E, and both are judged against a critical value for their degrees of freedom. The worked example below is goodness of fit; the test of independence guide covers the other form.

Worked example: is this die fair?

A die is rolled 60 times, and the results are:

Face123456Total
Observed rolls81291171360

The 6 came up 13 times and the 5 only 7 times. Is the die loaded, or is that just the wobble you expect from 60 rolls? The test will tell us.

Step 1: Work out the expected counts

If the die is fair, each face has a 1-in-6 chance on every roll. With 60 rolls, each face is expected 60 / 6 = 10 times. The expected count is the same for every face here, because the claim is an equal split. If the claim had been an uneven split — say 40% for one category — the expected count for that category would be 60 × 0.40 = 24.

Step 2: Apply the formula to each face

For each face, subtract the expected count from the observed count, square the difference, and divide by the expected count:

FaceObserved (O)Expected (E)(O − E)² / E
1810(8 − 10)² / 10 = 4 / 10 = 0.40
21210(12 − 10)² / 10 = 4 / 10 = 0.40
3910(9 − 10)² / 10 = 1 / 10 = 0.10
41110(11 − 10)² / 10 = 1 / 10 = 0.10
5710(7 − 10)² / 10 = 9 / 10 = 0.90
61310(13 − 10)² / 10 = 9 / 10 = 0.90

Adding the six terms: 0.40 + 0.40 + 0.10 + 0.10 + 0.90 + 0.90 = χ² = 2.80. Notice that the two biggest gaps — the 5 and the 6 — contribute the most, which is exactly how the test should behave: the categories furthest from expectation drive the result.

Step 3: Degrees of freedom

For a goodness-of-fit test, the degrees of freedom are the number of categories minus 1. There are 6 faces, so df = 6 − 1 = 5. One degree is lost because the counts must add up to the total of 60 — once five of the counts are known, the sixth is fixed.

Step 4: The verdict

At the usual 5% significance level, the critical value for 5 degrees of freedom is 11.07. Our value, 2.80, is far below it — so we do not reject the claim that the die is fair. The p-value is about 0.73, which means a gap this big (or bigger) would appear by chance about 73% of the time with a fair die. In plain words: 13 sixes and 7 fives in 60 rolls is entirely normal. There is no evidence this die is loaded.

That does not prove the die is fair — a loaded die could still produce these results by luck, and a bigger sample would be needed to catch a small bias. A non-significant result is always “not enough evidence”, never proof that the claim is true.

When to use a goodness-of-fit test

Use it whenever you have one categorical variable and a claim about how its counts should be spread. Common cases include checking whether a die, coin or random process is fair; testing whether survey answers split the way a plan assumed; checking whether observed counts match a known ratio, such as the 9:3:3:1 ratio in genetics; and quality control, where a factory expects defects to fall into categories in known proportions.

The conditions are the same as for the test generally: the data should come from a random sample, each observation should be counted once, and the expected counts should mostly be 5 or more. Here every expected count is 10, so the condition is met easily. If some expected counts are tiny, combine categories where that makes sense — for example, grouping rare answers under “other” — or collect more data.

One more rule: the test needs raw counts, not percentages. Two surveys can both show 20% for a category yet carry very different amounts of evidence if one surveyed 60 people and the other surveyed 6,000. Percentages hide the sample size, and the test’s answer depends on it.

Frequently Asked Questions

What does a goodness-of-fit test check?

It checks whether the counts you observed in categories match the counts you would expect from a claimed distribution — for example, whether a die lands on each face equally often, or whether survey answers split equally between options.

How is goodness of fit different from the test of independence?

Goodness of fit compares one variable against expected proportions you state in advance. The test of independence compares two variables against each other, to see whether they are related. Both use the same chi-square statistic.

How do you work out the expected counts?

Multiply the total number of observations by the expected proportion for each category. If all categories are equally likely, divide the total by the number of categories — for 60 dice rolls across 6 faces, each face is expected 10 times.

What are the degrees of freedom for a goodness-of-fit test?

The number of categories minus 1. A six-sided die has 6 categories, so the test has 5 degrees of freedom. One degree is lost because the counts must add up to the total.

What if an expected count is less than 5?

The test becomes unreliable when expected counts are very small. Combine small categories where that makes sense, or collect more data so the expected counts grow. The rule of thumb is that most expected counts should be 5 or more.

References

  1. Pearson, Karl (1900). "On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling." Philosophical Magazine, Series 5, 50(302), 157–175. Source
  2. Illowsky, Barbara & Dean, Susan (2023). Introductory Statistics 2e. OpenStax, Rice University. Chapter 11, "The Chi-Square Distribution". Source