Quiz 2

Contingency Tables — Analyzing Two Categorical Variables

2968 words
15 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Contingency Tables — Analyzing Two Categorical Variables ## 🎯 Learning Objectives After completing this topic, you will be able to: - Construct **contingency tables** (two-way tables) from raw data - Compute **marginal distributions** and **conditional distributions** - Determine whether two categorical variables...

Contingency Tables — Analyzing Two Categorical Variables

🎯 Learning Objectives

After completing this topic, you will be able to:
  • Construct contingency tables (two-way tables) from raw data
  • Compute marginal distributions and conditional distributions
  • Determine whether two categorical variables are associated
  • Calculate and interpret joint probabilities and conditional probabilities
  • Create and interpret mosaic plots and stacked bar charts

📋 Prerequisites


📖 Core Content

7.1 Intuition: When One Variable Isn't Enough

So far, we've analyzed one variable at a time (univariate analysis). But the most interesting questions involve relationships between variables:
  • Does gender relate to job promotion?
  • Is there a relationship between smoking and lung cancer?
  • Does education level affect voting preference? Contingency tables (also called two-way tables or cross-tabulations) are the primary tool for summarizing and analyzing two categorical variables simultaneously.
Everyday analogy: Imagine a restaurant wants to know if there's a relationship between the day of the week (weekday vs weekend) and customer satisfaction (satisfied vs dissatisfied). They'd create a table crossing "Day" with "Satisfaction" — that's a contingency table. 🔑 Key Insight: The question is always: "Does the distribution of one variable change depending on the other variable?" If yes, the variables are associated.

7.2 Anatomy of a Contingency Table

A contingency table shows the frequency of observations for every combination of two categorical variables.

Example: Gender and Product Preference

A survey of 200 people asks about gender (Male, Female) and preferred product (A, B).
Product AProduct BRow Total
Male4060100
Female7030100
Column Total11090200
Terminology:
  • Cell frequencies: The 4 inner values (40, 60, 70, 30) — number of people in each combination
  • Row totals: 100 men, 100 women
  • Column totals: 110 prefer A, 90 prefer B
  • Grand total: 200 (sum of all cells = sum of row totals = sum of column totals)

7.3 Joint Distribution

The joint distribution shows the proportion of observations in each cell relative to the grand total.
Product AProduct BTotal
Male40/200 = 0.2060/200 = 0.300.50
Female70/200 = 0.3530/200 = 0.150.50
Total0.550.451.00
Interpretation: 20% of all respondents are males who prefer Product A. 35% are females who prefer Product A. These are joint probabilities.

7.4 Marginal Distribution

The marginal distribution shows the distribution of one variable alone, ignoring the other. It's what we studied in Week 2 — just frequency tables. Marginal distribution of Gender (Row totals):
  • Male: 100/200 = 50%
  • Female: 100/200 = 50% Marginal distribution of Product (Column totals):
  • Product A: 110/200 = 55%
  • Product B: 90/200 = 45% Key insight: The "margins" of the table give us the univariate distributions.

7.5 Conditional Distribution

The conditional distribution shows the distribution of one variable for a specific value of the other variable. This is where we start detecting associations. Conditional distribution of Product preference given Gender: Among Males:
  • Product A: 40/100 = 40%
  • Product B: 60/100 = 60% Among Females:
  • Product A: 70/100 = 70%
  • Product B: 30/100 = 30% Interpretation: Males prefer Product B (60% vs 40%), while Females prefer Product A (70% vs 30%). This is a clear association between gender and product preference.
🔑 Check for association: If the conditional distributions are the same for all values of the other variable, there's no association. If they differ, there's an association.

7.6 Independence vs Association

Two categorical variables are independent (no association) if the conditional distributions of one variable are identical across all categories of the other variable. Hypothetical independent data:
Product AProduct BTotal
Male5545100
Female5545100
Total11090200
Conditional distributions:
  • Males: 55% A, 45% B
  • Females: 55% A, 45% B Identical! Gender and product preference are independent — knowing gender tells you nothing about product preference.

