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.
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.
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
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.
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:
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
How to Perform a \(\chi^2\) Test
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:
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
Common Mistakes
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?