SECTION 1
Historical Context & Motivation
Statistical inquiry has long grappled with a fundamental question: when we observe that two categorical attributes appear to co-vary in a sample, does that pattern reflect a genuine relationship in the population, or is it merely the product of sampling variability? Before the development of formal hypothesis tests for categorical data, researchers relied on intuitive judgments about tabulated frequencies—an approach that was subjective and unreliable. The chi-square test of independence emerged at the turn of the twentieth century to provide a principled, quantitative answer to precisely this question. Its development was intertwined with the broader formalization of mathematical statistics and marked a turning point in how scientists analyzed categorical data across disciplines from genetics to sociology.
1900 • Pearson's Chi-Square Statistic
1922 • Fisher's Degrees of Freedom Correction
1934 • Fisher's Exact Test for Small Samples
1954 • Yates' Continuity Correction
Modern Era • Computational & Simulation-Based Approaches
The central question that motivated Pearson's work remains the question at the heart of every chi-square test of independence: given observed frequencies in a two-way contingency table, is there sufficient evidence to conclude that the row variable and the column variable are associated, or can the observed pattern be attributed to chance alone? Understanding this test equips you with one of the most widely used tools in the analyst's toolkit for categorical data.
SECTION 2
Core Principles & Definitions
Before performing a chi-square test of independence, it is essential to understand the conceptual architecture that supports it. The test operates on data organized in a contingency table (also called a cross-tabulation or two-way table), where each cell contains the frequency count for a specific combination of levels from two categorical variables. The logic of the test mirrors hypothesis testing in general: we posit a null hypothesis, compute a test statistic that measures deviation from that hypothesis, and then compare the statistic to a reference distribution to obtain a p-value.
Contingency Table
Null Hypothesis (H₀)
Expected Frequencies
Chi-Square Statistic (χ²)
Degrees of Freedom
KEY TAKEAWAY
Assumptions & Conditions
SECTION 3
Visual Explanation: The Contingency Table
The diagram below illustrates the structure of a generic r × c contingency table and how the expected frequencies are derived from marginal totals. Understanding this visual layout is critical because every computation in the chi-square test flows directly from the observed and expected values in each cell of this table.
In the diagram, each interior cell of the table contains both the observed count Oij and, in parentheses, the expected count Eij. The marginal totals along the right edge (Ri) and bottom edge (Cj) are essential because the expected frequency for any cell is simply the product of its row total and column total divided by the grand total N. This formula operationalizes the null hypothesis: if the two variables are independent, the proportion of observations in row i should be the same across all columns, and the expected count reflects that proportional distribution.
SECTION 4
Mathematical Framework
The mathematical machinery of the chi-square test of independence involves three key formulas: computing expected frequencies under H₀, calculating the χ² test statistic, and determining the degrees of freedom that specify which chi-square distribution to use as the reference. Each formula plays a distinct role in the logical chain from data to decision.
EXPECTED FREQUENCY
Where Eij is the expected count in cell (i, j), Ri is the total for row i, Cj is the total for column j, and N is the grand total of all observations.
CHI-SQUARE TEST STATISTIC
The summation runs over all r × c cells in the contingency table. Oij is the observed count and Eij is the expected count. Squaring the difference ensures all deviations contribute positively, and dividing by Eij standardizes each term so that a deviation of 5 in a cell with an expected count of 10 is weighted more heavily than the same deviation in a cell with an expected count of 100.
DEGREES OF FREEDOM
Where r is the number of rows and c is the number of columns. Once the marginal totals are fixed, only (r − 1)(c − 1) cell frequencies are free to vary—the rest are determined by the constraint that rows and columns must sum to their respective marginals.
Under the null hypothesis and given sufficiently large expected counts, the test statistic χ² follows approximately a chi-square distribution with (r − 1)(c − 1) degrees of freedom. The p-value is the probability of observing a χ² value at least as large as the one computed from the data, assuming H₀ is true. Because chi-square distributions are right-skewed, the test is always one-tailed (right tail): only large values of χ² provide evidence against independence.
Why Divide by Eᵢⱼ?
SECTION 5
Decision Process & the χ² Distribution
Once you have computed the χ² test statistic and identified the degrees of freedom, the final step is to locate the computed value on the appropriate chi-square distribution curve and determine whether it falls in the rejection region. The following diagram illustrates this decision process for a test conducted at the α = 0.05 significance level.
The curve shown is the probability density function for a χ² distribution with 4 degrees of freedom. As degrees of freedom increase, the distribution shifts to the right and becomes more symmetric, approaching a normal distribution for large df. Notice that the distribution is right-skewed and bounded below by zero—this reflects the fact that χ², being a sum of squared terms, can never be negative. The critical value of 9.488 is the point beyond which only 5% of the distribution's area lies, so if the computed χ² exceeds this threshold, we conclude that the observed data would be very unlikely under the assumption of independence.
- Step 1: State H₀ (variables are independent) and H₁ (variables are not independent).
- Step 2: Choose a significance level α (commonly 0.05).
- Step 3: Construct the contingency table and compute all expected frequencies.
- Step 4: Verify that all expected counts are ≥ 5.
- Step 5: Compute χ² = Σ (O − E)² / E and the degrees of freedom.
- Step 6: Compare χ² to χ²_crit or compute the p-value. If p < α, reject H₀.
SECTION 6
Worked Example
A university health center surveyed 500 students about their sleep quality (Good or Poor) and their academic performance (Above Average, Average, Below Average). The observed data are shown below. At the α = 0.05 significance level, test whether sleep quality and academic performance are independent.
| Sleep Quality | Above Avg | Average | Below Avg | Row Total |
|---|---|---|---|---|
| Good | 120 | 100 | 30 | 250 |
| Poor | 80 | 100 | 70 | 250 |
| Col Total | 200 | 200 | 100 | 500 |
Observed Frequencies: Sleep Quality vs. Academic Performance
Chi-Square Test of Independence: Sleep Quality vs. Academic Performance
- 1
Step 1 — State Hypotheses
H₀: Sleep quality and academic performance are independent. H₁: Sleep quality and academic performance are not independent (they are associated). - 2
Step 2 — Compute Expected Frequencies
For each cell, Eij = (Row total × Column total) / Grand total. For Good & Above Avg: E = (250 × 200) / 500 = 100. For Good & Average: E = (250 × 200) / 500 = 100. For Good & Below Avg: E = (250 × 100) / 500 = 50. For Poor & Above Avg: E = (250 × 200) / 500 = 100. For Poor & Average: E = (250 × 200) / 500 = 100. For Poor & Below Avg: E = (250 × 100) / 500 = 50.Expected frequencies: 100, 100, 50, 100, 100, 50. All Eij ≥ 5 ✓ - 3
Step 3 — Compute the χ² Statistic
χ² = Σ (O − E)² / E = (120 − 100)²/100 + (100 − 100)²/100 + (30 − 50)²/50 + (80 − 100)²/100 + (100 − 100)²/100 + (70 − 50)²/50 = 400/100 + 0/100 + 400/50 + 400/100 + 0/100 + 400/50 = 4 + 0 + 8 + 4 + 0 + 8 = 24.0χ² = 24.0 - 4
Step 4 — Determine Degrees of Freedom
df = (r − 1)(c − 1) = (2 − 1)(3 − 1) = 1 × 2 = 2.df = 2 - 5
Step 5 — Find Critical Value and Make Decision
From the chi-square table, χ²crit at α = 0.05 with df = 2 is 5.991. Since our computed χ² = 24.0 is much greater than 5.991, we reject H₀. Equivalently, the p-value is approximately 6.1 × 10⁻⁶, which is far below 0.05.Reject H₀: There is statistically significant evidence that sleep quality and academic performance are associated (χ² = 24.0, df = 2, p < 0.001). - 6
Step 6 — Interpret in Context
Students with good sleep quality are overrepresented in the 'Above Average' category (120 observed vs. 100 expected) and underrepresented in the 'Below Average' category (30 observed vs. 50 expected). The reverse pattern holds for poor sleepers. This suggests that better sleep quality is associated with higher academic performance in this sample, though the chi-square test does not establish causation.
SECTION 7
Strengths, Limitations & Common Pitfalls
Like any statistical tool, the chi-square test of independence has important strengths and limitations that determine when it should—and should not—be used. A thoughtful analyst understands these boundaries and selects the appropriate alternative when the chi-square test's assumptions are violated.
| Strengths | Limitations |
|---|---|
| Works with nominal and ordinal categorical data—no distributional assumption about the variables themselves. | Requires sufficiently large expected counts (all E ≥ 5); breaks down with sparse data. |
| Computationally straightforward and easy to interpret: a single test statistic and p-value. | Does not indicate the direction or strength of association—only that one exists. |
| Applicable to tables of any dimension (r × c), not just 2 × 2. | Cannot establish causation; association may be confounded by lurking variables. |
| Well-understood asymptotic theory with widely available critical-value tables. | Sensitive to sample size: very large N can yield statistical significance for trivially small associations. |
| Nonparametric—no requirement for normally distributed underlying data. | Ignores the ordinal structure of variables; treats all categories as purely nominal. |
Strengths and Limitations of the Chi-Square Test of Independence
KEY TAKEAWAY
Common Pitfall: Using Percentages Instead of Counts
SECTION 8
Connection to Related Tests & Advanced Theory
The chi-square test of independence does not exist in isolation. It belongs to a broader family of chi-square tests and connects to several other inferential procedures. Understanding these connections clarifies when to use the test of independence versus its relatives and how it serves as a gateway to more sophisticated analyses.
| Test / Measure | Purpose | Relationship to χ² Independence Test |
|---|---|---|
| χ² Goodness-of-Fit | Tests whether a single categorical variable follows a hypothesized distribution. | Same χ² formula but applied to a one-way table (one variable, k categories) rather than a two-way table. |
| Fisher's Exact Test | Tests independence in 2 × 2 tables when expected counts are small. | An exact alternative that does not rely on the large-sample χ² approximation; preferred when any E < 5. |
| G-Test (Likelihood Ratio) | Tests independence using a log-likelihood ratio statistic. | Asymptotically equivalent to Pearson's χ²; preferred in some advanced settings due to additive decomposability. |
| Cramér's V | Measures the strength of association between two categorical variables. | Computed from χ² as V = √(χ² / (N × min(r−1, c−1))); provides effect size context the test alone lacks. |
| Log-Linear Models | Model the cell counts in multi-way contingency tables as functions of main effects and interactions. | Generalize the independence test to three or more variables; the two-way χ² test is a special case of a saturated log-linear model comparison. |
Related Tests and Measures in Categorical Data Analysis
For students continuing into advanced categorical data analysis, the chi-square test of independence can be viewed as a special case of the likelihood ratio framework central to generalized linear models. In that framework, the null hypothesis of independence corresponds to a log-linear model with no interaction term, and rejecting H₀ motivates fitting models with interaction effects. Additionally, when one variable is naturally a predictor and the other a response, the chi-square test of independence is equivalent to comparing proportions across groups—linking it directly to logistic regression for binary outcomes. These connections illustrate that the chi-square test is not merely a standalone procedure but the entry point into a rich hierarchy of models for categorical data.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
Show answer
Question 2: BASIC CALCULATION
Show answer
Question 3: INTERMEDIATE
Show answer
Question 4: APPLIED
Show answer
Question 5: CRITICAL THINKING
Show answer
SUMMARY
Summary
Summary
The chi-square test of independence determines whether two categorical variables are associated in a population by comparing observed frequencies in a contingency table to the expected frequencies derived under the assumption of independence. The test statistic χ² = Σ (O − E)² / E aggregates standardized squared deviations across all cells, and is compared to a chi-square distribution with (r − 1)(c − 1) degrees of freedom to obtain a p-value.
Key requirements include random sampling, independent observations, and all expected counts ≥ 5. When the test rejects H₀, follow up with Cramér's V for effect size and standardized residuals to identify which cells drive the association. Remember: the chi-square test detects association, not causation, and statistical significance does not imply practical significance—always consider effect size alongside p-values.