7.7 Simpson's Paradox

Simpson's Paradox occurs when a trend appears in several groups of data but disappears or reverses when the groups are combined. Example: Two hospitals perform a surgery.
HospitalSmall Stones Success RateLarge Stones Success RateOverall Success Rate
A93% (81/87)73% (192/263)78% (273/350)
B87% (234/270)69% (55/80)83% (289/350)
Wait! Hospital A has a higher success rate for both small stones (93% > 87%) AND large stones (73% > 69%). But Hospital B has a higher overall success rate (83% > 78%). What's happening? Hospital A treats far more large stones (263 vs 80), which are harder. Hospital B treats more small stones (270 vs 87), which are easier. The overall rate is confounded by the case mix. Lesson: Always consider conditional distributions before marginal ones. Aggregating data can hide meaningful patterns.

7.8 Mosaic Plots

A mosaic plot is a visual representation of a contingency table where:
  • The width of each column represents the marginal frequency of that category
  • The height of each rectangle represents the conditional frequency
  • The area of each rectangle represents the joint frequency (Diagram) Interpretation: If the variables were independent, the rectangles would align perfectly (no "break" in the pattern). The more the rectangles shift, the stronger the association.

7.9 Measures of Association for Categorical Variables

While the full treatment (chi-square test) comes in BSMA1004, we can describe association strength using:

Contingency Coefficient

A simple measure: compare the observed frequencies to what we'd expect under independence. We'll explore this fully in Stats 2.

Relative Risk and Odds Ratio

For 2×2 tables (both variables have two categories): Relative Risk (for cohort studies):
RR=Risk in exposed groupRisk in unexposed group\text{RR} = \frac{\text{Risk in exposed group}}{\text{Risk in unexposed group}}
Odds Ratio (for case-control studies):
OR=Odds of exposure in casesOdds of exposure in controls\text{OR} = \frac{\text{Odds of exposure in cases}}{\text{Odds of exposure in controls}}
Example: Smoking and lung cancer
Lung CancerNo Lung Cancer
Smoker6040
Non-smoker1585
Risk of lung cancer:
  • Smokers: 60/100 = 0.60
  • Non-smokers: 15/100 = 0.15 Relative Risk: 0.60/0.15 = 4.0 Interpretation: Smokers are 4 times more likely to develop lung cancer than non-smokers.

7.10 Worked Examples

Example 1: Constructing a Contingency Table (Easy)

Scenario: 150 people are surveyed about their exercise habits (Regular, Sometimes, Never) and health status (Good, Poor). Data:
  • Regular exercise, Good health: 40
  • Regular exercise, Poor health: 10
  • Sometimes exercise, Good health: 35
  • Sometimes exercise, Poor health: 15
  • Never exercise, Good health: 20
  • Never exercise, Poor health: 30 Construct the contingency table and find marginal distributions. Solution: Step 1 — Fill the table:
Good HealthPoor HealthRow Total
Regular401050
Sometimes351550
Never203050
Column Total9555150
Step 2 — Marginal distribution of exercise:
  • All row totals are 50 — equal thirds. Each exercise level has 33.3%. Step 3 — Marginal distribution of health:
  • Good: 95/150 = 63.3%
  • Poor: 55/150 = 36.7% Step 4 — Conditional distributions: Among Regular exercisers:
  • Good: 40/50 = 80%
  • Poor: 10/50 = 20% Among Sometimes exercisers:
  • Good: 35/50 = 70%
  • Poor: 15/50 = 30% Among Never exercisers:
  • Good: 20/50 = 40%
  • Poor: 30/50 = 60% Interpretation: There's a clear association: more frequent exercise is associated with better health. Among regular exercisers, 80% have good health. Among those who never exercise, only 40% have good health.

Example 2: Determining Independence (Medium)

