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 categories enters a regression model through indicator variables, of them when the model has an intercept. Coding the categories as the numbers 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 , and and fit with that code as . The fitted means of the regions are , and , so the region coded 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 and , 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 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 , and 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 for group B and group A as the reference, take the constructed fitted model
With for group A instead, substituting gives
which describes the same two fitted lines with B as the reference. The group coefficient changes from to and the coefficient of the product term from to . For three categories A, B and C with A as the reference, the indicators are and , and a model with separate slopes adds the products and .
Interaction term
Definition For a numerical predictor and an indicator that is for group B and for the reference group A, the interaction term is the product . The model
has the mean line in group A and in group B, so 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 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 .
In plain words Without the product term the two lines are parallel, and the group difference is the same at every . The product term lets the lines have different slopes, so they can come together, cross or spread apart.
Example In , setting removes both and and leaves . Setting gives , so . Group A has slope and group B has slope , and the interaction coefficient is .
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 , the group difference at is
and the slope of in group is . The coefficients and , often called main-effect coefficients, are conditional effects too. The coefficient is the slope in the reference group, and is the group difference at . Writing the model in the centered predictor turns the group coefficient into the difference at 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 , and the model gives its own answer for each . Centering 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 the group difference is . At group B is above group A, at it is below, and at both lines equal . Centering at with gives
The group coefficient is now , the difference at , where group A predicts and group B predicts . The slopes and 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 this means including and , and for it means including . 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 and whenever 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 , and has no term, so it forces the two group lines to meet at . If is recentered at , the same kind of model forces them to meet at instead, so the two versions generally give different fits to the same data. The model with only , and forces group A’s line to be flat, since and for every unit in group A.
Check yourself
- A fitted model is , where for group B and group A is the reference. Write each group’s fitted line and explain what the coefficients and mean.
Show the answer
Setting gives group A the line . Setting gives , so group B has the line . The interaction coefficient is the slope in B minus the slope in A, . The coefficient is the difference B minus A at only, not at other values of .
- For the same model, find where the two fitted lines coincide, and find the difference B minus A at and at .
Show the answer
The difference is . It is zero at , where both groups have fitted value . At group A predicts and group B predicts , a difference of . At group A predicts and group B predicts , a difference of . The crossing describes the data only if lies inside the range observed in both groups.
- An analyst refits the model of the first question with the centered predictor . 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 units below group A. Correct both.
Show the answer
Substituting gives
Because the model is hierarchical, the least-squares refit gives exactly these coefficients and the same fitted values. The group coefficient says only that the lines meet at . The coefficient says only that B is below A at . The group difference is , which changes with , so neither number is the group difference. Report the difference at the predictor values that matter, or give both lines.