Statistics 🔬 Confidence & Inference

Two-Way Tables & Categorical Association

Cross-classifying two categorical variables and reading association straight off the counts — no p-values yet.

Intro StatisticsCollege intro statistics level
Two-Way Tables & Categorical Association — illustration
Illustrative image (AI-generated).
💡
The big idea: Many real questions involve two categorical variables measured on the same individuals — region and opinion, treatment and outcome, major and employment status. A two-way table lays out the counts for every combination and lets you split them into marginal, joint, and conditional distributions. Comparing conditional distributions across groups is how you judge association from a table, entirely by counting and dividing. This lesson is descriptive only — it is the prerequisite vocabulary for the chi-square test, which turns this comparison into an actual hypothesis test.
🎯 By the end, you'll be able to
  • Read a two-way table and identify its marginal, joint, and conditional distributions
  • Compute conditional distributions and compare them across groups to judge association
  • Compute the expected count for a cell under independence
  • Distinguish association from causation, and explain why conditional distributions — not raw counts — reveal independence
  • Recognize Simpson's paradox and explain why it happens
📎 You should already know
  • Categorical data and bar charts
  • Percentages and proportions
  • Basic probability notation, P(A) and P(A and B)

Two variables, one table

So far you have studied one categorical variable at a time — favorite color, yes or no, pass or fail. Many real questions involve two categorical variables measured on the same individuals: smoking status and lung disease, treatment and outcome, region and opinion. A two-way table (also called a contingency table) lays out the counts for every combination of the two variables' categories.

This lesson is entirely descriptive: everything here is counting and dividing, with no p-values and no hypothesis test. It builds the vocabulary — marginal, joint, and conditional distributions — that the chi-square test in the next lesson turns into an actual inference.

Marginal, joint, and conditional distributions

Take a survey of 100 residents, cross-classified by region and opinion on a new policy:

  • North: 30 favor, 20 oppose (row total 50)
  • South: 15 favor, 35 oppose (row total 50)
  • Column totals: 45 favor, 55 oppose (grand total 100)

Three different distributions live inside this one table:

  • The marginal distribution of a single variable is its row or column totals, ignoring the other variable entirely — e.g. 45 of the 100 residents favor the policy, however their region breaks down.
  • The joint distribution is each cell count divided by the grand total — e.g. the joint probability of being from the North and favoring the policy is 30/100.
  • The conditional distribution restricts to one row or column and divides by that total — e.g. among Northerners only, the conditional proportion favoring is 30/50.
⚠️ Naming the distributions
WRONG: calling a row or column total a "joint" distribution. RIGHT: MARGINAL = the totals — one variable, ignoring the other. JOINT = cell count divided by the grand total — both variables at once. CONDITIONAL = cell divided by ITS OWN row or column total — one variable GIVEN a level of the other.

Judging association from conditional distributions

To ask whether region and opinion are related, do not stare at the raw counts — compare the conditional distributions across groups. If North's split between favor and oppose looks the same as South's split, the variables move independently of each other. If the splits differ, that difference is association.

A segmented bar chart draws each group's conditional distribution as one full-height bar, divided into segments by proportion. Two bars with matching segment heights say "no association"; bars that lean differently say the opposite.

⚠️ Independence means matching conditional distributions
WRONG: "The variables are independent because the raw counts look similar." RIGHT: two categorical variables are independent exactly when the conditional distributions are identical across groups — equivalently, P(A and B) = P(A) × P(B) for every cell. Judge independence from conditional distributions, not from raw counts, which can look close even when the underlying proportions differ a lot.
🎮 Interactive: marginal, joint, and conditional distributions LIVE
Edit the four cell counts a, b, c, d of a 2x2 table and watch the marginal totals, the joint distribution (each cell divided by the grand total), and the conditional distribution of each row update live, alongside a segmented bar for each row. Change the counts to see when the two rows' segmented bars match — no association — versus lean apart — association.
\[ E_{ij} = \frac{(\text{row}_i)(\text{col}_j)}{n} \]
The count expected in cell (i, j) if the two variables were independent: its row total times its column total, divided by the grand total n.
⚠️ Computing an expected count
WRONG: "Expected count = row percent times observed count." RIGHT: expected count = (row total)(column total) divided by the grand total — the count expected in that cell if the two variables were independent. It uses the table's totals, not any single observed cell.
📝 Worked example: Using the region-and-opinion table above (North: 30 favor, 20 oppose; South: 15 favor, 35 oppose; row totals 50 and 50; column totals 45 favor and 55 oppose; n = 100), find all four expected counts under independence, then compare the conditional distribution for the North row to the marginal distribution for "favor".
  1. Expected count, North and favor: \( E = (50)(45)/100 = 22.5 \).
  2. Expected count, North and oppose: \( E = (50)(55)/100 = 27.5 \).
  3. Expected count, South and favor: \( E = (50)(45)/100 = 22.5 \).
  4. Expected count, South and oppose: \( E = (50)(55)/100 = 27.5 \).
  5. Joint: \( P(\text{North and favor}) = 30/100 = 0.30 \).
  6. Marginals: \( P(\text{North}) = 50/100 = 0.50 \). \( P(\text{favor}) = 45/100 = 0.45 \).
  7. Conditional: \( P(\text{favor} \mid \text{North}) = 30/50 = 0.60 \).
