Sub topic 7
Representing Categories with Dummy Variables
Assume you know simple linear regression, slope and intercept interpretation, and hypothesis tests using t-statistics and p-values. This chapter focuses on using qualitative information, such as region or education level, as explanatory variables in a multiple regression.
Why dummy variables are needed
A regression model requires numerical inputs. A category such as region has labels, not meaningful numerical distances. Coding East as 1, Central as 2, and West as 3 would incorrectly imply that West is three times East and that the distance between categories is meaningful.
A dummy variable is a numerical variable that equals 1 when an observation belongs to a category and 0 otherwise. For a category with m groups, a regression normally uses m − 1 dummy variables. The omitted group is the reference category.
Suppose annual salary depends on years of experience and region. Let East be the reference region:
Salaryi = β0 + β1 Experiencei + β2 Northi + β3 Southi + ε_i
Here, Northi equals 1 for a person in the North and 0 otherwise. Southi is defined similarly. A person in the East has both dummy variables equal to 0.
Interpreting coefficients
The intercept, β0, is the predicted salary for the reference group when all numerical predictors equal zero. In this example, it is the predicted salary for an East employee with zero years of experience.
The coefficient β1 is the expected salary change for one additional year of experience, holding region constant. This is a partial slope.
The coefficient β2 is the predicted salary difference between North and East employees with the same experience. It is not the predicted salary for a North employee by itself. The coefficient β3 gives the corresponding difference between South and East employees.
| Coefficient | Interpretation |
|---|---|
| β0 | Reference-group prediction when numerical predictors equal zero |
| β1 | Change in salary for one more year of experience, holding region fixed |
| β2 | North-minus-East salary difference at the same experience |
| β3 | South-minus-East salary difference at the same experience |
To change the comparison group, recode the variables or interpret differences between estimated coefficients. For example, the predicted North-minus-South difference is β2 − β3.
import numpy as np
import pandas as pd
import statsmodels.api as sm
data = pd.DataFrame({
"salary": [50, 55, 62, 58, 65, 72, 54, 61, 68],
"experience": [1, 3, 5, 2, 4, 7, 2, 5, 6],
"region": ["East", "East", "East", "North", "North", "North",
"South", "South", "South"]
})
data = pd.get_dummies(
data,
columns=["region"],
drop_first=True,
dtype=int
)
X = sm.add_constant(data[["experience", "region_North", "region_South"]])
model = sm.OLS(data["salary"], X).fit()
print(model.params)
print(model.summary())With drop_first=True, East becomes the reference category. The North and South coefficients compare those regions with East while controlling for experience.
Testing category effects
A t-test can test one regional difference. For example:
- Null hypothesis: β2 = 0
- Alternative hypothesis: β2 ≠ 0
This tests whether North and East have different expected salaries after controlling for experience.
An F-test can test the regional variables jointly:
- Null hypothesis: β2 = β3 = 0
- Alternative hypothesis: at least one regional effect differs from zero
The joint test answers whether region contributes explanatory power as a group. Individual t-tests can be insignificant even when the joint F-test is significant.
Common errors and extensions
The reference category is part of the model, not missing data. Its effect is contained in the intercept and the comparisons made by the other dummy coefficients.
A category effect can vary with another variable. For example, if experience has a different salary effect by region, include interaction terms such as North × Experience. Then the experience slope for North is β1 plus the interaction coefficient.
Also check the meaning of zero for numerical variables. If zero experience is outside the useful data range, the intercept may have little practical meaning even though the fitted model remains valid.
Exercise
A regression uses Urban and Rural indicators with an intercept. What problem occurs if both indicators are included for every observation, and how can it be fixed?
Show answer
Urban + Rural always equals 1, so the indicators are perfectly collinear with the intercept. Omit one indicator and use it as the reference group, or remove the intercept.