AP Statistics • Inference for Categorical Data: Chi-Square

Carrying Out a Chi-Square Test for Homogeneity or Independence

Determine whether categorical variables are related by comparing observed and expected counts in two-way tables.

A scatter plot with data columns and a percentage symbol.
Inside this lesson 9 sections 5 questions 9 figures

SECTION 1

Historical Context & Motivation

Long before modern software made hypothesis testing routine, researchers faced a fundamental question: when you observe different proportions across groups, how do you know the difference is real rather than the product of random sampling variability? In the late nineteenth century, Karl Pearson developed a single statistic—what we now call the chi-square statistic—that elegantly measures the total discrepancy between what we observe in categorical data and what we would expect if there were no association. This idea became the backbone of two closely related tests: the chi-square test for independence and the chi-square test for homogeneity.

1900 • Pearson's Chi-Square

Karl Pearson publishes 'On the Criterion that a Given System of Deviations…,' introducing the χ² goodness-of-fit statistic and the family of chi-square distributions.

1922 • Fisher's Correction

R. A. Fisher clarifies the correct degrees of freedom for contingency tables, establishing (r − 1)(c − 1) as the standard and resolving Pearson's earlier miscalculation.

1954 • Cochran's Guidelines

W. G. Cochran formalizes the expected-count condition: the chi-square approximation is reliable when all expected counts are at least 5, a rule still cited on the AP exam today.

2000s • Modern AP Curriculum

The College Board integrates chi-square tests for goodness-of-fit, homogeneity, and independence as core inference procedures, emphasizing a unified four-step framework: State, Plan, Do, Conclude.

The central question these tests address is deceptively simple: Is there convincing evidence that two categorical variables are associated, or that the distribution of a categorical variable differs across populations? Answering this requires a structured hypothesis-testing procedure that converts a two-way table of counts into a single test statistic, then assesses how unusual that statistic would be if the null hypothesis were true.

SECTION 2

Core Principles & Definitions

Before executing the mechanics of a chi-square test, it is essential to distinguish the two test types and understand the logic that unites them. Both tests use the same formula and the same chi-square distribution, but they arise from different sampling designs and answer subtly different questions.

1

Test for Independence

A single sample is drawn from one population and each individual is classified on two categorical variables. H₀: the two variables are independent.
2

Test for Homogeneity

Independent samples are drawn from two or more populations (or treatment groups), and each individual is classified on one categorical variable. H₀: the distribution of that variable is the same across all populations.
3

Expected Counts

Under H₀, the expected count for any cell equals (row total × column total) / grand total. These represent counts we'd anticipate if no association exists.
4

Chi-Square Statistic

χ² = Σ (O − E)² / E sums squared deviations of observed from expected, scaled by expected count, across every cell. Larger values signal greater departure from H₀.
5

Degrees of Freedom

df = (r − 1)(c − 1), where r is the number of rows and c is the number of columns. This determines which chi-square distribution is used to find the P-value.

SECTION 3

Visual Explanation

Flowchart: Carrying Out a Chi-Square Test

Chi-Square Test ProcedureSTATE: Hypotheses + αPLAN: Name test + Check conditionsRandom: SRS or random assignmentLarge Counts: all E ≥ 5DO: Calculate χ² and P-valueE = (row tot × col tot) / nχ² = Σ(O − E)² / E, df = (r−1)(c−1)CONCLUDE: Compare P-value to αP ≤ α → Reject H₀ (association)P > α → Fail to reject H₀
This flowchart traces the four-step procedure (State → Plan → Do → Conclude) used for both the chi-square test for homogeneity and the chi-square test for independence. The 'Plan' step branches into two conditions that must be verified before proceeding.

The flowchart above captures the backbone of every chi-square test on the AP exam. Notice that the Plan stage requires you to check two conditions: the data must come from a random process (either an SRS from a population or a randomized experiment), and all expected counts must be at least 5. If either condition fails, the chi-square approximation may be unreliable and the conclusion invalid. In the 'Do' step, you compute expected counts using the formula E = (row total × column total) / n, then plug every cell's observed and expected counts into the chi-square formula before looking up the P-value using the χ² distribution with (r − 1)(c − 1) degrees of freedom.

SECTION 4

Mathematical Framework

The mathematical machinery behind the chi-square test is elegant in its simplicity. Three formulas do all the heavy lifting: one computes expected counts, one computes the test statistic, and one determines degrees of freedom. Together they convert a table of raw counts into a single number that can be compared against a known probability distribution.

EXPECTED COUNT

E=(rowtotal×columntotal)/nE = (row total × column total) / n

