a. Comparing Two Categorical Variables
So far, we’ve examined variables one at a time:
Today’s question: how do we summarize and visualize the relationship between two categorical variables — and what does it mean to say they’re associated?
When we examine two variables together:
Our goal: decide whether two categorical variables are associated.
| Coffee | Tea | Total | |
|---|---|---|---|
| Morning | 25 | 15 | 40 |
| Night | 35 | 25 | 60 |
| Total | 60 | 40 | 100 |
Example: 100 students surveyed about drink preference (coffee/tea) and morning/night habit.
A contingency table displays two categorical variables together.
| align | No Dual | Public | Secret | Unknown | Total |
|---|---|---|---|---|---|
| Bad | 453 | 2,106 | 4,352 | 7 | 6,918 |
| Good | 640 | 2,905 | 2,430 | 0 | 5,975 |
| Neutral | 377 | 946 | 908 | 2 | 2,233 |
| Total | 1,470 | 5,957 | 7,690 | 9 | 15,126 |
Raw counts alone don’t reveal patterns clearly — the row totals are very different.
Conditional proportion: the proportion within a row or column — calculated relative to that group’s total, not the grand total.
Formula (conditioned on rows): (count in cell) / (row total)
\begin{array}{c} \text{Proportion of} \\ \text{bad characters with} \\ \text{secret identities} \end{array} = \frac{\text{Bad characters with secret identity}}{\text{Total bad characters}}
Conditional proportions reveal patterns that raw counts hide.
| align | No Dual | Public | Secret | Unknown |
|---|---|---|---|---|
| Bad | 0.065 | 0.304 | 0.629 | 0.001 |
| Good | 0.107 | 0.486 | 0.407 | 0.000 |
| Neutral | 0.169 | 0.424 | 0.407 | 0.001 |
Key finding: Bad characters are more likely to have secret identities (62.9%) than good characters (40.7%) — the rows differ, suggesting alignment and identity type are associated.
A campus survey of 200 students asked about caffeine intake and typical sleep duration:
| Under 7 hrs sleep | 7+ hrs sleep | Total | |
|---|---|---|---|
| High caffeine | 75 | 45 | 120 |
| Low caffeine | 20 | 60 | 80 |
| Total | 95 | 105 | 200 |
What proportion of high-caffeine students sleep under 7 hours? 75 / 120 \approx 0.625 — about 62.5% of high-caffeine students sleep under 7 hours.
What proportion of low-caffeine students sleep under 7 hours? 20 / 80 = 0.25 — 25% of low-caffeine students sleep under 7 hours.
Do caffeine intake and sleep duration appear associated? Yes. The proportion sleeping under 7 hours is much higher in the high-caffeine group (62.5%) than the low-caffeine group (25%). Knowing one tells you something about the other.
Three bar-plot variants — same comics data, three views:
Dodged
Compare counts across groups
Stacked
Show composition of each group
Standardized
Compare proportions within groups
Standardized is the workhorse for spotting associations — every group on the same vertical scale.
Standardized bar plot of identity proportions by alignment
The Secret bands aren’t the same size across alignments — visual evidence of an association.
Summarizing two categorical variables:
Key concepts:
Up next (8b): when we can and can’t conclude that one variable causes changes in the other.