# Why is y~x1+x1:x2 and y~x1+x2+x1:x2 interpreted as equivalent models?

I came across unsuspected behavior of the lm function in R (3.6.1.) when evaluating model with interaction. Here is a toy example:

#generate data
set.seed(0)
data = data.frame(x1 = factor(rep(0:1, 10)),x2 = rbinom(20,10,1/2),y = rnorm(20))

#model and its submodel without main effect of x2
m1 = lm(y ~ x1 + x2 + x1:x2, data=data)
m2 = lm(y ~ x1 + x1:x2, data=data)

#compare models -> it's the same model
anova(m2, m1)


I would expect m1 and m2 to differ by 1 degree of freedom. However, the term x1:x2 in m2 is interpreted the different way then in m1 and it generates two regressors instead of one.

I understand the interpretation of each coefficients in m1 and m2. The thing that bothers me is what logic is hidden under this behavior and how I can perform submodel test when I need to omit one main effect and preserve its interaction with a nominal factor.

• it's been too long to give a good answer but I'm pretty sure that the reasoning has to do with the the idea that including an interaction without its main effect doesn't really make sense. Commented Mar 3, 2020 at 14:19
• Inspect both m1 and m2 and you'll understand :) Commented Mar 3, 2020 at 14:23
• @Firebug Thanks, I already did that. The problem is not that I don't understand the models. The question is why is " : " interpreted different way in each model and how can I avoid this behavior. Commented Mar 3, 2020 at 14:27
• : is not interpreted in a different way, see my answer Commented Mar 3, 2020 at 14:31

They are in fact the same model.

Long story short, if we inspect both models

m1

#Call:
#lm(formula = y ~ x1 + x2 + x1:x2, data = data)
#
#Coefficients:
#(Intercept)          x11           x2       x11:x2
#    0.26253     -0.10427     -0.05206     -0.02664

m2

#Call:
#lm(formula = y ~ x1 + x1:x2, data = data)
#
#Coefficients:
#(Intercept)          x11       x10:x2       x11:x2
#    0.26253     -0.10427     -0.05206     -0.07870


Since you specified the interaction x1:x2 in both models, and since x1 is a factor, in m2 R understands that you specified x2 slopes for both levels of x1, as we can see in the model summary in the terms x10:x2 and x11:x2.

This makes sense, it does not report anymore the "baseline" x2 effect but instead one effect per level in x1, which amounts to the same model as m1.

The link between both models' coefficients is simply shown to be:

coef(m1)[4]
#     x11:x2
#-0.02663733
diff(coef(m2)[3:4])
#     x11:x2
#-0.02663733

• Do you know a way to avoid this behavior? In some cases it is desired to exclude one main effect without replacing it with a second interaction term. Commented Mar 3, 2020 at 14:33
• @DanielDostal if x1 is not a factor it works, but you'll have to carefully think about the model formulation that way. The coding in x1 will have an impact on the model specification, and I personally don't think it makes sense anyways to specify the model like that. Commented Mar 3, 2020 at 14:43
• Thanks, @Firebug . Changing the factor to quantitative variable works in this example but it has its caveats when the factor has many levels. I know that the interpretation of the model is strange but it some context it makes sense. Commented Mar 3, 2020 at 14:53
• @DanielDostal - you could also go the painful way of expanding x1 into a collection of dummy variables, calculating all the cross-variables with x2, putting them into the regression, and leaving out the x1 dummies. Commented Mar 3, 2020 at 16:48