Bivariate Categorical Data

⚡ Key Takeaways

Bivariate categorical data records two categorical variables for each individual, organized in a two-way (contingency) table. The key tools are joint, marginal, and conditional relative frequencies, which reveal whether the two variables are associated or independent. Used in surveys, medicine, market research, and the social sciences whenever two qualities (not numbers) are compared.

💡 The Core Insight

A single categorical variable tells you how a group is distributed. Two categorical variables let you ask the far richer question: does knowing one variable change the distribution of the other? If the conditional distributions of variable B are the same across every category of A, the variables are independent — A tells you nothing about B. If those distributions differ, the variables are associated. Everything in this topic is machinery for measuring and detecting that difference.

Formal Statement

DefinitionCategorical Variable

A categorical variable assigns each individual to exactly one of a finite set of categories (levels). Categories may be:

  • Nominal — unordered labels (e.g. blood type: A, B, AB, O).
  • Ordinal — labels with a natural order (e.g. rating: poor, fair, good).

A bivariate categorical dataset records two such variables, XX and YY, for each of nn individuals.

DefinitionTwo-Way (Contingency) Table

Let XX have categories {a1,…,ar}\{a_1,\dots,a_r\} (the rr rows) and YY have categories {b1,…,bc}\{b_1,\dots,b_c\} (the cc columns). The cell count nijn_{ij} is the number of individuals with X=aiX=a_i and Y=bjY=b_j. The table of all nijn_{ij} is the two-way table. Define the marginal totals and grand total:

1
Ri=∑j=1cnij,Cj=∑i=1rnij,n=∑i=1r∑j=1cnij\quad R_i = \sum_{j=1}^{c} n_{ij}, \qquad C_j = \sum_{i=1}^{r} n_{ij}, \qquad n = \sum_{i=1}^{r}\sum_{j=1}^{c} n_{ij}

Derivation hint: RiR_i sums a row across all columns, CjC_j sums a column across all rows; summing either set of margins recovers the grand total nn.

DefinitionJoint, Marginal, and Conditional Relative Frequencies

From the counts we form three families of proportions.

Joint relative frequency — fraction of all individuals in cell (i,j)(i,j):

2
pij=nijn\quad p_{ij} = \frac{n_{ij}}{n}

Derivation hint: divide each cell by the grand total; all rcrc joint frequencies sum to 1.

Marginal relative frequency — fraction in a single row or column overall:

3
pi⋅=Rin,p⋅j=Cjn\quad p_{i\cdot} = \frac{R_i}{n}, \qquad p_{\cdot j} = \frac{C_j}{n}

Derivation hint: divide a marginal total by nn; equivalently pi⋅=∑jpijp_{i\cdot}=\sum_j p_{ij}, summing a row of joint frequencies.

Conditional relative frequency — fraction within a fixed row (or column):

4
pj∣i=nijRi,pi∣j=nijCj\quad p_{j \mid i} = \frac{n_{ij}}{R_i}, \qquad p_{i \mid j} = \frac{n_{ij}}{C_j}

Derivation hint: restrict attention to the subgroup X=aiX=a_i (its total is RiR_i), then take the share in column jj; this rescales one row so it sums to 1.

TheoremIndependence Condition

Two categorical variables XX and YY are independent when every conditional distribution equals the corresponding marginal distribution. Equivalently, the joint frequency factors:

5
pij=pi⋅ p⋅jfor all i,j\quad p_{ij} = p_{i\cdot}\, p_{\cdot j} \quad\text{for all } i,j

In count form, the expected count under independence is:

6
Eij=Ri Cjn\quad E_{ij} = \frac{R_i\, C_j}{n}

Derivation hint: substitute pij=nij/np_{ij}=n_{ij}/n, pi⋅=Ri/np_{i\cdot}=R_i/n, p⋅j=Cj/np_{\cdot j}=C_j/n into [5] and solve nij=RiCj/nn_{ij}=R_i C_j/n for the expected count.

Why It Holds / Derivation

ProofFrom Independence to Expected Counts