For each cell in the two-way table, E is the count expected under H₀. The row total is the sum of observed counts in that cell's row, the column total is the sum in that cell's column, and n is the grand total of all observations.

CHI-SQUARE TEST STATISTIC

χ2=Σ(O−E)2/Eχ² = Σ (O − E)² / E

Sum over all cells in the table. O is the observed count and E is the expected count for each cell. Squaring ensures positive and negative deviations contribute equally; dividing by E standardizes the contribution so that cells with larger expected counts do not dominate by default.

DEGREES OF FREEDOM

df=(r−1)(c−1)df = (r − 1)(c − 1)

Here r is the number of rows and c is the number of columns in the two-way table. The degrees of freedom determine which member of the chi-square family of distributions is used to compute the P-value.

P-VALUE

P−value=P(χ2≥χ2observed∣df)P-value = P(χ² ≥ χ²observed | df)

The P-value is always a right-tail probability because larger χ² values correspond to greater departure from H₀. Use a χ² distribution table or your calculator's χ²cdf function.

ℹ
Calculator Tip

SECTION 5

Anatomy of Chi-Square Components

Each cell in the two-way table contributes one component to the overall chi-square statistic. Examining these individual components tells you which cells deviate most from what independence or homogeneity would predict. After rejecting H₀, it is good practice (and often requested on the AP exam) to identify the largest components and explain their direction—whether the observed count is above or below the expected count—because this provides context for the nature of the association.

Chi-Square Components by CellBar height = (O − E)² / E for each cell. Taller bars → bigger contributors to χ².02465.52Cell (1,1)2.25Cell (1,2)1.33Cell (2,1)0.80Cell (2,2)0.47Cell (3,1)0.26Cell (3,2)Component value
Each bar represents one cell's contribution (O − E)² / E to the overall χ² statistic. Cell (1,1) contributes the most at 5.52—this cell drives the bulk of the evidence against H₀. Summing all bars gives the total χ² value of 10.63.

The bar chart above illustrates how unevenly components can contribute. In this hypothetical 3 × 2 table, Cell (1,1) alone accounts for over half of the total χ² = 10.63. On a free-response question, identifying this cell and stating whether O exceeded or fell short of E demonstrates a deeper understanding than simply reporting χ² and the P-value. Remember: the chi-square statistic is a sum, and understanding the parts of that sum tells you where the association is strongest.

ℹ
AP Exam Tip

SECTION 6

Worked Example

A researcher surveys 400 randomly selected adults from three regions (North, Central, South) and records whether each person supports or opposes a proposed policy. The data are summarized below.

SupportOpposeRow Total
North7050120
Central10555160
South7545120
Col Total250150400

Observed counts for policy opinion by region

Worked example

  1. 1

    Step 1 — State Hypotheses

    H₀: The distribution of opinion (Support vs. Oppose) is the same for all three regions. Hₐ: The distribution of opinion is not the same for all three regions. We use α = 0.05.
  2. 2

    Step 2 — Plan: Name Test & Check Conditions

    We will perform a chi-square test for homogeneity. Random: The problem states adults were randomly selected. Large Counts: We compute expected counts: E(North, Support) = 120 × 250 / 400 = 75; E(North, Oppose) = 120 × 150 / 400 = 45; E(Central, Support) = 160 × 250 / 400 = 100; E(Central, Oppose) = 160 × 150 / 400 = 60; E(South, Support) = 120 × 250 / 400 = 75; E(South, Oppose) = 120 × 150 / 400 = 45. All expected counts are ≥ 5. ✓
  3. 3

    Step 3 — Do: Compute χ² and P-value

    Compute each component (O − E)² / E: (70 − 75)²/75 = 0.333; (50 − 45)²/45 = 0.556; (105 − 100)²/100 = 0.250; (55 − 60)²/60 = 0.417; (75 − 75)²/75 = 0.000; (45 − 45)²/45 = 0.000.
    χ² = 0.333 + 0.556 + 0.250 + 0.417 + 0.000 + 0.000 = 1.556. df = (3 − 1)(2 − 1) = 2. Using χ²cdf(1.556, 10⁹, 2), the P-value ≈ 0.459.
  4. 4

    Step 4 — Conclude

    Because the P-value (0.459) is greater than α = 0.05, we fail to reject H₀. There is not convincing evidence that the distribution of opinion on the proposed policy differs across the three regions.
ℹ
Scoring Note

SECTION 7

Homogeneity vs. Independence: Strengths & Limitations