Scenario: 300 people were surveyed on hair color (Black, Brown, Blonde) and eye color (Brown, Blue).
Brown EyesBlue EyesTotal
Black Hair8040120
Brown Hair603090
Blonde Hair405090
Total180120300
Are hair color and eye color independent? Solution: Check: Is the conditional distribution of eye color the same for all hair colors? Conditional distributions:
Hair Color% Brown Eyes% Blue Eyes
Black80/120 = 66.7%40/120 = 33.3%
Brown60/90 = 66.7%30/90 = 33.3%
Blonde40/90 = 44.4%50/90 = 55.6%
Analysis:
  • Black and Brown hair: same distribution (66.7% brown eyes) — consistent between these two
  • Blonde hair: different (44.4% brown eyes, 55.6% blue eyes) Conclusion: Hair color and eye color are not independent. Blonde-haired people are more likely to have blue eyes than people with black or brown hair.

Example 3: Simpson's Paradox (Harder)

Scenario: Two admissions programs at a university. Program A (Arts):
AdmittedRejectedTotalAdmit Rate
Female802010080%
Male40105080%
Program B (Engineering):
AdmittedRejectedTotalAdmit Rate
Female10405020%
Male3012015020%
Overall Combined:
AdmittedRejectedTotalAdmit Rate
Female906015060%
Male7013020035%
Wait — within each program, admit rates are EQUAL by gender (Program A: 80% each, Program B: 20% each). But overall, females have a HIGHER admit rate (60% vs 35%). What's happening?
  • Women are more likely to apply to Program A (high admit rate, 80%)
  • Men are more likely to apply to Program B (low admit rate, 20%)
  • The overall rate is confounded by program choice Lesson: The apparent overall gender disparity is NOT due to gender discrimination — it's due to different application patterns. This is Simpson's Paradox.

7.11 Edge Cases & Gotchas

Small Cell Counts

When a cell has a very small count (e.g., 0, 1, 2), percentages become unreliable. A single observation shifting categories can dramatically change the interpretation. Rule of thumb: Be cautious with conditional distributions when the base (row total) is less than about 20.

More Than Two Categories

Contingency tables can extend to any number of categories per variable (e.g., 3×4, 4×5). The same analysis applies, but visualization becomes harder.

Zero Rows or Columns

If a row or column sum is zero, that variable level has no observations. This may indicate an impossible combination or a data collection issue.

7.12 Why This Matters

Contingency tables are everywhere:
  • Medicine: Treatment vs outcome tables
  • Marketing: Customer segment vs purchase behavior
  • Social science: Demographics vs voting patterns
  • Quality control: Defect type vs production shift They also form the foundation for:
  • BSMA1004 (Stats 2): Chi-square tests of independence
  • BSMS2002 (Business Analytics): Customer segmentation and A/B testing
  • BSMA3012 (Linear Stat Models): Logistic regression uses odds ratios

📐 Key Formulas / Concepts

