Sub topic 7
Representing categories with dummy variables
This chapter assumes you can read a multiple regression output: partial slope coefficients, t-statistics and p-values, and the F-test. It also assumes you know the basic OLS assumptions from earlier chapters. Here you learn how to put qualitative information, such as "January" or "technology sector", into a regression that only accepts numbers.
What a dummy variable is
A dummy variable (also called an indicator variable) takes the value 1 when an observation has a characteristic and 0 when it does not. It lets a regression use categories.
Examples from CFA-style problems:
- A firm is in the technology sector (1) or not (0).
- A return was recorded in January (1) or in another month (0).
- A quarter falls after a regulatory change (1) or before it (0).
Consider a model for monthly excess return:
Return = b0 + b1 × Beta + b2 × JanuaryDummy + error
The dummy is just another independent variable. Its coefficient has a specific meaning, described next.
Interpreting the coefficients
Take the two possible values of the dummy and substitute them.
- When JanuaryDummy = 0: expected return = b0 + b1 × Beta.
- When JanuaryDummy = 1: expected return = (b0 + b2) + b1 × Beta.
So the dummy shifts the intercept. The coefficient b2 is the average difference in the dependent variable between the group coded 1 and the omitted (base) group, holding the other variables constant.
If b2 = 0.9 (percentage points), then, at the same beta, January returns average 0.9 points more than returns in the other months. The intercept b0 is the expected return for the base group when all other variables equal zero.
The t-test on b2 tests whether the group difference is statistically different from zero. The test is the same as for any other slope: t = estimated coefficient divided by its standard error, compared with a critical value with n − k − 1 degrees of freedom.
The n − 1 rule and the dummy variable trap
A category with n possible values needs n − 1 dummy variables. The omitted category becomes the base group, and all dummy coefficients are measured relative to it.
Months of the year are a standard case. There are 12 months, so you use 11 dummies, for example February through December, with January as the base. The intercept then represents January.
If you include all n dummies and also an intercept, the dummies add up to 1 for every observation. That equals the constant column exactly, which is perfect multicollinearity. OLS cannot separate the intercept from the dummies, and the model cannot be estimated. This is the dummy variable trap.
The choice of base group does not change the fitted values, R-squared, or F-statistic. It changes only how the coefficients are labeled and which comparisons the t-tests perform directly. A t-test on a dummy compares that category with the base category only. To compare two non-base categories, you need a joint or linear-restriction test.
A worked example: a sector dummy
Suppose you regress the price-to-earnings ratio (P/E) on expected earnings growth for a sample of firms. Some are technology firms. The estimated model is:
P/E = 8.0 + 1.5 × Growth + 4.0 × TechDummy
with these standard errors: 0.7 for Growth and 1.6 for TechDummy, and 60 observations (so 57 degrees of freedom with k = 2).
For a firm with 10% growth:
- Non-technology: 8.0 + 1.5 × 10 = 23.0.
- Technology: 8.0 + 4.0 + 1.5 × 10 = 27.0.
The t-statistic for the dummy is 4.0 / 1.6 = 2.5. With 57 degrees of freedom, the two-tailed 5% critical value is about 2.00. Since 2.5 exceeds 2.00, you reject the null that the sector has no effect on P/E, after controlling for growth. The technology premium of 4 P/E points is statistically significant.
You can check this in Python. The code simulates data from this model and recovers the dummy coefficient.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(7)
n = 60
growth = rng.uniform(2, 20, size=n)
tech = rng.integers(0, 2, size=n)
pe = 8.0 + 1.5 * growth + 4.0 * tech + rng.normal(scale=3.0, size=n)
X = sm.add_constant(np.column_stack([growth, tech]))
model = sm.OLS(pe, X).fit()
print(model.params)
print(model.tvalues)
print(model.pvalues)The second coefficient printed is the growth slope, and the third is the technology shift. The estimates vary around 1.5 and 4.0 because of the random noise.
Slope dummies and interaction terms
An intercept dummy assumes the slope on the other variable is the same in both groups. Sometimes the relationship itself differs by group. To allow that, multiply the dummy by the variable to form an interaction term:
P/E = b0 + b1 × Growth + b2 × Tech + b3 × (Tech × Growth)
- Non-technology: slope on Growth is b1, intercept is b0.
- Technology: slope on Growth is b1 + b3, intercept is b0 + b2.
The coefficient b3 is the difference in slope between the groups. A significant b3 means growth is priced differently in technology firms. The dummy b2 alone then no longer measures the full group gap, because the gap now depends on the level of growth: it equals b2 + b3 × Growth.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(11)
n = 120
growth = rng.uniform(2, 20, size=n)
tech = rng.integers(0, 2, size=n)
pe = 8.0 + 1.0 * growth + 2.0 * tech + 1.0 * tech * growth + rng.normal(scale=3.0, size=n)
X = sm.add_constant(np.column_stack([growth, tech, tech * growth]))
model = sm.OLS(pe, X).fit()
print(model.params)
# Joint test that technology differs from non-technology in intercept AND slope
print(model.f_test("x2 = 0, x3 = 0"))The joint F-test is the right way to ask whether the two groups follow the same line at all. Testing b2 and b3 one at a time can miss a difference when the two estimates are highly correlated.
Several categories at once
With a categorical variable of m levels, you add m − 1 dummies. Each one has its own t-test against the base. To test whether the categorical variable matters at all, use an F-test that all m − 1 dummy coefficients equal zero.
For quarterly seasonality, use three dummies (Q2, Q3, Q4) with Q1 as the base:
| Quarter | Q2 | Q3 | Q4 | Expected value |
|---|---|---|---|---|
| Q1 | 0 | 0 | 0 | b0 |
| Q2 | 1 | 0 | 0 | b0 + b2 |
| Q3 | 0 | 1 | 0 | b0 + b3 |
| Q4 | 0 | 0 | 1 | b0 + b4 |
(Here b1 is the slope on another variable, so the quarter dummies carry the labels b2 to b4.) Each dummy coefficient equals that quarter's average difference from Q1.
Common errors and edge cases
- Dummy variable trap. Using m dummies plus an intercept. The software either errors out or silently drops a column. Check which category became the base.
- Misreading the base. A dummy coefficient is a difference from the omitted group, not an absolute level. "Not significant" means "not different from the base", not "has no effect".
- Reading intercept shifts as slope changes. An intercept dummy does not change how the other variables affect the outcome. Use an interaction if you think slopes differ.
- Tiny groups. If only a few observations have a 1, the coefficient is estimated imprecisely and one outlier can drive it. Check the group counts first.
- Many dummies and degrees of freedom. Each dummy uses one degree of freedom. Eleven month dummies in a short sample reduce power, and adjusted R-squared or AIC and BIC will penalize them if they add little.
- Heteroskedasticity across groups. The error variance often differs between groups, such as large and small firms. The dummy corrects the mean but not the variance. Check with a Breusch-Pagan test and use robust standard errors if needed, as covered in the heteroskedasticity chapter.
- A dummy for an event window. A dummy that equals 1 after a structural change tests whether the intercept shifted. If the slopes changed as well, you need interactions, which links to the data-pooling issue in the misspecification chapter.
Exam reminders
- With an intercept, use n − 1 dummies for n categories.
- The coefficient is the difference from the omitted category, holding other variables constant.
- Intercept dummy: shifts the line. Interaction term: changes the slope.
- Test all dummies for one categorical variable together with an F-test.
- Test statistic for any dummy: t = coefficient / standard error, with n − k − 1 degrees of freedom.
Exercise
A regression of monthly return on market return and a dummy for January gives: Return = 0.2 + 1.1 × MarketReturn + 1.4 × JanDummy, with a standard error of 0.5 on the January coefficient, from 84 observations. (a) What is the expected return in a January when the market return is 3%? (b) Is the January effect significant at the 5% level (critical t about 1.99 with 81 degrees of freedom)? (c) How many dummies do you need to model all 12 months separately, and why?
Show answer
(a) 0.2 + 1.1 × 3 + 1.4 = 4.9%. (b) t = 1.4 / 0.5 = 2.8, which exceeds 1.99, so you reject the null that January returns equal those of other months, after controlling for the market return. (c) You need 11 dummies, with one month as the base group. Twelve dummies plus the intercept would sum to the constant column, giving perfect multicollinearity.
Exercise
In the model Y = b0 + b1 × X + b2 × D + b3 × (D × X), where D = 1 for group B and 0 for group A, give the slope and intercept for each group, and state what a significant b3 tells you.
Show answer
Group A: intercept b0, slope b1. Group B: intercept b0 + b2, slope b1 + b3. A significant b3 means the effect of X on Y differs between the groups, so the two groups should not be forced to share a slope.