FeatureTest for HomogeneityTest for Independence
Sampling DesignIndependent random samples from 2+ populations (or groups in an experiment)Single random sample from one population; each individual classified on two variables
Null HypothesisThe distribution of the response variable is the same across all populationsThe two categorical variables are independent in the population
Formulaχ² = Σ (O − E)² / E — identicalχ² = Σ (O − E)² / E — identical
Degrees of Freedom(r − 1)(c − 1) — identical(r − 1)(c − 1) — identical
Key LimitationCannot establish causation unless data come from a randomized experimentCannot establish causation; association ≠ causation for observational data
ConditionsRandom samples/assignment; all expected counts ≥ 5Random sample; all expected counts ≥ 5

Side-by-side comparison of the two chi-square tests

SECTION 8

Connection to Advanced Methods

The chi-square test for homogeneity and independence is your first encounter with a broader family of categorical-data methods. In a college statistics course or graduate research, you will meet extensions that handle situations the basic chi-square test cannot.

AP-Level ConceptAdvanced Extension
χ² test with all expected counts ≥ 5Fisher's Exact Test: used when expected counts are small and the chi-square approximation is unreliable
Two-way table with r × c cellsLog-linear models: model complex multi-way contingency tables with three or more categorical variables simultaneously
χ² tests association (yes/no)Cramér's V and odds ratios: measure the strength and direction of association, not just its existence
Independent observationsMcNemar's test: handles paired or matched categorical data (e.g., before/after studies)

For the AP exam, you will not be asked to perform these advanced methods, but understanding that the chi-square test has limitations—it cannot measure strength of association, it requires sufficiently large expected counts, and it assumes independent observations—prepares you to appreciate why statisticians developed these refinements. A strong conceptual grasp of the basic chi-square procedure also provides the foundation for logistic regression, which models the relationship between a categorical response and one or more explanatory variables.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

A researcher collects a single random sample of 500 high-school students and records each student's grade level (9, 10, 11, 12) and preferred lunch option (hot meal, packed lunch, skip lunch). Which chi-square test is appropriate? (A) Goodness-of-fit test (B) Test for homogeneity (C) Test for independence (D) Two-sample z-test for proportions (E) Paired t-test
  • A. Goodness-of-fit test
  • B. Test for homogeneity
  • C. Test for independence
  • D. Two-sample z-test for proportions
  • E. Paired t-test
Show answer (C)
Correct: (C). Because one sample is drawn and each student is classified on two categorical variables (grade level and lunch preference), the test for independence is appropriate. (A) is wrong because a goodness-of-fit test applies to a single categorical variable tested against a hypothesized distribution. (B) is wrong because a test for homogeneity requires separate samples from distinct populations, not a single sample classified two ways. (D) is wrong because a two-sample z-test compares proportions for one binary variable across exactly two groups and uses a different distribution. (E) is wrong because a paired t-test is for quantitative paired data, not categorical data.

Question 2: BASIC CALCULATION

In a 3 × 4 two-way table with a grand total of 600, one cell has a row total of 200 and a column total of 150. The observed count for that cell is 60. What is that cell's contribution to the chi-square statistic? (A) 1.00 (B) 2.00 (C) 2.40 (D) 3.00 (E) 10.00
  • A. 1.00
  • B. 2.00
  • C. 2.40
  • D. 3.00
  • E. 10.00
Show answer (B)
Correct: (B). E = (200 × 150) / 600 = 50. Component = (60 − 50)² / 50 = 100 / 50 = 2.00. (A) is wrong because it uses an incorrect expected count or formula. (C) is wrong—2.40 arises from dividing by an incorrect denominator. (D) is wrong because 3.00 would result from using (O − E)² / E with E = 33.33, which is not the correct expected count. (E) is wrong because 10.00 equals (O − E)² without dividing by E.

Question 3: INTERMEDIATE

A chi-square test for homogeneity is performed on a 2 × 3 contingency table. The test statistic is χ² = 9.73 and α = 0.05. What conclusion should be drawn? (A) Fail to reject H₀ because 9.73 < 9.488 (B) Reject H₀ because 9.73 > 5.991 (C) Fail to reject H₀ because 9.73 < 12.592 (D) Reject H₀ because 9.73 > 7.815 (E) Reject H₀ because 9.73 > 3.841
  • A. Fail to reject H₀ because 9.73 < 9.488
  • B. Reject H₀ because 9.73 > 5.991
  • C. Fail to reject H₀ because 9.73 < 12.592
  • D. Reject H₀ because 9.73 > 7.815
  • E. Reject H₀ because 9.73 > 3.841
Show answer (B)
Correct: (B). df = (2 − 1)(3 − 1) = 2. The critical value for χ² with 2 df at α = 0.05 is 5.991. Since 9.73 > 5.991, we reject H₀. (A) is wrong because 9.488 is the critical value for 4 df, not 2 df. (C) is wrong because 12.592 is the critical value for 6 df. (D) is wrong because 7.815 is the critical value for 3 df, which would apply to a 2 × 4 table, not 2 × 3. (E) is wrong because 3.841 is the critical value for 1 df, which would apply to a 2 × 2 table.