ConceptCalculationInterpretation
Cell frequencyCount in each combinationObserved joint frequency
Row totalSum of cells in a rowMarginal frequency of row variable
Column totalSum of cells in a columnMarginal frequency of column variable
Grand totalSum of all cellsTotal sample size
Joint proportionCell / Grand totalP(Category A and Category B)
Marginal proportionRow or column total / Grand totalP(Category A) alone
Conditional proportionCell / Row total (or cell / Column total)P(B
IndependenceAll conditional distributions are identicalNo association between variables
Relative RiskRisk exposed / Risk unexposedHow many times more likely
Odds RatioOdds exposed / Odds unexposedStrength of association

⚠️ Common Pitfalls

Pitfall 1: Confusing Conditional and Joint Distributions

The mistake: Saying "20% of all people are males who prefer Product B" when you mean "20% of males prefer Product B" — or vice versa. Why it happens: People confuse the denominator (row total vs grand total). How to avoid:
  • Joint: "X% of all respondents" (denominator = grand total)
  • Conditional: "X% of [group]" (denominator = row total for that group)

Pitfall 2: Claiming Causation from Association

The mistake: Seeing an association in a contingency table and concluding one variable causes the other. Why it happens: Contingency tables show association, not causation. Correction: Observational studies can only show association. Experiments (with random assignment) are needed for causation. The association between smoking and lung cancer was observed in contingency tables before causation was established.

Pitfall 3: Ignoring Simpson's Paradox

The mistake: Analyzing only the aggregate (marginal) table without checking the disaggregated (conditional) tables. Why it happens: The aggregate table is simpler and seems to tell a clear story. Correction: Always check conditional distributions. If the grouping variable is important (like the hospital's case mix), present the disaggregated results.

Pitfall 4: Computing Percentages in the Wrong Direction

The mistake: Computing the conditional distribution of X given Y when you meant to compute Y given X. Why it happens: Both look similar but answer different questions. Check: "What percentage of males prefer Product A?" → use Males as the denominator (row). "What percentage of Product A preferers are male?" → use Product A as the denominator (column). These are different numbers!

📝 Practice Questions

Q1: Building a Contingency Table
200 people are surveyed: 120 women, 80 men. 90 people prefer online shopping, 110 prefer in-store. Among online shoppers, 60 are women. Build the contingency table.
<details> <strong>Solution</strong>
Known:
  • Women: 120, Men: 80
  • Online: 90, In-store: 110
  • Women who prefer online: 60
OnlineIn-StoreTotal
Women60120-60 = 60120
Men90-60 = 3080-30 = 5080
Total90110200
Check: 60+60+30+50 = 200 ✅
</details> > **Q2: Conditional Distributions** > > Using the table from Q1, compute: > > a) % of women who prefer online shopping b) % of men who prefer online shopping c) % of online shoppers who are women d) Is there an association between gender and shopping preference? > > <details> <strong>Solution</strong> > > a) **Women online:** 60/120 = 50% b) **Men online:** 30/80 = 37.5% c) **Online shoppers who are women:** 60/90 = 66.7% > > d) **Association?** Yes. Women are more likely to prefer online shopping (50%) than men (37.5%). The conditional distributions differ. </details> > **Q3: Independence Check** > >
PassedFailedTotal
Studied7525100
Didn't Study153550
Total9060150
Are studying and passing independent?
<details> <strong>Solution</strong>
Conditional distributions:
Among those who studied: Pass = 75/100 = 75%, Fail = 25/100 = 25%
Among those who didn't study: Pass = 15/50 = 30%, Fail = 35/50 = 70%
The distributions are very different (75% pass vs 30% pass). Therefore, studying and passing are NOT independent — there's a strong association.
(As we'd expect — studying helps!)
</details> > **Q4: Joint Probabilities** > >
CoffeeTeaTotal
Morning302050
Afternoon153550
Total4555100
Find the joint probabilities for all four cells.
<details> <strong>Solution</strong>
Joint probability = cell / grand total
  • P(Morning AND Coffee) = 30/100 = 0.30
  • P(Morning AND Tea) = 20/100 = 0.20
  • P(Afternoon AND Coffee) = 15/100 = 0.15
  • P(Afternoon AND Tea) = 35/100 = 0.35
Check: 0.30 + 0.20 + 0.15 + 0.35 = 1.00 ✅
</details> > **Q5: Simpson's Paradox** > > A university has two colleges. The combined admission data shows: > >
Admit Rate
Women45%
Men55%
But within each college, admit rates are equal (50% for both genders). Explain how this is possible.
<details> <strong>Solution</strong>
This is Simpson's Paradox. It happens when:
  • Women disproportionately apply to the more competitive college (lower admit rate)
  • Men disproportionately apply to the less competitive college (higher admit rate)
Even though each college admits genders equally, the overall rate favors men because of the different application patterns.
Example:
  • College X (competitive, 30% admit): 90% of women apply here, 10% of men
  • College Y (easy, 70% admit): 10% of women apply here, 90% of men
Women: 0.90(30%) + 0.10(70%) = 34% overall Men: 0.10(30%) + 0.90(70%) = 66% overall
The overall disparity is entirely due to application patterns, not discrimination.
</details> > **Q6: Relative Risk** > >
DiseaseNo Disease
Exposed50150
Unexposed10190
Compute the relative risk. Interpret.
<details> <strong>Solution</strong>
Risk in exposed: 50/(50+150) = 50/200 = 0.25 Risk in unexposed: 10/(10+190) = 10/200 = 0.05
Relative Risk: 0.25/0.05 = 5.0
Interpretation: Exposed individuals are 5 times more likely to develop the disease compared to unexposed individuals.
RR=5.0\boxed{\text{RR} = 5.0}
</details> > **Q7: Building from Raw Data** > > Raw data (20 people): > > - Male, Urban: 5 > - Male, Rural: 3 > - Female, Urban: 8 > - Female, Rural: 4 > > Create the contingency table and compute the conditional distribution of location for each gender. > > <details> <strong>Solution</strong> > > **Contingency table:** > >
UrbanRuralTotal
Male538
Female8412
Total13720
Conditional distributions:
Males: Urban = 5/8 = 62.5%, Rural = 3/8 = 37.5% Females: Urban = 8/12 = 66.7%, Rural = 4/12 = 33.3%
Conclusion: The distributions are very similar (62.5% vs 66.7% urban). There's little to no association between gender and urban/rural location in this sample.
</details> > **Q8: Application** > > You're analyzing a marketing campaign. You send an email to 2,000 customers (1,000 get Version A, 1,000 get Version B). Version A gets 80 clicks (8%), Version B gets 120 clicks (12%). Among those who clicked, 60% made a purchase for Version A vs 40% for Version B. > > a) Create a contingency table for email version × clicked (Yes/No). b) Create a contingency table for email version × purchased (Yes/No) among those who clicked. c) Which version has a higher click rate? Higher conversion rate? d) What would you recommend? > > <details> <strong>Solution</strong> > > **a) Version × Clicked:** > >
ClickedNot ClickedTotal
Version A809201000
Version B1208801000
Total20018002000
b) Among clickers, Version × Purchased:
Version A clickers: 80, of which 60% = 48 purchased Version B clickers: 120, of which 40% = 48 purchased
PurchasedNot PurchasedTotal
Version A483280
Version B4872120
Total96104200
c) Comparison:
  • Click rate: Version B (12%) > Version A (8%)
  • Conversion rate (click→purchase): Version A (60%) > Version B (40%)
  • Total purchases: Identical (48 each)
d) Recommendation:
  • If the goal is clicks (e.g., ad revenue per click), choose Version B
  • If the goal is purchases, they're equal (48 each)
  • But Version A has a better quality of click (higher conversion rate)
  • Optimal choice: Consider an A/B test with a combined metric (total revenue). If the goal is purchases, the versions are equivalent.
</details> * * * ## 🔗 Cross-References - **Next topic:** [Correlation](/notes/01-foundation-bsma1002-stats-1-week04-07-correlation) — measuring relationships between numerical variables - **Previous:** [Data Visualization](/notes/01-foundation-bsma1002-stats-1-week03-05-data-visualization) — visualizing relationships - **Week 7 (Probability):** Conditional probability from contingency tables - **BSMA1004 (Stats 2):** Chi-square test of independence - **BSMS2002 (Business Analytics):** Market segmentation with contingency tables [Join Discord](https://discord.gg/gE2m4Qrdqv) [Previous**Data Visualization**](/notes/01-foundation-bsma1002-stats-1-week03-05-data-visualization)[Next**Correlation**](/notes/01-foundation-bsma1002-stats-1-week04-07-correlation)
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.