Categorical Predictors and Interaction Terms
What you'll learn: How to include categorical variables in regression models and capture when the effect of one predictor depends on another.
Incorporating Categorical Predictors with Dummy Coding
Until now, our predictors have been continuous numbers. But what if you want to include gender, region, or treatment group? These categorical variables don't have inherent numerical values.
The solution is dummy coding (also called indicator coding). For a categorical variable with k categories, create k-1 binary (0/1) variables. One category becomes the reference level (all dummies = 0), and each dummy represents "being in that category vs. the reference."
Example: Suppose you're modeling salary with experience and job type (Engineer, Manager, Analyst). Choose Analyst as reference. Create two dummies:
D_Engineer = 1if Engineer,0otherwiseD_Manager = 1if Manager,0otherwise
Your model becomes:
Salary = β₀ + β₁·Experience + β₂·D_Engineer + β₃·D_Manager + ε
Now β₂ measures the salary difference between Engineers and Analysts (holding experience constant), and β₃ does the same for Managers.
Modeling Interactions with Product Terms
Sometimes one predictor's effect changes depending on another's value. An interaction term captures this by multiplying predictors together.
Example: Perhaps experience matters more for Engineers than Analysts. Add interaction terms:
Salary = β₀ + β₁·Experience + β₂·D_Engineer + β₃·D_Manager
+ β₄·(Experience × D_Engineer) + β₅·(Experience × D_Manager) + ε
Now β₄ represents how much steeper the experience-salary slope is for Engineers compared to Analysts.
Think of interactions as asking: "Does the relationship between X and Y differ across groups or levels of Z?"
Key Takeaway: Use dummy coding to include categorical predictors (k-1 dummies for k categories), and create product terms between predictors to model interactions where one variable's effect depends on another's level.