We show that [5] forces [6]. 1. Start from the factorization. Independence means pij=pi⋅ p⋅jp_{ij}=p_{i\cdot}\,p_{\cdot j} — the probability of landing in cell (i,j)(i,j) is the product of being in row ii and being in column jj. 2. Convert proportions to counts. Multiply both sides by nn:

n pij=n pi⋅ p⋅j.n\,p_{ij} = n\,p_{i\cdot}\,p_{\cdot j}.

The left side is the cell count we would expect, call it EijE_{ij}. 3. Substitute the marginals. Using pi⋅=Ri/np_{i\cdot}=R_i/n and p⋅j=Cj/np_{\cdot j}=C_j/n:

Eij=n⋅Rin⋅Cjn=RiCjn.E_{ij} = n\cdot \frac{R_i}{n}\cdot\frac{C_j}{n} = \frac{R_i C_j}{n}.

4. Interpret. EijE_{ij} is the count we'd see if the row and column variables carried no information about each other. Comparing the observed nijn_{ij} to EijE_{ij} measures association: large gaps signal a relationship. ■\blacksquare

∎
mathematician · Karl Pearson | 1857–1936

Karl Pearson, the founder of modern mathematical statistics, introduced the chi-squared test in 1900 precisely to compare observed counts in a contingency table to the expected counts of [6]. He also coined the term "contingency" for these tables (1904) and built much of the vocabulary — correlation, standard deviation, histogram — that students still use. His student-rival G. Udny Yule developed measures of association for 2×22\times 2 tables in the same era.

Building Intuition

🧠 Intuition

Picture a class of 100 students sorted into a grid: rows for wears glasses / doesn't, columns for plays a sport / doesn't. The joint frequencies are the four cell shares — the bird's-eye view. The marginals are the edge totals — what you'd know if you only asked one question. The conditional frequencies are what you get by walking into just the "glasses" row and asking of these people, what fraction play a sport? Association is simply this: if the glasses-wearers play sports at a different rate than the non-glasses-wearers, the two variables are linked. Independence is the boring case where every row looks like a shrunken copy of the overall column distribution. The whole subject is the discipline of conditioning — narrowing to a subgroup — and then comparing.

Figure
A two-way table with marginals; conditioning means reading along one row.

```svg

Glasses × Sport (n = 100) Sport No Sport Total Glasses None 12 18 28 42 30 70 40 60 100 Condition on "Glasses": 12/30 = 40% play sport Marginal: 40/100 = 40% play sport overall Equal rates here ⇒ no association in this row

```

Worked Examples

ExampleAssociation in a Survey

Given. 200 people are asked whether they prefer coffee or tea, cross-classified by age group.

CoffeeTeaRow total
Under 30503080
30 or over6060120
Col total11090200

Question. Are age and beverage preference associated?

Steps. 1. Conditional distribution for Under 30: coffee =50/80=0.625=50/80 = 0.625, tea =30/80=0.375=30/80=0.375. 2. Conditional distribution for 30 or over: coffee =60/120=0.500=60/120 = 0.500, tea =60/120=0.500=60/120 = 0.500. 3. Compare: under-30s prefer coffee 62.5% of the time, older group only 50%. The conditional distributions differ. 4. Check against marginal coffee rate: 110/200=0.55110/200 = 0.55. Neither row matches it.

Answer.

The variables are associated: younger people lean toward coffee (62.5% vs 50%).\boxed{\text{The variables are associated: younger people lean toward coffee (62.5\% vs 50\%).}}
ExampleTesting Independence via Expected Counts

Given. The same table. Compute the expected count of Under 30 & Coffee under independence, then the contribution to the chi-squared statistic.

Steps. 1. Marginals: R1=80R_1 = 80 (Under 30), C1=110C_1 = 110 (Coffee), n=200n=200. 2. Expected count from [6]:

E11=R1C1n=80×110200=8800200=44.E_{11} = \frac{R_1 C_1}{n} = \frac{80\times 110}{200} = \frac{8800}{200} = 44.

3. Observed was n11=50n_{11}=50, so the deviation is 50−44=650 - 44 = 6. 4. Chi-squared contribution of this cell, (O−E)2E=(50−44)244=3644≈0.818.\dfrac{(O-E)^2}{E} = \dfrac{(50-44)^2}{44} = \dfrac{36}{44} \approx 0.818.

