Track 10 · Comparing Groups and Categorical Data · Lesson 1 of 11

Contingency tables

Build a contingency table for two categorical variables and read its joint, marginal and conditional distributions correctly.

25 minIntermediate7 graded exercisesFree

A marketing team asks whether customers who sign up through email go on to buy more often than those from display ads. A hospital board asks whether recovery differs between its two sites. Both questions are about two categorical variables measured on the same people, and both are answered — or misanswered — by the percentages you take from one table.

A contingency table (or cross-tabulation) counts how many units fall into each combination of two categorical variables. With rr categories for the row variable and cc for the column variable it has r×cr \times c cells. The row and column totals in the margins are called the marginal totals, and the bottom-right corner holds the grand total NN.

Every unit must be counted in exactly one cell: the categories of each variable must be exhaustive and mutually exclusive, and each unit (patient, customer, respondent) appears once.

RecoveredNot recoveredTotal
Hospital A395105500
Hospital B43070500
Total8251751000
Recovery by hospital in the (synthetic) hospital_outcomes dataset, 1000 patients.

From one table you can read three different kinds of distribution. They answer different questions, and confusing them is the most common error in reading cross-tabulations.

p^ij=nijN⏟jointp^i⋅=ni⋅N⏟marginalP^(col j∣row i)=nijni⋅⏟conditional (row %)\underbrace{\hat p_{ij} = \frac{n_{ij}}{N}}_{\text{joint}} \qquad \underbrace{\hat p_{i\cdot} = \frac{n_{i\cdot}}{N}}_{\text{marginal}} \qquad \underbrace{\hat P(\text{col } j \given \text{row } i) = \frac{n_{ij}}{n_{i\cdot}}}_{\text{conditional (row \%)}}
Joint, marginal and conditional proportions
nijn_{ij}
the count in row ii, column jj
ni⋅n_{i\cdot}
the total of row ii (the dot means “summed over the columns”); n⋅jn_{\cdot j} is the total of column jj
NN
the grand total
P^(col j∣row i)\hat P(\text{col } j \given \text{row } i)
the proportion of row ii that falls in column jj — a row percentage. The column percentage nij/n⋅jn_{ij}/n_{\cdot j} conditions on the column instead

Worked example

Reading the hospital table three ways

Use the hospital table above.

  1. Joint. 395 of 1000 patients were treated at Hospital A and recovered: 395/1000=0.395395/1000 = 0.395.

  2. Marginal. Overall 825 of 1000 recovered: 0.8250.825. This ignores the hospital entirely.

  3. Conditional on hospital (row %). Hospital A: 395/500=79.0%395/500 = 79.0\% recovered; Hospital B: 430/500=86.0%430/500 = 86.0\%. These are the numbers that compare the hospitals.

  4. Conditional on outcome (column %). Of the 825 patients who recovered, 395/825=47.9%395/825 = 47.9\% were at Hospital A. This answers a different question (“where did recovered patients come from?”) and mostly reflects how many patients each hospital treats.

To compare groups, condition on the group: compute percentages within each hospital, so groups of different sizes become comparable.

A third variable can change the story

Split the same patients by severity on admission. Among severe cases Hospital A's recovery rate is 75.0% against 70.0% at B; among mild cases it is 95.0% against 90.0%. Hospital A does better within both severity groups, yet worse overall, because it treats most of the severe cases. This reversal is Simpson's paradox: a two-way table summarises the data honestly, but the comparison it suggests can be confounded by a variable it leaves out.

SeverityHospital AHospital B
Mild95.0% of 10090.0% of 400
Severe75.0% of 40070.0% of 100
All patients79.0% of 50086.0% of 500
Conditional recovery rates (row %) by hospital within each severity group.

Tables from aggregated counts

Data often arrive already counted. Each row of campaign_funnel is one channel-week with counts of signups and of purchases by those signups. To build the table channel × purchased (yes/no) among signups, add the counts over all weeks: yes = purchases, no = signups − purchases. Email gives 30.7% purchasing (3,903 of 12,708 signups) and Display 12.9% (617 of 4,768).

