Two categorical variables
Data organization
When you’re studying two categorical variables, you’ll often want to know whether they’re associated. In other words, does knowing one variable give you information about the other?
A common way to organize this kind of data is with a two-way table (also called a contingency table). A two-way table shows the frequency (count) for every combination of categories. It’s also helpful to compute row totals and column totals, since those totals let you find proportions (relative frequencies).
Example:
Some people were surveyed and were asked about their hair color and whether or not they have at least one pet. Here are the results:
Brown Hair Black Hair Blonde Hair Total Has at least one pet Does not have a pet Total
Before answering questions about the results, fill in the totals:
- Row totals: add across each row to find the total number of people in each pet-status group.
- Column totals: add down each column to find the total number of people in each hair-color group.
- Grand total: add either all row totals or all column totals to find the total number of people surveyed.
The “total” row and “total” column should give the same grand total if everything is added correctly.
| Brown Hair | Black Hair | Blonde Hair | Total | |
|---|---|---|---|---|
| Has at least one pet | ||||
| Does not have a pet | ||||
| Total | ( or ) |
Practice problem
Part 1.
1. How many people were surveyed in total?
You can find this by adding the column totals (brown hair + black hair + blonde hair) or by adding the row totals (total with at least one pet + total with no pet).
Solution to part 1:
people were surveyed in total.
Part 2.
2. How many people surveyed had black hair?
Look at the black-hair column total: .
Solution to part 2:
people surveyed had black hair.
Part 3.
3. What is the relative frequency of people with black hair?
Relative frequency means “what fraction of the whole group is in this category.” Since people have black hair out of total, the relative frequency is .
Solution to part 3:
Part 4.
4. What is the frequency of people who have at least one pet?
Look at the row total for “has at least one pet”: .
Solution to part 4:
The frequency is .
Part 5.
5. What is the relative frequency of people who have at least one pet?
Since people have at least one pet out of total, the relative frequency is .
Solution to part 5:
Part 6.
6. What is the conditional relative frequency of people with blonde hair given that they have at least one pet?
Conditional relative frequency means “within a specific group, what fraction is in a particular category?” Here, restrict attention to people who have at least one pet — a total of people. Among them, have blonde hair, so the conditional relative frequency is .
Solution to part 6:
Part 7.
7. What is the conditional relative frequency of people who do not have any pets given that they have brown hair?
Restrict attention to people with brown hair — a total of people. Among them, do not have a pet, so the conditional relative frequency is .
Solution to part 7:
Part 8.
8. What is the relative frequency of people with brown hair who have at least one pet?
This is a joint relative frequency: the fraction of the entire sample that has both brown hair and at least one pet. The count in that cell is , and the total surveyed is , so the relative frequency is .
Solution to part 8:
Displaying the data
This data can also be displayed using a bar chart or a segmented bar chart. Both charts show the same data from the two-way table, just in a visual format.
In the bar chart below, each hair color has two bars side by side — one for people who have at least one pet, and one for people who don’t. This makes it easy to compare pet ownership within each hair color group.
In the segmented (stacked) bar chart below, each hair color has a single bar divided into two segments — one for pet owners and one for non-pet owners. The full height of each bar represents the total count for that hair color group. This format makes it easier to see how the two categories stack up within each group, and to compare the overall sizes of each hair color group.