Contingency Tables — Analyzing Two Categorical Variables
2968 words
15 min read
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
- Categorical Data (02-categorical-frequency) — frequency tables for single variables
- Data Types & Scales (01-data-types-scales) — understanding nominal vs ordinal
- Basic arithmetic: counting, division, percentages
📖 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 A | Product B | Row Total | |
|---|---|---|---|
| Male | 40 | 60 | 100 |
| Female | 70 | 30 | 100 |
| Column Total | 110 | 90 | 200 |
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 A | Product B | Total | |
|---|---|---|---|
| Male | 40/200 = 0.20 | 60/200 = 0.30 | 0.50 |
| Female | 70/200 = 0.35 | 30/200 = 0.15 | 0.50 |
| Total | 0.55 | 0.45 | 1.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 A | Product B | Total | |
|---|---|---|---|
| Male | 55 | 45 | 100 |
| Female | 55 | 45 | 100 |
| Total | 110 | 90 | 200 |
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.
| Hospital | Small Stones Success Rate | Large Stones Success Rate | Overall Success Rate |
|---|---|---|---|
| A | 93% (81/87) | 73% (192/263) | 78% (273/350) |
| B | 87% (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):
Odds Ratio (for case-control studies):
Example: Smoking and lung cancer
| Lung Cancer | No Lung Cancer | |
|---|---|---|
| Smoker | 60 | 40 |
| Non-smoker | 15 | 85 |
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 Health | Poor Health | Row Total | |
|---|---|---|---|
| Regular | 40 | 10 | 50 |
| Sometimes | 35 | 15 | 50 |
| Never | 20 | 30 | 50 |
| Column Total | 95 | 55 | 150 |
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 Eyes | Blue Eyes | Total | |
|---|---|---|---|
| Black Hair | 80 | 40 | 120 |
| Brown Hair | 60 | 30 | 90 |
| Blonde Hair | 40 | 50 | 90 |
| Total | 180 | 120 | 300 |
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 |
|---|---|---|
| Black | 80/120 = 66.7% | 40/120 = 33.3% |
| Brown | 60/90 = 66.7% | 30/90 = 33.3% |
| Blonde | 40/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):
| Admitted | Rejected | Total | Admit Rate | |
|---|---|---|---|---|
| Female | 80 | 20 | 100 | 80% |
| Male | 40 | 10 | 50 | 80% |
Program B (Engineering):
| Admitted | Rejected | Total | Admit Rate | |
|---|---|---|---|---|
| Female | 10 | 40 | 50 | 20% |
| Male | 30 | 120 | 150 | 20% |
Overall Combined:
| Admitted | Rejected | Total | Admit Rate | |
|---|---|---|---|---|
| Female | 90 | 60 | 150 | 60% |
| Male | 70 | 130 | 200 | 35% |
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
| Concept | Calculation | Interpretation |
|---|---|---|
| Cell frequency | Count in each combination | Observed joint frequency |
| Row total | Sum of cells in a row | Marginal frequency of row variable |
| Column total | Sum of cells in a column | Marginal frequency of column variable |
| Grand total | Sum of all cells | Total sample size |
| Joint proportion | Cell / Grand total | P(Category A and Category B) |
| Marginal proportion | Row or column total / Grand total | P(Category A) alone |
| Conditional proportion | Cell / Row total (or cell / Column total) | P(B |
| Independence | All conditional distributions are identical | No association between variables |
| Relative Risk | Risk exposed / Risk unexposed | How many times more likely |
| Odds Ratio | Odds exposed / Odds unexposed | Strength 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<details> <strong>Solution</strong>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.Known:
Women: 120, Men: 80 Online: 90, In-store: 110 Women who prefer online: 60
| Online | In-Store | Total | |
|---|---|---|---|
| Women | 60 | 120-60 = 60 | 120 |
| Men | 90-60 = 30 | 80-30 = 50 | 80 |
| Total | 90 | 110 | 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** > >Check: 60+60+30+50 = 200 ✅
| Passed | Failed | Total | |
|---|---|---|---|
| Studied | 75 | 25 | 100 |
| Didn't Study | 15 | 35 | 50 |
| Total | 90 | 60 | 150 |
</details> > **Q4: Joint Probabilities** > ><details> <strong>Solution</strong>Are studying and passing independent?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!)
| Coffee | Tea | Total | |
|---|---|---|---|
| Morning | 30 | 20 | 50 |
| Afternoon | 15 | 35 | 50 |
| Total | 45 | 55 | 100 |
</details> > **Q5: Simpson's Paradox** > > A university has two colleges. The combined admission data shows: > ><details> <strong>Solution</strong>Find the joint probabilities for all four cells.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 ✅
| Admit Rate | |
|---|---|
| Women | 45% |
| Men | 55% |
</details> > **Q6: Relative Risk** > ><details> <strong>Solution</strong>But within each college, admit rates are equal (50% for both genders). Explain how this is possible.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% overallThe overall disparity is entirely due to application patterns, not discrimination.
| Disease | No Disease | |
|---|---|---|
| Exposed | 50 | 150 |
| Unexposed | 10 | 190 |
</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:** > ><details> <strong>Solution</strong>Compute the relative risk. Interpret.Risk in exposed: 50/(50+150) = 50/200 = 0.25 Risk in unexposed: 10/(10+190) = 10/200 = 0.05Relative Risk: 0.25/0.05 = 5.0Interpretation: Exposed individuals are 5 times more likely to develop the disease compared to unexposed individuals.RR=5.0
| Urban | Rural | Total | |
|---|---|---|---|
| Male | 5 | 3 | 8 |
| Female | 8 | 4 | 12 |
| Total | 13 | 7 | 20 |
</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:** > >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.
| Clicked | Not Clicked | Total | |
|---|---|---|---|
| Version A | 80 | 920 | 1000 |
| Version B | 120 | 880 | 1000 |
| Total | 200 | 1800 | 2000 |
b) Among clickers, Version × Purchased:Version A clickers: 80, of which 60% = 48 purchased Version B clickers: 120, of which 40% = 48 purchased
| Purchased | Not Purchased | Total | |
|---|---|---|---|
| Version A | 48 | 32 | 80 |
| Version B | 48 | 72 | 120 |
| Total | 96 | 104 | 200 |
</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)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.