✓ 0.60 does not equal 0.45: the conditional proportion favoring, given North, is noticeably higher than the overall (marginal) proportion favoring. That gap is evidence of association between region and opinion — description only, no p-value has been computed yet.
⚠️ Association is not causation
WRONG: "Association in the table proves one variable causes the other." RIGHT: association is a statement about the data; a causal claim requires design — specifically, random assignment. This table came from a survey, not an experiment, so region "causing" the opinion split is not something the counts alone can establish.
⚠️ Simpson's paradox is real, not an error
WRONG: "Simpson's paradox is a computational mistake." RIGHT: Simpson's paradox is real: an association present in every subgroup can reverse when the subgroups are combined, because a lurking variable is distributed unevenly across them. It is a fact about how weighted averages combine, not an arithmetic error.
📝 Worked example: Two treatments, A and B, are each tried at two hospitals with very different patient mixes. Hospital 1 (easier cases): A succeeds 9 of 10 times (90%); B succeeds 85 of 100 times (85%). Hospital 2 (harder cases): A succeeds 30 of 100 times (30%); B succeeds 2 of 10 times (20%). Combine the two hospitals and compare A to B overall.
  1. Hospital 1: A = 9/10 = 0.90. B = 85/100 = 0.85. A beats B here.
  2. Hospital 2: A = 30/100 = 0.30. B = 2/10 = 0.20. A beats B here too.
  3. Combined A: \( (9 + 30)/(10 + 100) = 39/110 \approx 0.355 \).
  4. Combined B: \( (85 + 2)/(100 + 10) = 87/110 \approx 0.791 \).
✓ A wins in both hospitals separately, yet B wins overall — 79.1% versus 35.5%. The reversal happens because A was mostly given at the harder hospital and B mostly at the easier one: hospital is a lurking variable, unevenly split between the treatments. Nothing was computed wrong; this is Simpson's paradox.

Check your understanding

1. In the region-and-opinion table (North: 30 favor / 20 oppose; South: 15 favor / 35 oppose), which value is the JOINT probability of North and favor?
Joint = cell count divided by the grand total: 30/100 = 0.30. The others are a marginal (0.45 or 0.50) or a conditional (0.60).
2. Which pair of numbers is the CONDITIONAL distribution of opinion given South?
Conditional on South means dividing by South's own row total (50): 15/50 favor, 35/50 oppose.
3. A hospital's two-way table shows drug use and recovery are associated, based on observational (non-randomized) records. What can be concluded?
Association is a fact about the data. A causal claim needs random assignment, which an observational record does not have.
4. An association between treatment and recovery is positive within every individual hospital, but negative when the hospitals are combined into one table. What does this show?
This is the signature of Simpson's paradox: combining subgroups with very different weights can reverse the direction of an association, with no error involved.
✅ Key takeaways
  • A two-way (contingency) table cross-classifies two categorical variables measured on the same individuals.
  • MARGINAL = row or column totals (one variable alone). JOINT = cell divided by the grand total (both variables at once). CONDITIONAL = cell divided by its own row or column total (one variable given a level of the other).
  • Judge association by comparing conditional distributions across groups, not by eyeballing raw counts.
  • Expected count under independence: E = (row total)(column total) / n.
  • Association is a property of the data; a causal claim needs random assignment.
  • Simpson's paradox: an association present in every subgroup can reverse when the subgroups are combined, if a lurking variable is unevenly split across them.