BIOSTATISTICS • CATEGORICAL DATA ANALYSIS

Chi-Square Goodness-of-Fit

A foundational test for determining whether observed categorical frequencies match a hypothesized distribution.

Historical Context & Motivation

The analysis of categorical data — counts of observations falling into discrete categories — has been a persistent challenge in the biological and social sciences since the era of early probability theory. When a geneticist observes phenotypic ratios, an epidemiologist tallies disease classifications across populations, or an ecologist catalogs species abundances in habitat plots, each is confronted with the same fundamental question: do these observed frequencies conform to a theoretically expected pattern, or has something systematically departed from expectation? Before the development of a formal test, researchers relied on subjective judgment to decide whether deviations from expected proportions were "close enough" to be attributable to random sampling variability. The chi-square goodness-of-fit test was developed precisely to replace that subjective assessment with a rigorous, probability-based framework for evaluating categorical distributions.

1900
Pearson's Seminal Paper
Karl Pearson published "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," introducing the χ² statistic as a measure of discrepancy between observed and expected frequencies.
1922
Fisher's Correction on Degrees of Freedom
Ronald A. Fisher demonstrated that Pearson's original degrees-of-freedom formula required adjustment when parameters were estimated from the data, establishing the modern rule of subtracting the number of estimated parameters from k − 1.
1934
Cochran's Guidelines on Expected Counts
William G. Cochran formalized the conditions under which the chi-square approximation is reliable, recommending that expected frequencies should generally be at least 5 per cell, a guideline that remains widely cited in biostatistics curricula.
1952
Exact and Simulation-Based Alternatives
With the advent of electronic computing, exact multinomial tests and Monte Carlo simulation methods emerged as alternatives to the asymptotic chi-square approximation, extending the test's applicability to sparse-data scenarios common in genomics and rare-disease epidemiology.

The central question that the chi-square goodness-of-fit test addresses is deceptively simple: could the differences between what we observed and what we expected reasonably have arisen by chance alone? This question lies at the heart of hypothesis testing in biostatistics, where categorical outcomes — genotype classes, treatment response categories, diagnostic classifications — are the natural currency of data. Understanding how Pearson's χ² statistic quantifies deviation from expectation, how it is distributed under the null hypothesis, and under what conditions the approximation holds is essential groundwork for the broader toolkit of categorical data analysis, including tests of independence, homogeneity, and loglinear modeling.

Core Principles & Definitions

Before computing any test statistic, it is important to internalize the conceptual architecture underlying the chi-square goodness-of-fit test. The test operates on a single categorical variable with k mutually exclusive and exhaustive categories. One collects a random sample of n observations and records how many fall into each category (the observed frequencies). A theoretical model, prior research, or biological hypothesis supplies the expected frequencies — the counts one would anticipate if the null hypothesis were true. The test then asks whether the discrepancy between observed and expected counts is larger than what chance alone would typically produce.

1

Null & Alternative Hypotheses

H₀ states that the population proportions equal the hypothesized values (p₁ = π₁, p₂ = π₂, …, pₖ = πₖ). H₁ states that at least one population proportion differs from its hypothesized value.
2

Observed vs. Expected Frequencies

Observed frequencies (Oᵢ) are the empirical counts from data. Expected frequencies (Eᵢ) are computed as Eᵢ = n × πᵢ, where πᵢ is the hypothesized probability for category i and n is the total sample size.
3

The χ² Test Statistic

The statistic sums the squared standardized residuals across all k categories: χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ. Squaring ensures all deviations contribute positively; dividing by Eᵢ normalizes for the scale of each expected count.
4

Degrees of Freedom

For k categories with all proportions specified under H₀, df = k − 1. If m additional parameters are estimated from the data (e.g., fitting a Poisson mean), then df = k − 1 − m, per Fisher's correction.
5

Assumptions & Conditions

