Dummy variable coding

Definition taught in Linear model examples, part of Data analysis

Dummy variable coding

Birth weight against the mother’s age, with separate least-squares lines for non-smokers and smokers. From Linear model examples.
Type Definition
Taught in Linear model examples
Part Data analysis
Worked examples 2

A categorical explanatory variable with d levels enters a linear model through d-1 indicator columns, one for each level except a reference level. The coefficient of an indicator is the difference in mean response between that level and the reference level, with the other explanatory variables held fixed.

Examples

Explaining birth weight

Worked example from Linear model examples

Question. Fit birth weight on the mother’s weight, age, race and smoking, and interpret each coefficient.

Solution.

library(MASS)
model1 <- lm(bwt ~ lwt + age + factor(race) + smoke, data = birthwt)
summary(model1)
#                Estimate Std. Error t value Pr(>|t|)
# (Intercept)    2839.433    321.435   8.834  8.2e-16 ***
# lwt               4.000      1.738   2.301  0.02249 *
# age              -1.948      9.820  -0.198  0.84299
# factor(race)2  -510.501    157.077  -3.250  0.00137 **
# factor(race)3  -398.644    119.579  -3.334  0.00104 **
# smoke          -401.720    109.241  -3.677  0.00031 ***
# Residual standard error: 682.1 on 183 degrees of freedom
# Multiple R-squared:  0.1483,  Adjusted R-squared:  0.125
# F-statistic: 6.373 on 5 and 183 DF,  p-value: 1.758e-05

The overall F test, p=1.8\times10^{-5}, says at least one of the variables is associated with birth weight. Taking them one at a time, each with the others held fixed:

  • each extra pound of the mother’s weight goes with 4.0 g more birth weight on average (p=0.022);
  • the mother’s age adds nothing once the other variables are known (p=0.84);
  • babies of black mothers weigh 511 g less on average than those of white mothers, the reference level, and those of mothers of other races 399 g less (p=0.001 for both);
  • babies of mothers who smoked weigh 402 g less than those of non-smokers of the same weight, age and race (p=0.0003).

The model explains only 15% of the variation in birth weight; the residual standard deviation is 682 g. These are observational data, so the coefficients are associations adjusted for the variables in the model, not effects: race, for instance, stands in for many unmeasured differences.

Age and smoking interact

Worked example from Linear model examples

Question. Does the association between the mother’s age and birth weight depend on smoking?

Solution.

model2 <- lm(bwt ~ age * smoke, data = birthwt)
summary(model2)
#             Estimate Std. Error t value Pr(>|t|)
# (Intercept) 2406.058    292.190   8.235 3.18e-14 ***
# age           27.731     12.149   2.283   0.0236 *
# smoke        798.175    484.342   1.648   0.1011
# age:smoke    -46.572     20.447  -2.278   0.0239 *

The interaction is significant (p=0.024). For non-smokers, birth weight rises by 27.7 g per year of the mother’s age; for smokers, the slope is 27.73-46.57=-18.8 g per year, a decline. The coefficient of smoke alone, +798 g, is the difference between smokers and non-smokers at age zero, an extrapolation with no meaning; with an interaction in the model, main-effect coefficients have to be read together with it. Age had no association with birth weight in the additive model above precisely because the two groups’ slopes point in opposite directions and largely cancel.

Used in

See also

Glossary and index