Dataset

Marketing funnel by channel

130 rows · one row = one channel-week (aggregated counts) · synthetic data

Week startChannelClicks (clicks)Signups (signups)Purchases (orders)
2024-01-07Search6282714158
2024-01-07Social566647356
2024-01-07Email2991450130
2024-01-07Display261616222
2024-01-07Affiliate168823967
2024-01-14Search6962909203

One row per channel-week. Pool the counts over the 26 weeks before computing rates.

Quick check

A survey table has department in the rows and work mode (onsite, hybrid, remote) in the columns. Which percentages show whether work arrangements differ between departments?

Exercises

The lesson is complete when every exercise is answered correctly. Hints are free; viewing a worked solution before solving means the exercise won't count towards mastery.

0 of 7 exercises correct (not saved — sign in to keep your progress)

Exercise 1 · Guided, difficulty 1 of 3

Joint, marginal and conditional

Not started

A bank surveyed customers about the channel they use most:

Age groupAppWebsitePhoneTotal
Under 35312581137
35 and over186642126
Total4991123263

Give each proportion to 3 decimal places.

  1. The joint proportion of all customers who are under 35 and prefer the website.
  2. The marginal proportion of all customers who prefer the phone.
  3. The conditional proportion of under-35 customers who prefer the website.
  4. The proportion of website users who are under 35.

Exercise 2 · Interpretation, difficulty 1 of 3

Which percentage answers the question?

Not started

A retailer cross-tabulates customer type (new, returning, loyalty; rows) against payment method (card, cash, wallet, buy-now-pay-later; columns). The manager asks: “Do loyalty customers pay by card more often than new customers?” Which numbers should you compare?

Choose one

Exercise 3 · Application, difficulty 1 of 3

Recovery rate at each hospital

Not started

In hospital_outcomes, cross-tabulate hospital against recovered. What percentage of each hospital's patients recovered? Give both to 1 decimal place.

Data: hospital_outcomes · Open in the lab to explore and calculate.

Exercise 4 · Application, difficulty 2 of 3

Recovery among severe cases

Not started

Now keep only the patients with severity = severe. What percentage recovered at each hospital? Give both to 1 decimal place.

Data: hospital_outcomes (severity = severe) · Open in the lab to explore and calculate.

Exercise 5 · Application, difficulty 2 of 3

Purchase rate by channel from weekly counts

Not started

In campaign_funnel, what percentage of signups made a purchase over the whole 26 weeks, for Email and for Display? Pool the counts over weeks. Give both to 2 decimal places.

Data: campaign_funnel · Open in the lab to explore and calculate.

Exercise 6 · Practice, difficulty 1 of 3

Joint, marginal or conditional?

Not started

A table cross-classifies employees by department (rows) and work mode (columns). Classify the calculation each question needs.

What share of all employees work remotely?
What share of Finance staff work remotely?
What share of remote workers are in Finance?
What share of all employees are Finance staff who work remotely?
Is hybrid working more common in Technology than in Operations?

Exercise 7 · Interpretation, difficulty 2 of 3

Describe work arrangements by department

Not started

The engagement_survey table of department × work mode (respondents) is:

DepartmentOnsiteHybridRemoteTotal
Operations136449189
Customer Service124356165
Sales327734143
Technology476638151
Finance24391982
Human Resources19191048
Total382280116778

In 2–3 sentences, describe how work arrangements differ between departments. Use percentages that make departments of different sizes comparable, and be careful about what the table can and cannot tell you.

0 words

What you can now do

  • Build a contingency table from raw categorical data or from aggregated counts
  • Compute joint, marginal and conditional proportions and say which question each answers
  • Compare groups with percentages conditioned on the group
  • Recognise that a third variable can reverse a two-way comparison (Simpson's paradox)