How to Run a Chi-Square Goodness-of-Fit Test

Enter observed counts, choose how expected counts are specified, compute the Pearson chi-square statistic and p-value, and interpret the result correctly.

  1. 1. Choose how the expected distribution is specified: Three modes are supported. Equal proportions uses E = N / k for every category — the uniform null hypothesis. Expected proportions lets you enter a percentage per category, which the calculator multiplies by N to get expected counts. Expected counts lets you enter the expected frequency directly per category. Pick whichever matches how you formulated the null hypothesis.
  2. 2. Enter the observed and expected data: For each category, enter a name, the observed count, and — depending on the mode — either an expected percentage or an expected count. Add or remove categories with the buttons under the list. Up to 20 categories are supported. Click Load fair-die example to populate the classic six-category test.
  3. 3. Set α and the parameter-estimation option: Choose the significance level α from the presets (0.10, 0.05, 0.01) or enter a custom value. If any parameter of the hypothesized distribution was estimated from the same data — for example, using the sample mean to fit a Poisson model — check the box to reduce df by 1. The checkbox makes the df reduction explicit.
  4. 4. Read the chi-square statistic, df, p-value, and decision: The results panel shows the chi-square statistic, the degrees of freedom, the upper-tail p-value, the critical value at your chosen α, and the decision under both the p-value rule and the critical-value rule. The two rules always give the same answer. The step-by-step breakdown shows exactly how the statistic was built from the contributions.
  5. 5. Review the contribution table and charts: The contribution table shows the (O − E)² / E term for every category, and the two charts visualize observed vs expected counts and the contributions side by side. When the omnibus test is significant, the largest contributions point to which categories are driving the result — a useful starting point for interpreting the test.
  6. 6. Check the assumption warnings: The calculator flags two situations. If any expected count is below 5, the chi-square approximation may be unreliable — consider combining sparse categories or using an exact method. If only two categories are present, an exact binomial test is often preferable to the asymptotic chi-square. Both warnings are visible on the results panel.

What the goodness-of-fit test measures

The chi-square goodness of fit test compares observed category frequencies with the frequencies expected under a hypothesized distribution. It answers one question: do the observed counts match the expected pattern? You have one sample, one categorical variable with k categories, and a distribution you want to test against. The output is a chi-square statistic, the degrees of freedom for the design, and a p-value that tells you whether the discrepancy between observed and expected is larger than chance alone would explain under the null hypothesis.

Common uses include testing whether a die is fair, whether observed genetic ratios match Mendelian predictions, whether weekly customer traffic is evenly distributed across the days of the week, and whether a categorical survey response follows a known population breakdown. In every case the structure is the same: observed counts, expected counts, chi-square statistic, p-value.

Formula and degrees of freedom

The Pearson chi-square goodness of fit formula is χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ, where the sum runs over every category, Oᵢ is the observed count in category i, and Eᵢ is the expected count under the null hypothesis. Each term (Oᵢ − Eᵢ)² / Eᵢ is a contribution; the statistic is their sum. Larger discrepancies produce larger contributions, scaled by how many observations were expected in that category.

Degrees of freedom for a standard goodness-of-fit test with k categories is df = k − 1. If any parameter of the hypothesized distribution was estimated from the same data — for example, using the sample mean to fit a Poisson model — subtract one degree of freedom per estimated parameter. A chi-square value interpreted with the wrong df produces a meaningless p-value, which is why the df adjustment matters.

The p-value is the upper-tail probability P(X² ≥ χ²) under the chi-square distribution with the given df. The critical value χ²crit is the threshold at which that probability equals α. Reject the null if p < α, or equivalently if χ² > χ²crit. Both rules always give the same answer.

Worked example: is a die fair?

Roll a six-sided die 100 times and record each face. The null hypothesis is that all six faces are equally likely, so the expected count for each is 100 / 6 ≈ 16.67. Suppose the observed counts are 18, 14, 12, 20, 16, 20. Each contribution is (O − E)² / E:

  • (18 − 16.67)² / 16.67 ≈ 0.106
  • (14 − 16.67)² / 16.67 ≈ 0.427
  • (12 − 16.67)² / 16.67 ≈ 1.306
  • (20 − 16.67)² / 16.67 ≈ 0.667
  • (16 − 16.67)² / 16.67 ≈ 0.027
  • (20 − 16.67)² / 16.67 ≈ 0.667

Summing the contributions gives χ² ≈ 3.20. With k = 6 categories and no estimated parameters, df = 5. The upper-tail p-value is P(X² ≥ 3.20) with df = 5, which is about 0.67. At α = 0.05 the data are consistent with a fair die — the observed counts are not far enough from the expected to reject the null.

Worked example: Mendelian ratio

Suppose you expect a 9:3:3:1 ratio of four phenotypes in 160 offspring. The expected counts are 90, 30, 30, 10. You observe 86, 32, 29, 13. Each contribution is (O − E)² / E, and the sum gives χ². With k = 4 categories and no estimated parameters, df = 3. Compare the p-value to your chosen α for the decision. The structure is identical to the die example — only the expected counts and df change.

