Frequency tables and bar plots for a single categorical variable
Summary statistics and histograms for a single numerical variable
Hypothesis tests and CIs for a single proportion
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?
Association and Independence
When we examine two variables together:
Associated: there is a relationship
Knowing the value of one variable tells you something about the other
Independent: there is no relationship
Knowing one variable tells you nothing about the other
Our goal: decide whether two categorical variables are associated.
Comic Characters
15,126 characters from DC and Marvel comics
Two key categorical variables:
Alignment: good, bad, neutral
Identity type: secret, public, no dual, unknown
Research question: is alignment associated with identity type?
Contingency Tables
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.
Each cell holds the count for that combination of values
Row totals: counts for each row category
Column totals: counts for each column category
Grand total: the sample size — often called \(n\)
Comics Contingency Table
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 Proportions
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.
Comics Conditional Proportions
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.
Caffeine and Sleep
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.
Visualizing Two Categorical Variables
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.
Comics Standardized Bar Plot
Standardized bar plot of identity proportions by alignment
The Secret bands aren’t the same size across alignments — visual evidence of an association.
Summary
Summarizing two categorical variables:
Contingency table: counts for each combination
Conditional proportions: proportions within a row (or column) — reveal patterns hidden by raw counts
Standardized bar plot: visualize the conditional proportions side-by-side
Key concepts:
Associated: the conditional proportions differ across groups
Independent: the conditional proportions are (essentially) the same
Up next (8b): when we can and can’t conclude that one variable causes changes in the other.