Ⅵ Counting, Probability & Statistics · Stage 34 — Statistics · 34.7 The Chi-Square TestAll lessons →
Stage 34 · Statistics

Association in Categorical Data: The Chi-Square Test

When two yes/no traits travel together, χ² puts a number on the suspicion.

Ages 15–18 · Reading the world from data
Two groups, the same two outcomes. If staying up late had nothing to do with feeling tired in class, both bars would split the same way. They do not — the left bar is mostly tired, the right bar mostly alert. χ² measures exactly how far that picture is from "no link."

Probability asked how likely is tomorrow? and the rest of Stage 34 squeezed a pile of numbers into a center, a spread, a line. But some data are not numbers at all — smokes or doesn't; tired in class or alert; left-handed or right-handed. These are categorical, and the only natural summary is a count. Pair two such traits and the counts fall into a little grid — a contingency table — and the question becomes: do the two traits travel together, or are they unrelated? This lesson builds one honest number, the chi-square statistic χ², that turns "those splits look different" into a verdict you can defend.

34.7.1 Categorical traits and the contingency table

A categorical variable sorts each individual into a named box, not onto a number line. "Eye color," "favorite sport," and the yes/no answer "Did you sleep fewer than six hours?" are all categorical. You cannot take their mean; you can only count how many landed in each box.

Pair two categorical traits and you cross-tabulate. Suppose we ask 100 students two questions — "Do you use a screen after midnight on school nights?" and "Do you feel tired in first period?" — and tally the answers into a 2×2 grid. That grid is the contingency table (or two-way table): rows for one trait, columns for the other, and a count in every cell.

Observed counts — 100 students (rows = screen habit, columns = how they feel)
TiredAlertRow total
Late screen341650
No late screen183250
Column total5248100

The inside four numbers are the cell counts; the margins are the row totals and column totals; the corner is the grand total n = 100. Everything we do flows from these counts — never from percentages someone already rounded off.

Key idea

A contingency table is just counts arranged by two categorical traits at once. The totals on the edges (the marginal counts) are the bookkeeping that the rest of the test leans on.

34.7.2 Spotting association from conditional rates

Raw counts can mislead because the row totals may differ. The fair comparison is a conditional rate: within each row, what fraction is tired? Among the 50 late-screen students, 34 are tired — a rate of 3450 = 68%. Among the 50 who avoid late screens, only 18 are tired — 1850 = 36%.

Those two rates are very different, so we suspect a link. The picture that shows this at a glance is the mosaic: one full-height bar per row, sliced by the column rates. If the two traits were unrelated, both bars would slice at the same height. When the slice lines sit at clearly different heights — as below — that mismatch is the visible face of association.

The mosaic of the table above. The late-screen bar is 68% tired; the no-late-screen bar is only 36% tired. The split heights disagree — that gap is what χ² will measure.
Watch out

Different conditional rates make you suspect a link — but a small sample can produce different rates by pure luck. "The mosaic looks lopsided" is a hunch, not a verdict. We need a number that accounts for how big the sample is. That number is χ².

Try it Edit a 2×2 table and watch the mosaic respond

Step the four cell counts. The table and the mosaic redraw, and the readout reports the two conditional "tired" rates and whether they differ.

a · Late + Tired
b · Late + Alert
c · No-late + Tired
d · No-late + Alert

34.7.3 Independence and the expected counts

To judge the gap fairly we ask a sharp question: if the two traits were truly unrelated (statisticians say independent), how many students would we expect in each cell? Independence means the "tired" rate is the same in both rows — namely the overall rate 52100 = 52%. So among the 50 late-screen students we would expect 50 × 0.52 = 26 to be tired.

That arithmetic packs into one clean rule. The expected count for a cell is

E = (row total) × (column total)grand total

Apply it to every cell. Row total 50, column total 52, grand total 100 gives E = 26; and so on for all four:

