Unit 9, lesson 66

Categorical Predictors and Interactions

How to code a categorical predictor with a reference category, how an interaction term lets slopes differ by group, why a group coefficient is then a conditional effect at one predictor value, and why model hierarchy matters.

  • Video 66
  • Multiple Regression and Model Building

Suppose a response rises with a numerical predictor in two groups, but faster in one group than in the other. A model with a group indicator and a single slope draws two parallel lines, so it gives the same group difference at every value of the predictor and cannot show this. Adding the product of the predictor and the indicator lets each group have its own slope. This lesson shows how to code categories with a reference category, how to read an interaction model one group at a time, and why its group coefficient describes the difference at one particular predictor value rather than everywhere.

Key terms

Categorical predictor

Definition A categorical predictor is a predictor whose values are categories, such as route, region or treatment, rather than measured quantities. A categorical predictor with kk categories enters a regression model through indicator variables, k−1k - 1 of them when the model has an intercept. Coding the categories as the numbers 1,2,…,k1, 2, \dots, k and fitting one slope instead imposes an order and equal spacing, because it forces the same change in fitted mean response from each category to the next.

In plain words A category label says which group a unit belongs to. It is not a measured amount, even when the categories have a natural order. Indicators let each group have its own level without treating the labels as positions on a number line.

Example Code three regions as 11, 22 and 33 and fit y^=a+bx\hat{y} = a + bx with that code as xx. The fitted means of the regions are a+ba + b, a+2ba + 2b and a+3ba + 3b, so the region coded 22 is forced to lie exactly halfway between the other two, and a numbering that puts a different region in the middle gives a different model. With an intercept and indicators for regions 22 and 33, the three fitted means are free, and a least-squares fit with no other predictors makes them the three sample means.

Reference coding

Definition Reference coding chooses one category as the reference category and includes, next to the intercept, an indicator for each of the other k−1k - 1 categories. In a model without interactions, the coefficient of each indicator is the difference in fitted mean response between its category and the reference category at the same values of the other predictors. Changing the reference category can change the coefficients and flip their signs. In a hierarchical model, described under model hierarchy below, it does not change any least-squares fitted value, because the columns of both codings then span the same space. Without hierarchy it can, since a model with only 11, gg and xgxg forces the reference group’s line to be flat, and switching the reference changes which line that is.

In plain words The reference category is the baseline, and every indicator coefficient is measured from it. A coefficient reported without its reference category may be impossible to read correctly.

Example With g=1g = 1 for group B and group A as the reference, take the constructed fitted model

y^=10+2x+3g−xg.\hat{y} = 10 + 2x + 3g - xg .

With h=1h = 1 for group A instead, substituting g=1−hg = 1 - h gives

y^=13+x−3h+xh,\hat{y} = 13 + x - 3h + xh ,

which describes the same two fitted lines with B as the reference. The group coefficient changes from 33 to −3-3 and the coefficient of the product term from −1-1 to 11. For three categories A, B and C with A as the reference, the indicators are IBI_B and ICI_C, and a model with separate slopes adds the products xIBxI_B and xICxI_C.

Interaction term

Definition For a numerical predictor xx and an indicator gg that is 11 for group B and 00 for the reference group A, the interaction term is the product xgxg. The model

Y=β0+β1x+β2g+β3xg+ε\begin{aligned} Y &= \beta_0 + \beta_1 x + \beta_2 g \\[0.4em] &\quad + \beta_3 xg + \varepsilon \end{aligned}

has the mean line β0+β1x\beta_0 + \beta_1 x in group A and (β0+β2)+(β1+β3)x(\beta_0 + \beta_2) + (\beta_1 + \beta_3)x in group B, so β3\beta_3 is the slope in B minus the slope in A. More generally, two predictors interact in a model when the change in mean response that goes with one of them depends on the value of the other. When each group has at least two distinct xx values, the least-squares lines from this model are the same as separate least-squares lines fitted in each group. The single model still estimates one residual variance from both groups together, which assumes a common σ2\sigma^2.

In plain words Without the product term the two lines are parallel, and the group difference is the same at every xx. The product term lets the lines have different slopes, so they can come together, cross or spread apart.

Example In y^=10+2x+3g−xg\hat{y} = 10 + 2x + 3g - xg, setting g=0g = 0 removes both 3g3g and xgxg and leaves y^A=10+2x\hat{y}_A = 10 + 2x. Setting g=1g = 1 gives 10+2x+3−x10 + 2x + 3 - x, so y^B=13+x\hat{y}_B = 13 + x. Group A has slope 22 and group B has slope 11, and the interaction coefficient is 1−2=−11 - 2 = -1.

Conditional effect

Definition In a model with an interaction, a conditional effect is the change in the fitted mean response that goes with a change in one predictor at a stated value of the other. In the fitted model y^=b0+b1x+b2g+b3xg\hat{y} = b_0 + b_1 x + b_2 g + b_3 xg, the group difference at xx is

y^B(x)−y^A(x)=b2+b3x,\hat{y}_B(x) - \hat{y}_A(x) = b_2 + b_3 x ,