The observations must be independent, the data must arise from a random sample, categories must be mutually exclusive and exhaustive, and each expected frequency Eᵢ should be ≥ 5 (Cochran's guideline) for the χ² approximation to be reliable.
KEY TAKEAWAY
Think of the chi-square goodness-of-fit test as a quality-control inspector comparing a factory's actual output distribution to its design specifications. If the inspector finds that the proportions of different product grades deviate substantially from the engineered targets, the process is declared out of specification. The χ² statistic is the inspector's summary score: the larger it is, the greater the evidence that the actual distribution has departed from the target — and the p-value quantifies how surprising that score would be if the process were truly on target.

Visual Explanation

A bar chart comparing observed and expected frequencies is perhaps the most intuitive way to visualize the goodness-of-fit test. The diagram below illustrates a hypothetical genetics experiment in which offspring phenotypes are observed and compared against the classic Mendelian 9:3:3:1 expectation. Each pair of bars — observed in cyan and expected in violet — shows how the empirical data stacks up against theory. The vertical gap between bars in any category represents the residual (Oᵢ − Eᵢ), and the χ² statistic aggregates these gaps into a single measure of overall discrepancy.

The bar chart shows four phenotype categories from a dihybrid cross with n = 160 offspring. Cyan bars represent observed frequencies and violet bars represent expected frequencies under the 9:3:3:1 model. Notice that the aabb class deviates most sharply — this category would contribute the largest component to the χ² statistic.

Examining the diagram, the first two categories (A_B_ and A_bb) show virtually no discrepancy: observed counts match expectations perfectly at 90 and 30, respectively. The aaB_ category shows a modest positive residual of +5, contributing a moderate term to the sum. However, the aabb category shows the most dramatic departure — only 5 offspring were observed when 10 were expected, yielding a residual of −5 that, once squared and divided by the small expected count, produces a comparatively large component. This visual underscores a critical feature of the χ² statistic: deviations from categories with small expected values can dominate the test statistic even when they involve relatively modest absolute differences, because the denominator Eᵢ amplifies those discrepancies.

Mathematical Framework

The mathematical foundation of the chi-square goodness-of-fit test rests on the multinomial sampling model. Under the null hypothesis, n independent trials each produce an outcome in one of k categories with fixed probabilities π₁, π₂, …, πₖ, where Σπᵢ = 1. The joint probability of observing counts O₁, O₂, …, Oₖ is given by the multinomial distribution. Karl Pearson showed that under H₀, as n → ∞, a particular function of the observed and expected counts converges in distribution to a chi-square random variable. This asymptotic result is the engine that drives the test.

PEARSON'S CHI-SQUARE STATISTIC
χ² = Σᵢ₌₁ᵏ (Oᵢ − Eᵢ)² / Eᵢ
where Oᵢ = observed count in category i, Eᵢ = n × πᵢ = expected count in category i under H₀, and k = number of categories. Each term (Oᵢ − Eᵢ)²/Eᵢ is the squared standardized residual for cell i.
EXPECTED FREQUENCY COMPUTATION
Eᵢ = n × πᵢ
where n is the total sample size and πᵢ is the hypothesized probability for category i. For example, under a 9:3:3:1 ratio with n = 160, E₁ = 160 × (9/16) = 90.
DEGREES OF FREEDOM
df = k − 1 − m
where k is the number of categories and m is the number of parameters estimated from the data. When all proportions are fully specified under H₀ (e.g., a genetic ratio), m = 0 and df = k − 1. When fitting a distribution (e.g., Poisson) whose parameter is estimated from the sample, m ≥ 1.

The key asymptotic result is that under the null hypothesis, as n grows large, the test statistic χ² converges in distribution to a chi-square distribution with df degrees of freedom. This convergence relies on the central limit theorem applied to multinomial counts: each standardized residual (Oᵢ − Eᵢ)/√Eᵢ is approximately standard normal for large n, and the sum of their squares — subject to the constraint that ΣOᵢ = ΣEᵢ = n — follows a χ² distribution with k − 1 degrees of freedom. Because the chi-square distribution is right-skewed and bounded below by zero, the p-value is always computed from the right tail: p = P(χ²(df) ≥ χ²_obs). If p < α, we reject H₀ and conclude that the observed distribution differs significantly from the hypothesized one.

💡 Why Divide by Eᵢ?
Division by Eᵢ serves as a normalization that accounts for the inherent variability of each cell count. Under multinomial sampling, the variance of Oᵢ is approximately n × πᵢ × (1 − πᵢ) ≈ Eᵢ for moderate πᵢ. Without this division, categories with large expected counts would dominate the sum regardless of how well the model fits. Dividing by Eᵢ puts all categories on a comparable scale, so that a deviation of 5 from an expectation of 10 is treated more seriously than a deviation of 5 from an expectation of 100.

The Chi-Square Distribution & Decision Making

Understanding the shape of the chi-square distribution is essential for interpreting test results. The χ² distribution is a family of continuous distributions indexed by degrees of freedom (ν). For small ν, the distribution is strongly right-skewed; as ν increases, it becomes more symmetric and approaches a normal distribution (by the central limit theorem, since a χ² random variable is a sum of squared standard normals). The mean of a χ²(ν) distribution equals ν and its variance equals 2ν, which means the distribution's center and spread are both governed by the degrees of freedom. In a goodness-of-fit context, if your computed χ² value is much larger than the mean ν, that signals the observed data deviate substantially from expectations.

The figure displays χ² density curves for df = 2, df = 4, and df = 8. The red shaded area shows the rejection region for α = 0.05 with df = 8, where χ²_crit ≈ 15.51. Observed statistics falling in this tail lead to rejection of H₀.

The decision rule is straightforward. After computing the test statistic χ²_obs and identifying the appropriate degrees of freedom, one compares χ²_obs to the critical value χ²_α,df obtained from a chi-square table or statistical software. Equivalently, one computes the p-value as the area under the χ²(df) curve to the right of χ²_obs. If p < α (commonly 0.05 in biostatistics), we reject H₀ and conclude that the observed distribution is not consistent with the hypothesized model. It is important to note that rejection tells us that the data deviate from the null model, but not which categories are responsible or why. Post-hoc inspection of individual standardized residuals (Oᵢ − Eᵢ)/√Eᵢ can help identify the specific cells driving the overall significance.

Selected critical values of the χ² distribution
dfχ²₀.₁₀χ²₀.₀₅χ²₀.₀₁
12.7063.8416.635
24.6055.9919.210
36.2517.81511.345
47.7799.48813.277
59.23611.07015.086

Worked Example

A genetics researcher crosses two dihybrid pea plants (AaBb × AaBb) and observes 160 offspring. Under the Mendelian model of independent assortment with complete dominance at both loci, the expected phenotypic ratio is 9:3:3:1. The researcher records the following phenotype counts: A_B_ = 90, A_bb = 30, aaB_ = 35, aabb = 5. Does this sample provide statistically significant evidence against the 9:3:3:1 model at α = 0.05?

Testing a 9:3:3:1 Mendelian Ratio
1
Step 1 — State HypothesesH₀: The offspring are distributed in a 9:3:3:1 ratio, i.e., π₁ = 9/16, π₂ = 3/16, π₃ = 3/16, π₄ = 1/16. H₁: At least one population proportion differs from the hypothesized value. We will use α = 0.05.
2
Step 2 — Compute Expected FrequenciesWith n = 160 total offspring: E₁ = 160 × (9/16) = 90, E₂ = 160 × (3/16) = 30, E₃ = 160 × (3/16) = 30, E₄ = 160 × (1/16) = 10. All expected frequencies exceed 5, so Cochran's condition is satisfied.
E = {90, 30, 30, 10} — all ≥ 5 ✓
3
Step 3 — Verify AssumptionsThe offspring are assumed to be genetically independent (no epistasis, no linkage, random mating). The categories are mutually exclusive and exhaustive. The sample arises from a controlled cross. All expected counts ≥ 5. Assumptions are met.
4
Step 4 — Calculate the χ² Statisticχ² = (90 − 90)²/90 + (30 − 30)²/30 + (35 − 30)²/30 + (5 − 10)²/10 = 0/90 + 0/30 + 25/30 + 25/10 = 0 + 0 + 0.833 + 2.500 = 3.333.
χ²_obs = 3.333
5
Step 5 — Determine Degrees of FreedomWe have k = 4 categories and no parameters estimated from the data (the proportions are fully specified by the Mendelian model), so df = 4 − 1 − 0 = 3.
df = 3
6
Step 6 — Find Critical Value and p-valueFrom the χ² table with df = 3 and α = 0.05, the critical value is χ²₀.₀₅,₃ = 7.815. Our observed statistic χ²_obs = 3.333 < 7.815. Equivalently, using software or a chi-square CDF, the p-value is P(χ²(3) ≥ 3.333) ≈ 0.343.
p ≈ 0.343
7
Step 7 — State ConclusionSince p = 0.343 > α = 0.05 (and equivalently, χ²_obs = 3.333 < χ²_crit = 7.815), we fail to reject H₀. There is insufficient evidence at the 5% significance level to conclude that the offspring phenotype distribution deviates from the expected 9:3:3:1 Mendelian ratio. The observed deviations are consistent with random sampling variability.
Fail to reject H₀ — data are consistent with the 9:3:3:1 model.

Strengths, Limitations & Common Pitfalls

Like every statistical procedure, the chi-square goodness-of-fit test has a well-defined domain of applicability and a set of conditions under which it can mislead. Understanding these boundaries is as important as understanding the mechanics of the test itself. The table below summarizes the principal strengths and limitations, followed by a discussion of common pitfalls encountered in biostatistical practice.

Strengths and limitations of the chi-square goodness-of-fit test
StrengthsLimitations
Distribution-free: no assumption about the underlying shape of the population distribution is required beyond the multinomial model.Requires sufficiently large expected counts (Eᵢ ≥ 5 by Cochran's guideline) for the χ² approximation to be reliable.
Simple computation: the formula involves only elementary arithmetic, making it accessible without specialized software.Omnibus test: rejection indicates some deviation from H₀ but does not identify which specific categories are responsible.
Flexible: can test any fully specified multinomial distribution, from uniform to complex genetic ratios.Sensitive to sample size: with very large n, trivially small deviations from H₀ achieve significance; with small n, meaningful departures may go undetected.
Well-established theory: its asymptotic properties have been rigorously studied for over a century.Requires independent observations: clustered, paired, or longitudinally correlated data violate the independence assumption.
Widely implemented in all major statistical software (R, SAS, SPSS, Python).Cannot handle continuous data directly — the variable must be categorized, which may involve information loss.
⚠️ COMMON PITFALLS
Three mistakes appear frequently in biostatistical applications. First, using percentages or proportions instead of raw counts in the formula — the test requires actual frequencies, not relative ones. Second, applying the test when expected counts are very small (< 5) without resorting to exact methods or collapsing categories, which leads to inflated Type I error rates. Third, conflating "fail to reject H₀" with "H₀ is true" — a non-significant result may simply reflect insufficient power, not genuine conformity to the hypothesized model. Always consider statistical power and effect size alongside the p-value.

Connection to Advanced Categorical Methods

The chi-square goodness-of-fit test is the conceptual gateway to a rich family of categorical data methods. Once you master the logic of comparing observed to expected frequencies in a one-way table, the same architecture extends naturally to two-way and multi-way tables, where you encounter tests of independence and homogeneity. Furthermore, the likelihood-ratio statistic G² and loglinear models generalize the Pearson χ² framework into a flexible modeling paradigm for complex categorical data structures. The table below highlights key distinctions and extensions.

Comparing the goodness-of-fit framework with two-way table methods
FeatureGoodness-of-Fit TestTest of Independence / Homogeneity
Number of variablesOne categorical variable (one-way table)Two categorical variables (two-way table)
Null hypothesisData follow a specified distributionThe two variables are independent / populations are homogeneous
Source of expected valuesHypothesized proportions (externally specified)Marginal proportions estimated from the data
Degrees of freedomk − 1 − m(r − 1)(c − 1)
Advanced generalizationsExact multinomial test, G² likelihood-ratio testLoglinear models, logistic regression, CMH test for stratified tables

Two important extensions deserve mention. The likelihood-ratio chi-square statistic (G² = 2 Σ Oᵢ ln(Oᵢ/Eᵢ)) is asymptotically equivalent to Pearson's χ² but is preferred in the context of loglinear modeling because G² statistics are additive when partitioning complex models. For small samples where the χ² approximation breaks down, exact multinomial tests compute p-values directly from the multinomial probability mass function, and Monte Carlo simulation offers a computationally tractable alternative when the number of categories is too large for exact enumeration. In contemporary biostatistics, these tools are readily accessible via functions such as chisq.test() in R and scipy.stats.chisquare() in Python.

Practice Problems

PROBLEM 1CONCEPTUAL
A researcher uses a chi-square goodness-of-fit test and obtains χ² = 0. Explain what this result implies about the relationship between the observed and expected frequencies, and discuss whether this necessarily means the null hypothesis is true.
PROBLEM 2BASIC CALCULATION
A six-sided die is rolled 120 times, yielding the following face counts: 1→25, 2→17, 3→19, 4→22, 5→15, 6→22. Test whether the die is fair at α = 0.05.
PROBLEM 3INTERMEDIATE
An epidemiologist classifies 200 patients into four blood types and wishes to test whether the distribution matches the known population frequencies for a reference population: O = 0.44, A = 0.42, B = 0.10, AB = 0.04. Observed counts are: O = 82, A = 89, B = 20, AB = 9. Compute the χ² statistic, determine the degrees of freedom, and draw a conclusion at α = 0.05.
PROBLEM 4APPLIED
A microbiologist hypothesizes that bacterial colonies on 400 agar plates follow a Poisson distribution with a mean estimated from the sample. The observed frequency distribution is: 0 colonies → 75 plates, 1 → 120, 2 → 100, 3 → 60, 4 → 30, 5+ → 15. The sample mean is 1.75. Compute the expected Poisson frequencies, calculate χ², and test goodness-of-fit at α = 0.05. Note the adjustment to degrees of freedom for estimating the mean.
PROBLEM 5CRITICAL THINKING
A geneticist tests a 3:1 Mendelian ratio with 10,000 offspring and obtains χ² = 12.5 (df = 1, p < 0.001). She concludes that Mendelian inheritance is violated. A colleague tests the same ratio with 40 offspring and obtains χ² = 1.2 (df = 1, p = 0.27). He concludes that Mendelian inheritance is confirmed. Critically evaluate both conclusions. Discuss the concepts of statistical power, effect size, and the distinction between statistical and practical significance.

Summary

The chi-square goodness-of-fit test evaluates whether observed categorical frequencies are consistent with a set of expected frequencies derived from a null hypothesis. The test statistic χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ aggregates squared standardized residuals across all categories. Under H₀ and appropriate conditions (independent observations, Eᵢ ≥ 5), the statistic follows a chi-square distribution with df = k − 1 − m, where k is the number of categories and m is the number of parameters estimated from the data.

The test's power depends on sample size, the magnitude of the true departure from H₀, and the number of categories. Always report effect sizes alongside p-values and examine individual standardized residuals to identify which categories drive a significant result. The goodness-of-fit test serves as the conceptual foundation for more advanced methods including tests of independence, loglinear models, and the likelihood-ratio G² statistic.

Varsity Tutors • Biostatistics • Chi-Square Goodness-of-Fit