Expected counts under independence — E = rowSum · colSum / total
TiredAlert
Late screen2624
No late screen2624

The expected table has the same margins as the observed one (the row totals are still 50 and 50, the columns still 52 and 48) — it just spreads the counts as if the traits ignored each other. The closer the observed counts hug these expected ones, the less reason there is to claim a link.

Key idea

Expected counts are not a guess — they are the counts forced by the margins and the assumption of independence. Every expected cell is rowSum · colSum / total, computed by GX.chiExpected.

34.7.4 The chi-square statistic χ²

Now we need one number for "how far is observed from expected?" Cell by cell, take the gap O − E, square it (so over- and under-counts both count, and big misses count extra), and divide by E (so a gap of 8 means more in a cell of 5 than in a cell of 500). Add the four pieces:

χ² = Σ (O − E)²E

Worked example — χ² for the screen table

Each cell's contribution (O−E)²E:

Late + Tired: (34−26)²26 = 6426 = 2.462
Late + Alert: (16−24)²24 = 6424 = 2.667
No-late + Tired: (18−26)²26 = 2.462
No-late + Alert: (32−24)²24 = 2.667

Sum: χ² = 2.462 + 2.667 + 2.462 + 2.667 = 10.256.

The 2×2 shortcut agrees exactly: χ² = n(ad − bc)²(a+b)(c+d)(a+c)(b+d) = 100·(34·32 − 16·18)²50·50·52·48 = 10.256.

A χ² of 0 would mean observed equalled expected in every cell — perfect independence. The bigger the gaps, the bigger χ² climbs. So we have built a meter; next we calibrate it.

Try it Observed vs. expected, contribution by contribution

Choose a table. The left grid is your observed counts; the right grid is the expected counts from GX.chiExpected, each cell showing its (O−E)²/E. The readout sums them to χ² and confirms the 2×2 shortcut matches.

Table

34.7.5 The test and the verdict

How big is "big"? Even with truly independent traits, random sampling makes χ² wobble above 0. Statisticians worked out how far it typically wanders, and set a critical value as the line of reasonable doubt. For a 2×2 table the table has 1 degree of freedom (df = (rows−1)(cols−1) = 1·1 = 1), and at the 5% significance level the critical value is

χ²crit = 3.841  (df = 1, α = 0.05)

The rule is a single comparison:

If…Verdict
χ² is greater than 3.841the traits are associated
χ² is at most 3.841no evidence of association

Our screen table gave χ² = 10.256, well past 3.841 — so we conclude the late-screen habit and feeling tired are associated. (For a bigger table you would read a different critical value for its df: at df=2 the 5% line is 5.991, at df=3 it is 7.815, and so on.)

Notice what we just did: we measured a gap, compared it against a threshold, and reached a verdict — "associated" or "not." That move from evidence to a defensible yes/no is exactly the reasoning that Stage 35 · Logic will make formal, with hypotheses, claims, and proof. The chi-square test is the bridge from describing data to deciding with it.

Try it Strengthen the link and watch χ² cross the line

The slider shifts counts toward the diagonal — making the two rows split more and more differently. The gauge shows χ² rising past the dashed 3.841 verdict line.

Shift k (counts moved to the diagonal)
Watch out — what χ² does NOT tell you

Association is not causation. A big χ² says the two traits move together; it does not say one causes the other, nor which direction it runs. (A lurking variable — say, having a heavy course load — might drive both late screens and tiredness.)

It needs counts, not percentages. Feed χ² the raw cell counts; the same percentages from 20 people and from 2000 people carry very different weight.

Expected counts should be at least 5. When an expected cell is tiny, the χ² approximation gets shaky — gather more data or merge categories.

Size matters more than significance. With a huge sample even a trivial difference can push χ² past 3.841. "Statistically associated" is not the same as "a difference big enough to care about."

What to carry forward