Answer.

E11=44 people;cell χ2-contribution≈0.82 (dimensionless).\boxed{E_{11} = 44 \text{ people};\quad \text{cell } \chi^2\text{-contribution} \approx 0.82 \text{ (dimensionless)}.}

The observed count exceeds the independence prediction, consistent with the association found in Example 1.

Common Misconceptions

⚠️ Common mistake

Conditional ≠ joint ≠ marginal — and the denominator is everything.

  • A joint frequency divides by the grand total nn.
  • A conditional frequency divides by a single row or column total.
  • Saying "40% of glasses-wearers play sport" (conditional, ÷Ri\div R_i) is utterly different from "12% of all students wear glasses and play sport" (joint, ÷n\div n).

Also: pj∣ip_{j\mid i} (column jj given row ii) and pi∣jp_{i\mid j} (row ii given column jj) are not interchangeable. The fraction of doctors who are women is not the fraction of women who are doctors. Confusing these two directions is one of the most common — and consequential — statistical errors.

CounterexampleSimpson's Paradox

Association can reverse when groups are combined. Consider two hospitals' survival rates: Severe patients: Hospital A saves 30/100 = 30%, Hospital B saves 10/50 = 20%. A is better. Mild patients: Hospital A saves 100/100 = 100%(say 90/100=90%), Hospital B saves 180/200 = 90%. Tie or A leads. Yet combined, Hospital B can show a higher overall survival rate, because B treats mostly mild patients while A treats mostly severe ones. A is superior within each category but inferior in the lumped totals. This proves that a marginal (overall) comparison can flatly contradict every conditional comparison — a vivid warning against ignoring a third variable. See Simpson's Paradox.

Connections

connection

Bivariate categorical data sits at a crossroads of statistics:

  • It extends Univariate Categorical Data (one variable, a single bar chart) into a relational setting.
  • The independence check of [6] is the engine of the Chi-Squared Test of Independence, which turns the cell deviations into a single test statistic and p-value.
  • The conditional frequencies of [4] are the empirical face of Conditional Probability; equation [5] is exactly the multiplication rule for independent events.
  • For two numerical variables, the analogous study is Scatterplots and Correlation — association measured by direction and strength rather than by table comparison.
  • Measures like the odds ratio and relative risk quantify 2×22\times 2 association, linking to Risk and Odds.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- 1865 — Gregor Mendel publishes pea-plant ratios, implicitly cross-classifying traits in tables.
- 1888 — Francis Galton formalizes "co-relation," priming the idea of measuring association.
- 1900 — Karl Pearson introduces the chi-squared test, comparing observed to expected cell counts.
- 1904 — Pearson coins the term contingency table and proposes the contingency coefficient.
- 1900s–1910s — G. Udny Yule develops the odds ratio and QQ coefficient for 2×22\times 2 tables.
- 1922 — R. A. Fisher corrects the degrees of freedom for contingency tables, refining Pearson's test.
- 1934 — Fisher devises the exact test for small 2×22\times 2 tables (the famous "lady tasting tea").
- 1951 — E. H. Simpson formally describes the reversal paradox now bearing his name.
- 1970s onward — Log-linear models (Bishop, Fienberg, Holland) generalize two-way tables to high-dimensional categorical analysis.

Subtleties & Limits

open-problem

Detecting association is easy; explaining it is hard. A two-way table can show a strong, statistically significant association that is entirely due to a hidden confounding variable — Simpson's Paradox is the dramatic case, but subtler confounding pervades observational data. There is no purely mathematical rule that tells you which variables to condition on; that requires causal reasoning beyond the table itself. Modern causal inference (Pearl's directed graphs, potential-outcomes frameworks) attempts to formalize when conditioning reveals versus distorts a true relationship, but choosing the right adjustment set in real studies remains a deep, partly unsolved problem. Likewise, chi-squared tests assume adequate expected counts and independent sampling — conditions easily violated in practice.

See Also

Univariate Categorical Data · Conditional Probability · Chi-Squared Test of Independence · Simpson's Paradox · Scatterplots and Correlation · Risk and Odds