and the slope of xx in group gg is b1+b3gb_1 + b_3 g. The coefficients b1b_1 and b2b_2, often called main-effect coefficients, are conditional effects too. The coefficient b1b_1 is the slope in the reference group, and b2b_2 is the group difference at x=0x = 0. Writing the model in the centered predictor z=x−cz = x - c turns the group coefficient into the difference at x=cx = c and changes no fitted value. Here effect means a difference between fitted values under the model. It has a causal reading only when the design or further assumptions support one, for example when the groups were formed by random assignment.

In plain words With an interaction there is no single group difference. The useful question is how large the difference is at a particular xx, and the model gives its own answer for each xx. Centering xx at a meaningful value inside the data, such as a typical value, makes the group coefficient report the difference there. A main-effect coefficient near zero may describe only the chosen zero point or the reference group, so check the conditional comparisons across the relevant range before saying a variable has no association.

Example For y^=10+2x+3g−xg\hat{y} = 10 + 2x + 3g - xg the group difference is (13+x)−(10+2x)=3−x(13 + x) - (10 + 2x) = 3 - x. At x=0x = 0 group B is 33 above group A, at x=4x = 4 it is 11 below, and at x=3x = 3 both lines equal 1616. Centering at 22 with z=x−2z = x - 2 gives

y^=14+2z+g−zg.\hat{y} = 14 + 2z + g - zg .

The group coefficient is now 11, the difference at x=2x = 2, where group A predicts 1414 and group B predicts 1515. The slopes 22 and 11 and every fitted value are unchanged.

Model hierarchy

Definition A regression model respects hierarchy if, whenever it contains a product of predictors or a power of a predictor, it also contains every lower-order term built from the same predictors, as well as the intercept. A categorical predictor counts as one predictor, so all of its indicators enter each term that involves it. For the interaction xgxg this means including xx and gg, and for x2x^2 it means including xx. In a hierarchical model, recentering a numerical predictor or changing the reference category leaves every least-squares fitted value unchanged. A model that drops a lower-order term imposes a restriction whose meaning depends on where zero is placed or on which category is the reference.

In plain words Keep xx and gg whenever xgxg is in the model, unless there is a subject-matter reason for the restriction that dropping one of them imposes. A large p-value for a main-effect coefficient is not such a reason, because that coefficient describes a comparison at a zero point that may have been chosen arbitrarily.

Example The model with only 11, xx and xgxg has no gg term, so it forces the two group lines to meet at x=0x = 0. If xx is recentered at 22, the same kind of model forces them to meet at x=2x = 2 instead, so the two versions generally give different fits to the same data. The model with only 11, gg and xgxg forces group A’s line to be flat, since g=0g = 0 and xg=0xg = 0 for every unit in group A.

Check yourself

  1. A fitted model is y^=20+3x−6g+2xg\hat{y} = 20 + 3x - 6g + 2xg, where g=1g = 1 for group B and group A is the reference. Write each group’s fitted line and explain what the coefficients −6-6 and 22 mean.
Show the answer

Setting g=0g = 0 gives group A the line y^A=20+3x\hat{y}_A = 20 + 3x. Setting g=1g = 1 gives 20+3x−6+2x20 + 3x - 6 + 2x, so group B has the line y^B=14+5x\hat{y}_B = 14 + 5x. The interaction coefficient 22 is the slope in B minus the slope in A, 5−35 - 3. The coefficient −6-6 is the difference B minus A at x=0x = 0 only, not at other values of xx.

  1. For the same model, find where the two fitted lines coincide, and find the difference B minus A at x=1x = 1 and at x=5x = 5.
Show the answer

The difference is (14+5x)−(20+3x)=−6+2x(14 + 5x) - (20 + 3x) = -6 + 2x. It is zero at x=3x = 3, where both groups have fitted value 2929. At x=1x = 1 group A predicts 2323 and group B predicts 1919, a difference of −4-4. At x=5x = 5 group A predicts 3535 and group B predicts 3939, a difference of 44. The crossing describes the data only if x=3x = 3 lies inside the range observed in both groups.

  1. An analyst refits the model of the first question with the centered predictor z=x−3z = x - 3. Find the new coefficients. A colleague reads the new group coefficient and says the groups do not differ, and another reads the original fit and says group B is 66 units below group A. Correct both.
Show the answer

Substituting x=z+3x = z + 3 gives

y^=20+3(z+3)−6g+2(z+3)g=29+3z+0g+2zg.\begin{aligned} \hat{y} &= 20 + 3(z + 3) - 6g \\[0.4em] &\quad + 2(z + 3)g \\[0.4em] &= 29 + 3z + 0g + 2zg . \end{aligned}

Because the model is hierarchical, the least-squares refit gives exactly these coefficients and the same fitted values. The group coefficient 00 says only that the lines meet at x=3x = 3. The coefficient −6-6 says only that B is 66 below A at x=0x = 0. The group difference is 2z=−6+2x2z = -6 + 2x, which changes with xx, so neither number is the group difference. Report the difference at the predictor values that matter, or give both lines.

How this lesson connects