hanegreefs.com/ statistics II
Home / Chapters / 07
Chapter 07 · Course ch. 15

Inference for counts: chi-square tests

Goodness-of-fit, homogeneity and independence with expected counts and standardised residuals.

About 14 min read · Lecture material: HOC 7

Priority labels: ImportantNeutralLess important show where to spend revision time.

When chi-square appliesImportant

Use chi-square methods when the data are counts of observations falling into categories: a frequency table for one categorical variable, or a two-way (contingency) table. The counts must be real counts; if you only have percentages, convert back using the sample size. Chi-square is not for means, and it is unreliable when expected counts are tiny.

ChapterVariablesGroups
10 / 121 binary categorical1
141 quantitative2
151–2 categorical, any number of categories1 or several

Three tests, one statisticImportant

TestQuestionDataH₀df
Goodness-of-fitDoes one variable follow a stated distribution?1 variable, 1 sample, k cellsCounts follow the model proportionsk − 1
HomogeneityIs the distribution of a variable the same across groups?1 variable, r separate groupsSame distribution in every group(r − 1)(c − 1)
IndependenceAre two variables associated?2 variables, 1 sampleVariables are independent(r − 1)(c − 1)

Homogeneity and independence use identical arithmetic; what differs is the sampling story and the wording of the conclusion ("the distributions are the same" versus "the variables are independent").

The test statisticImportant

Formula
χ² = Σ (Obs − Exp)² / Exp over all cells
  • χ² ≥ 0, and it grows as the data drift away from the model.
  • Only large values count against H₀, so the test is always one-sided: p = P(χ²_df > observed).
  • The χ² curve is right-skewed with mean = df; it flattens and shifts right as df grows.
  • The critical value depends on df only, not on n — which is why very large samples flag even small departures.

Expected countsImportant

Formula
Goodness-of-fit: Exp = n · pᵢ Two-way table: Exp = (row total × column total) / grand total

Always check the expected counts, not the observed ones. If you run the test on a TI-84 the matrix [B] stores them for inspection.

ConditionsImportant

  1. Counted data — frequencies, not percentages or measurements.
  2. Independence — a random sample or random assignment.
  3. Expected cell frequency — every expected count at least 5.

If a cell falls short, merge categories where that is sensible (or use Fisher's exact test, which is outside this course). The TI-84 gives no warning, so look at [B] yourself.

Which cells drive the result: standardised residualsImportant

Formula
standardised residual = (Obs − Exp) / √Exp

Only after a significant result, scan the residuals: |value| above 2 is noteworthy and above 3 is striking. The sign shows the direction (positive = more than expected). Squaring and summing the residuals reproduces χ².

Worked example

Plan choice in three cities (homogeneity)

100 customers per city choose a basic, standard or premium plan. Observed counts — City A: 50, 35, 15; City B: 30, 45, 25; City C: 20, 40, 40. Is the plan mix the same in all cities?

1

Hypotheses and df

H₀: same distribution in every city; df = (3−1)(3−1) = 4
2

Expected counts

each row: 100 × 100/300 = 33.3; 100 × 120/300 = 40; 100 × 80/300 = 26.7
3

Conditions

counted data; assume random samples; all expected ≥ 5 ✓
4

Statistic

χ² = 2.89² + … = 27.1; p ≈ 0.00002
5

Residuals

City A basic +2.9; City C basic −2.3; City C premium +2.6
Answer

Reject H₀: plan preferences differ by city. Basic is over-represented in A, premium in C.

Worked example

Goodness-of-fit: weekday traffic

A shop expects 20%, 25%, 25% and 30% of its 120 weekly visitors to arrive on four set days, but it counts 18, 31, 27, 44.

1

Expected counts

24, 30, 30, 36
2

Statistic

χ² = 36/24 + 1/30 + 9/30 + 64/36 ≈ 3.61; df = 3
Answer

p ≈ 0.31: the counts are consistent with the expected mix.

The 2 × 2 special caseImportant

For a 2 × 2 table the homogeneity test (df = 1) is the same as the two-proportion z-test: χ² = z² and the p-values coincide. The course prefers χ² for the test, but only the z-interval gives a confidence interval for the difference p₁ − p₂.

Example: 48 of 60 versus 30 of 60 successes gives χ² ≈ 11.87 on 1 df (p ≈ 0.0006), equal to z² with z ≈ 3.44.

Nine steps for any chi-square testImportant

  1. Pick the test (fit / homogeneity / independence).
  2. State H₀ and H_A in words.
  3. Check the three conditions.
  4. Find df.
  5. Compute expected counts.
  6. Compute χ².
  7. Get the p-value from the right tail.
  8. Conclude in context.
  9. If significant, inspect the standardised residuals.

PitfallsImportant

  • Feeding percentages instead of counts into the test.
  • Small expected counts (below 5) breaking the approximation.
  • Over-reading significance in large samples; check effect size.
  • Reading "association" as "causation" — χ² treats rows and columns symmetrically.
  • Mixing up fit (df = cells − 1) and independence (df = (r−1)(c−1)).

ToolsNeutral

TI-84: matrix [A] then STAT ▸ TESTS ▸ C:χ²-Test; goodness-of-fit with D:χ²GOF-Test (L1 observed, L2 expected, df = k − 1); a p-value from a known statistic with χ²cdf(x, 1E99, df).

tab <- matrix(c(50,30,20, 35,45,40, 15,25,40), nrow = 3) res <- chisq.test(tab) res$expected # check all >= 5 res$stdres # standardised residuals chisq.test(c(18,31,27,44), p = c(.2,.25,.25,.3))

Key takeaways

  • One formula Σ(O − E)²/E covers fit, homogeneity and independence; only E and df differ.
  • Check expected counts ≥ 5 and use real counts.
  • After a rejection, standardised residuals show which cells caused it.
Question
Tap or press space to reveal
Answer
Did you know it?

Multiple choice

Ch 07 · Question 1

df of a 3×4 contingency table?

Why: (3−1)(4−1) = 6.
Ch 07 · Question 2

Expected count for a cell: row total 40, column total 30, grand total 120?

Why: 40·30/120 = 10.
Ch 07 · Question 3

The χ² test is…

Why: Only large values count against H₀.
Ch 07 · Question 4

The χ² goodness-of-fit with 5 categories has df…

Why: k − 1.
Ch 07 · Question 5

A cell with expected count 2.3 means…

Why: Expected counts should be ≥ 5.
Ch 07 · Question 6

A standardised residual of +3.1 means that cell has…

Why: Observed exceeds expected by 3.1 SDs.
Ch 07 · Question 7

For a 2×2 table the χ² statistic equals…

Why: χ² = z², same p-value.
Ch 07 · Question 8

Which question suits a homogeneity test?

Why: Homogeneity compares groups on one categorical variable.

Exam-style questions

Exam-style · Ch 07

A firm records complaint type (3 categories) by branch (4 branches). Describe a full chi-square test of independence/homogeneity and how to interpret the result.

Show outline of a full answer
  1. H₀: distribution of complaint types is the same in all branches.
  2. Expected = row·column/total; check ≥ 5.
  3. χ² = Σ(O−E)²/E, df = (3−1)(4−1) = 6, upper tail.
  4. If rejected: inspect standardised residuals; no causality; mention sample-size sensitivity.

Chi-square calculatorInteractive

Type a table of counts (or a goodness-of-fit list). Get expected counts, χ², df, p-value and residuals.