Assumptions and diagnostics

Three assumptions matter for the chi-square approximation to be reliable:

  • Independent observations. Each observation falls into exactly one category and does not influence any other. Repeated measures, matched pairs, and clustered sampling break this and call for a different method.
  • Mutually exclusive and exhaustive categories. Every observation belongs to one and only one category, and the categories together cover every possible outcome.
  • Sufficiently large expected counts. A common rule of thumb is at least 5 in every expected cell.

The calculator flags two situations. If any expected count is below 5, the asymptotic approximation may be unreliable — consider combining sparse categories (if scientifically defensible) or using an exact method. If only two categories are present, the test is mathematically related to the exact binomial, which is often preferable for small samples.

Goodness of fit vs test of independence

These are the two most common uses of the chi-square statistic and they are easy to confuse.

  • Goodness of fit — one sample, one categorical variable, one hypothesized distribution. df = k − 1 (or fewer if parameters were estimated). This is what this calculator handles.
  • Test of independence — one sample, two categorical variables arranged in an r × c contingency table. df = (r − 1)(c − 1). Use a dedicated independence calculator for that workflow.

Both use the Pearson χ² form. What changes is the design and the degrees of freedom. Mixing them up produces a statistic with the wrong df and a meaningless p-value.

Tips and limits

  • Convert percentages to counts. The chi-square statistic is not scale-invariant. If you have percentages, multiply by N first, or use the calculator's proportion mode.
  • Do not forget the df reduction when parameters were estimated. Fitting a distribution to the data costs one df per estimated parameter.
  • Do not read p > α as proof of the null. Failing to reject H₀ means the data did not provide sufficient evidence at the chosen α. The sample may simply be too small.
  • The contribution table is a diagnostic, not a formal test. The omnibus test tells you whether the overall distribution matches; it does not identify which categories differ. Formal follow-up comparisons require a separate analysis that controls the familywise error rate.
  • This calculator handles the one-sample goodness-of-fit case only. It does not run tests of independence, homogeneity, Fisher's exact test, McNemar's test, or any of the small-sample alternatives. It does not compute power or required sample size for goodness-of-fit tests.

Frequently Asked Questions

Q: What is the chi-square goodness of fit formula?

χ² = Σ (O − E)² / E, where O is the observed count in a category and E is the expected count under the null hypothesis. Each term is a contribution; the statistic is the sum. Degrees of freedom are k − 1 for k categories, reduced by one for each parameter estimated from the same data.

Q: When should I use goodness of fit instead of a test of independence?

Use goodness of fit when you have one sample, one categorical variable, and a hypothesized distribution to test against. Use a test of independence when you have one sample, two categorical variables arranged in a contingency table, and you want to know whether the two variables are associated. Both use the Pearson chi-square form, but the design and df differ — k − 1 versus (r − 1)(c − 1).

Q: How do I set the expected counts?

Three modes are supported. Equal proportions uses E = N / k for every category — the uniform null hypothesis. Expected proportions lets you enter a percentage per category, which the calculator multiplies by N. Expected counts lets you enter the expected frequency directly. Pick the one that matches how the null hypothesis is formulated.

Q: Can I reduce df if I estimated parameters from the data?

Yes. There is a checkbox on the input panel for exactly this case. When checked, the calculator subtracts 1 from k − 1 to account for the fact that a parameter of the hypothesized distribution was estimated from the same sample. The reduced df produces a more conservative and correct p-value.

Q: What does the contribution table tell me?

It shows each category's (O − E)² / E term. When the omnibus test is significant, the largest contributions identify which categories are driving the chi-square statistic. This is a diagnostic starting point, not a formal test of individual category differences — formal follow-up comparisons would need to control the familywise error rate.

Q: What does the warning about expected counts below 5 mean?

The chi-square approximation assumes expected counts are large enough that the sampling distribution of the statistic is well-approximated by the chi-square distribution. A common threshold is at least 5 in every expected cell. Below that, the approximation may be unreliable — combining sparse categories (if scientifically justified) or using an exact method is often the better choice.

Q: Why does the calculator warn me about two-category tests?

With only two categories, the chi-square goodness-of-fit test is mathematically related to the exact binomial test. For small samples the exact binomial is generally preferable because it does not rely on the chi-square approximation. The warning is there to prompt that consideration before you interpret the result.

Q: Does a non-significant result prove the null hypothesis is true?

No. A non-significant result means the data did not provide sufficient evidence to reject H₀ at the chosen α. It does not mean H₀ is true. The sample may simply be too small, or the true departure from the null may be smaller than the test has power to detect. Absence of evidence is not evidence of absence.

Q: Can I use this calculator for a chi-square goodness-of-fit test in R or SPSS?

For the standard case — independent observations, no estimated parameters, sufficient expected counts — the numbers match what R and SPSS produce. For estimated parameters the df adjustment matters; for small expected counts R and SPSS apply their own exact or simulation-based alternatives, which this calculator does not replicate.

Loaded shared calculation