Categorical Predictors and Interactions
Encode categories with dummy variables and a reference group, avoid the dummy variable trap, and model changing effects with interaction terms.
Welcome back!
Step 1 of 13Learn
Region 2 is not twice region 1
A rental company wants to model monthly rent using apartment size and location, which is Rural, Suburb or Downtown. An intern codes location as 1, 2 and 3 and puts it into the regression as a number. The model now assumes that moving from Rural to Suburb changes rent by exactly the same amount as moving from Suburb to Downtown, and that Downtown is somehow "three times" Rural. The order of the codes was arbitrary; coding Downtown as 1 would have given a different model.
Categories such as location, product type, day of the week or treatment group don't have numeric values. To use them in regression, you translate them into indicator variables, also called dummy variables: columns that equal 1 if an observation belongs to a category and 0 otherwise.
This simple trick lets regression handle categories honestly. Each category gets its own estimated difference, with no assumed order or spacing. It also opens the door to comparing groups while controlling for other variables, which is how analysts estimate things like pay gaps adjusted for job title and experience.
In this lesson you'll learn how to set up dummy variables, interpret their coefficients, avoid a classic trap, and model situations where the effect of one variable depends on another: interactions.
Step 2 of 13Learn
Dummy variables and the reference category
For a categorical variable with k categories, create k − 1 dummy variables. The category without a dummy is the reference category (or baseline). For location with Rural as the reference:
- Suburb = 1 if suburban, else 0
- Downtown = 1 if downtown, else 0
A rural apartment has both dummies equal to 0. The fitted model might be:
predicted rent = 400 + 1.2 × sqft + 150 × Suburb + 350 × Downtown
Interpreting the coefficients: each dummy coefficient is the difference from the reference category, holding the other predictors constant. A downtown apartment rents for about $350 more per month than a rural apartment of the same size; a suburban one for about $150 more. The difference between Downtown and Suburb is 350 − 150 = $200.
Why not k dummies? Because for every observation, the k dummies would add up to exactly 1, which duplicates the intercept. The model then has no unique solution, a problem called the dummy variable trap (an extreme case of multicollinearity). Software usually drops one category automatically, but you should know which one, because every dummy coefficient is a comparison with it.
Choose the reference category on purpose: the most common category, a natural baseline such as "control group", or whichever comparison your audience cares about.
Step 3 of 13Practice · Diagram
Three locations, two dummies
Every dummy coefficient is a comparison with which category?
Tap the shape that answers the question, then press Check.
- Location (3 categories)
- Rural: no dummy
The reference category. Its effect lives in the intercept.
- Suburb = 1 / 0
Coefficient: difference from Rural at the same size.
- Downtown = 1 / 0
Coefficient: difference from Rural at the same size.
Step 4 of 13Practice · Work it out
A categorical variable for season has four categories: winter, spring, summer and fall. How many dummy variables should be included in a regression that also has an intercept?
Step 5 of 13Practice · Work it out
Using predicted rent = 400 + 1.2 × sqft + 150 × Suburb + 350 × Downtown (Rural is the reference), what is the predicted monthly rent in dollars of an 800-square-foot downtown apartment?
Step 6 of 13Chart
Predicted rent for a 1,000 sq ft apartment
Step 7 of 13Learn
Interactions: when one effect depends on another
So far every model assumed that each predictor's effect is the same regardless of the other predictors. The extra rent for downtown is $350 whether the apartment is 400 or 1,400 square feet. Often that's unrealistic. Maybe each extra square foot is worth more downtown than in rural areas.
An interaction term lets the slope of one variable depend on another. You add the product of the two predictors:
ŷ = b₀ + b₁x + b₂z + b₃(x × z)
Example: a store models daily sales (in $1,000) from advertising spend (in $1,000) and whether the day is a holiday (1 or 0):
predicted sales = 10 + 2 × ads + 5 × holiday + 1.5 × (ads × holiday)
On regular days (holiday = 0), the model is 10 + 2 × ads: each $1,000 of ads goes with $2,000 more in sales. On holidays (holiday = 1), it is (10 + 5) + (2 + 1.5) × ads = 15 + 3.5 × ads: each $1,000 of ads goes with $3,500 more. The interaction coefficient, 1.5, is the difference in slopes between the two kinds of day.
With interactions, the "main effect" coefficients change meaning. Here b₁ = 2 is the slope of ads on regular days only, not overall, and b₂ = 5 is the holiday difference when ads = 0. Always interpret interaction models by writing out the equation for each group, as above.
Step 8 of 13Practice · Work it out
With predicted sales = 10 + 2 × ads + 5 × holiday + 1.5 × (ads × holiday), what is the predicted sales value (in $1,000) on a holiday with ads = 4?
Step 9 of 13Practice · Chart
An interaction: ads pay off more on holidays
Tap the ad spend where the holiday advantage is largest.
Tap the point that answers the question, then press Check.
| Ad spend | Holiday | Regular day |
|---|---|---|
| 0 | 15 | 10 |
| 2 | 22 | 14 |
| 4 | 29 | 18 |
| 6 | 36 | 22 |
| 8 | 43 | 26 |
Step 10 of 13Note
Step 11 of 13Practice · Match the pairs
Match each model term to what its coefficient means.
Tap an answer, then tap the place where it belongs (or drag it there).
- Downtown dummy (Rural is the reference)
- Intercept in a model with dummies
- Interaction ads × holiday
- Ads, in a model with the ads × holiday interaction
Step 12 of 13Practice · Choose one
Why is it a mistake to code a "region" variable as North = 1, South = 2, East = 3, West = 4 and enter it as a single numeric predictor?
Step 13 of 13Practice · True or false
Including a dummy variable for every category of a variable, along with an intercept, makes the regression coefficients impossible to estimate uniquely.
Lesson complete!