The chi-square test of independence, in one card
StepWhat you doTool
1 · TabulateCross two categorical traits into a 2×2 (or r×c) count table with row/column totals.contingency table
2 · Compare ratesWithin each row, the conditional rate; a mosaic shows the splits.GX.mosaic
3 · ExpectE = rowSum · colSum / total in every cell (independence).GX.chiExpected
4 · Measureχ² = Σ (O−E)²/E; 2×2 shortcut agrees.GX.chiSq · GX.chiSq2x2
5 · Decidedf = (r−1)(c−1); compare χ² to the critical value (3.841 at df=1, α=0.05). Above ⇒ associated.verdict

Remember the three guards: counts not percentages, expected ≥ 5, and association ≠ causation.

Exercises

  1. A survey of 80 people crosses "drinks coffee" with "sleeps poorly." Among 40 coffee drinkers, 28 sleep poorly; among 40 non-drinkers, 12 sleep poorly. Write the conditional "sleeps poorly" rate for each group.
    Show answer
    Coffee: 28/40 = 70%. No coffee: 12/40 = 30%. The rates differ a lot, so we suspect an association — but a rate gap alone is not a verdict.
  2. For that same table — coffee row (28 poor, 12 well), no-coffee row (12 poor, 28 well) — find the expected count in the "coffee & sleeps poorly" cell.
    Show answer
    Row total 40, column "poor" total 28+12 = 40, grand total 80. E = 40·40/80 = 20. (Every cell here expects 20, since both margins are 40/40.)
  3. Using the 2×2 shortcut χ² = n(ad−bc)² / [(a+b)(c+d)(a+c)(b+d)] with a=28, b=12, c=12, d=28, n=80, compute χ².
    Show answer
    ad − bc = 28·28 − 12·12 = 784 − 144 = 640. Numerator = 80·640² = 80·409600 = 32 768 000. Denominator = 40·40·40·40 = 2 560 000. χ² = 32 768 000 / 2 560 000 = 12.8. Since 12.8 > 3.841, the two traits are associated.
  4. A table gives χ² = 2.9 on 1 degree of freedom. At the 5% level, what do you conclude?
    Show answer
    2.9 is at most the critical value 3.841, so there is no evidence of association at the 5% level. We do not "prove independence" — we simply fail to find a link.
  5. A researcher reports: "Owning an umbrella is associated with rain (χ² = 50, very significant), so umbrellas cause rain." Name two things wrong with this.
    Show answer
    (1) Association is not causation — and χ² never tells direction; if anything rain drives umbrella-buying, not the reverse. (2) A lurking variable (the weather/season) drives both. A large χ² shows the traits move together, nothing more.
  6. In a 2×2 table, every expected count comes out to 3. Why should you hesitate to trust the χ² verdict, and what could you do?
    Show answer
    The rule of thumb wants every expected count ≥ 5; at E = 3 the χ² approximation is unreliable. Collect more data (raise n) or, with bigger tables, merge sparse categories.

🎯 Quick check

Six questions to lock it in. Tap the answer you think is right.

§ For teachers and parents

This lesson develops CCSS HSS-ID.B.5 — summarizing categorical data in two-way frequency tables and reading joint, marginal, and conditional relative frequencies to recognize possible associations. The chi-square test of independence itself (expected counts, the χ² = Σ(O−E)²/E statistic, degrees of freedom, and the critical-value verdict) is an AP Statistics topic that formalizes B.5 — here it is introduced concretely on a 2×2 table. The "reach a verdict from evidence" move foreshadows Stage 35 · Logic. Throughout, every expected count, χ² value, and conditional rate is computed by the shared GX helpers (chiExpected, chiSq, chiSq2x2, mosaic), never read off a picture — and we stress the three classic cautions: association is not causation, χ² needs counts not percentages, and expected counts should be at least 5.

eastmath.com · Stage 34 · 34.7 The Chi-Square Test · Reading the world from data