Question 4: APPLIED

A hospital randomly selects 300 patient records and classifies each by treatment group (Drug A, Drug B, Placebo) and outcome (Improved, No Change, Worsened). The observed and expected counts are shown below. | | Improved | No Change | Worsened | Row Total | |---|---|---|---|---| | Drug A | 55 | 30 | 15 | 100 | | Drug B | 48 | 35 | 17 | 100 | | Placebo | 27 | 45 | 28 | 100 | | Col Total | 130 | 110 | 60 | 300 | Carry out the full four-step chi-square test at α = 0.05. State hypotheses, verify conditions, calculate χ² and the P-value, and state a conclusion in context. Identify which cell contributes most to χ² and interpret its direction.
Show answer
STATE (1 pt): H₀: The distribution of outcomes (Improved, No Change, Worsened) is the same for Drug A, Drug B, and Placebo. Hₐ: The distributions are not all the same. α = 0.05. PLAN (1 pt): We use a chi-square test for homogeneity. Random: records were randomly selected. Large Counts: Each row total is 100 and each column total is 130, 110, or 60, so E = (100 × col total)/300. Smallest expected count = (100 × 60)/300 = 20 ≥ 5. ✓ DO (1 pt): Expected counts: E(A,Imp) = 43.33, E(A,NC) = 36.67, E(A,Wor) = 20; E(B,Imp) = 43.33, E(B,NC) = 36.67, E(B,Wor) = 20; E(P,Imp) = 43.33, E(P,NC) = 36.67, E(P,Wor) = 20. Components: (55−43.33)²/43.33 = 3.14; (30−36.67)²/36.67 = 1.21; (15−20)²/20 = 1.25; (48−43.33)²/43.33 = 0.50; (35−36.67)²/36.67 = 0.08; (17−20)²/20 = 0.45; (27−43.33)²/43.33 = 6.15; (45−36.67)²/36.67 = 1.89; (28−20)²/20 = 3.20. χ² = 17.87. df = (3−1)(3−1) = 4. P-value = P(χ² ≥ 17.87 | df = 4) ≈ 0.0013. CONCLUDE (1 pt): Because the P-value (0.0013) < 0.05, we reject H₀. There is convincing evidence that the distribution of outcomes differs across the three treatment groups. The largest component (6.15) comes from the Placebo–Improved cell, where O = 27 < E = 43.33, indicating that placebo patients improved at a much lower rate than would be expected if outcomes were distributed equally across groups.

Question 5: CRITICAL THINKING

A student performs a chi-square test for independence on a 2 × 2 table with n = 40 and obtains χ² = 4.82 with a P-value of 0.028. A classmate notes that a two-proportion z-test on the same data yields z = 2.196 and a two-sided P-value of 0.028. (a) Verify algebraically that z² = χ² in this setting. (b) Explain why the P-values are identical despite the tests using different reference distributions. (c) Discuss a scenario in which the chi-square test can address a question that the two-proportion z-test cannot, and explain why. (d) If the expected count in one cell were 3.5, explain which condition is violated and what alternative procedure you would recommend.
Show answer
(a) (1 pt): z² = (2.196)² ≈ 4.823 ≈ χ². For a 2 × 2 table, the chi-square statistic with 1 df is algebraically equivalent to the square of the two-proportion z-statistic. Both statistics measure the same discrepancy between observed and expected proportions but express it differently. (b) (1 pt): The χ²₁ distribution is the distribution of Z² where Z ~ N(0,1). Therefore, P(χ²₁ ≥ 4.82) equals P(|Z| ≥ √4.82) = P(|Z| ≥ 2.196), which is the two-sided P-value from the z-test. The right tail of χ²₁ captures both tails of the standard normal, so the P-values match exactly. (c) (1 pt): When the response variable has more than two categories (e.g., a 2 × 3 or 3 × 4 table), the two-proportion z-test is not applicable because it handles only two groups and a binary response. The chi-square test generalizes to any r × c table, making it the appropriate choice for comparing distributions with multiple categories. (d) (1 pt): The Large Counts condition requires all expected counts to be at least 5. An expected count of 3.5 violates this condition, so the chi-square approximation may not be accurate. Fisher's Exact Test is the recommended alternative because it computes the exact probability of observing a table as extreme as (or more extreme than) the observed table under H₀, without relying on the chi-square approximation.

Varsity Tutors • AP Statistics • Carrying Out a Chi-Square Test for Homogeneity or Independence