01 / 21

Statistics for Two
Categorical Variables

Learn to organize, display, and analyze the relationship between two categorical variables using two-way tables, conditional distributions, and tests of association.

AP Statistics

What Are Categorical Variables?

A categorical variable places each individual into one of several groups or categories. Unlike quantitative variables, you cannot perform arithmetic on categories: you count how many individuals fall into each category.

Examples
Gender, political party, grade level, favorite color, yes/no responses
Key Idea
When we have two categorical variables, we ask: is there a relationship between them?

The Two-Way Table

A two-way table (contingency table) displays counts for each combination of two categorical variables. Rows represent one variable, columns represent the other.

Prefers Online Prefers In-Person Total
Freshman 120 80 200
Senior 90 110 200
Total 210 190 400

Marginal vs. Conditional Distributions

Marginal Distribution
The distribution of one variable alone: totals in the margins. Example: \(\frac{210}{400} = 52.5\%\) prefer online overall.
Conditional Distribution
The distribution of one variable given a specific value of the other. Example: \(\frac{120}{200} = 60\%\) of freshmen prefer online.

Visualizing with Segmented Bar Charts

A segmented (stacked) bar chart shows conditional distributions visually. Each bar represents one category of the explanatory variable, and segments within the bar show the proportions of the response variable.

0% 25% 50% 75% 100% 60% 40% 45% 55% Freshman Senior Online In-Person

The Expected Count Formula

If there is no association between two categorical variables, we can compute the count we'd expect in each cell under independence:

This formula distributes the marginal totals proportionally across all cells. If observed counts differ substantially from expected counts, we have evidence of association.

The Chi-Square Test Statistic

The chi-square test for independence measures how far observed counts are from expected counts across all cells:

Degrees of Freedom
\(\text{df} = (r - 1)(c - 1)\) where \(r\) = number of rows and \(c\) = number of columns.
Large \(\chi^2\)
A large test statistic means observed counts deviate greatly from expected: evidence of association.

Independence vs. Homogeneity

Both tests use the same \(\chi^2\) formula, but they answer different questions depending on how data were collected.

Test of Independence Test of Homogeneity
Sampling One sample, both variables recorded Separate samples from each population
Question Are the two variables independent? Is the distribution the same across groups?
Example Survey 400 students: is grade related to preference? Survey 200 freshmen and 200 seniors separately: same preferences?
Formula \(\chi^2 = \sum \frac{(O-E)^2}{E}\) \(\chi^2 = \sum \frac{(O-E)^2}{E}\)

Conditions for the \(\chi^2\) Test

1
Random: Data come from a random sample or randomized experiment.
2
10% Condition: If sampling without replacement, the sample size is \(< 10\%\) of the population.
3
Large Counts: All expected counts must be at least 5. That is, \(E_i \geq 5\) for every cell.

How to Perform a \(\chi^2\) Test

1
State hypotheses. \(H_0\): no association (variables are independent). \(H_a\): there is an association.
2
Check conditions. Random, 10%, and all expected counts \(\geq 5\).
3
Compute expected counts using \(E = \frac{\text{row total} \times \text{col total}}{n}\) for every cell.
4
Calculate \(\chi^2\) = \(\sum \frac{(O - E)^2}{E}\) and find \(\text{df} = (r-1)(c-1)\).
5
Find the \(p\)-value using a \(\chi^2\) distribution with the appropriate \(\text{df}\). Compare to \(\alpha\).
6
Conclude in context. If \(p \leq \alpha\), reject \(H_0\): convincing evidence of association.

Example: Expected Counts

A survey of 400 students asked about grade level and study preference:

Online In-Person Total
Freshman 120 80 200
Senior 90 110 200
Total 210 190 400

Example: Computing \(\chi^2\)

Using the observed (O) and expected (E) counts from the previous slide:

df
\((2-1)(2-1) = 1\)
p-value
\(P(\chi^2 \geq 9.022) \approx 0.0027\)

Example: A 3 x 3 Table

A school surveyed 300 students on their grade and favorite extracurricular:

Sports Music Art Total
Freshman 50 30 20 100
Junior 40 35 25 100
Senior 30 25 45 100
Total 120 90 90 300

The \(\chi^2\) Distribution

0 2 4 6 8 10 df = 1 df = 4 df = 8 Test Statistic Value

Common Mistakes

✕ Using Observed for Conditions
The Large Counts condition requires all expected counts \(\geq 5\), not observed counts.
✕ Wrong Denominator
Conditional distributions divide by the row or column total, not the grand total. Marginal distributions use the grand total.
✕ Claiming Causation
A significant \(\chi^2\) test shows association, not causation. Only a randomized experiment can establish cause and effect.

Summary of Key Concepts

Concept Definition Key Detail
Two-Way Table Counts for each combination of two categorical variables Rows + columns = margins
Marginal Dist. Distribution of one variable alone (from totals) Divide by grand total
Conditional Dist. Distribution of one variable given a value of the other Divide by row/column total
Expected Count \(\frac{\text{row} \times \text{col}}{n}\) Assumes independence
\(\chi^2\) Statistic \(\sum \frac{(O-E)^2}{E}\) Larger = more evidence
Conditions Random, 10%, Large Counts (\(E \geq 5\)) Check before testing

Question 1 of 5

In a two-way table with row totals of 150 and 250, and column totals of 180 and 220, with a grand total of 400, what is the expected count for the cell in the first row and first column?

Question 2 of 5

What are the degrees of freedom for a chi-square test on a \(3 \times 4\) two-way table?

Question 3 of 5

If 60 out of 150 women and 45 out of 150 men prefer Brand A, what is the conditional proportion of women who prefer Brand A?

Question 4 of 5

Which condition for a chi-square test requires that all expected counts be at least 5?

Question 5 of 5

A chi-square test yields \(\chi^2 = 15.3\) with \(\text{df} = 4\) and \(p = 0.004\). At \(\alpha = 0.05\), what is the appropriate conclusion?