# Is it okay to specify some interactions before all main effects in a linear model (and ANOVA?)

I would like to specify some interaction terms before all main effects in a linear model, but am finding it difficult to do in R, so I am wondering if there is a statistical reason why I shouldn't? The reason for specifying the order in the model is that next I want to use a type I ANOVA to investigate the effects of different variables.

I want this model: y ~ x1 + x2 + x1:x2 + x3 + x1:x3 + x2:x3 + x1:x2:x3

But R insists on this model (no matter what order I type the terms in)*: y ~ x1 + x2 + x3 + x1:x2 + x1:x3 + x2:x3 + x1:x2:x3

The purpose of using the first model and a type I ANOVA would be to account for all variance explained by x1 and x2 before seeing if any remaining variance is explained by x3. Is there any reason why I shouldn't be trying to do this?

1. Actually it is not that hard to do in R.
2. Maybe it helps to specify that in my case, x1 and x2 are factors and x3 is continuous.

It makes no sense to look at variance for x1 and x2 before x3 as long as you have the 3-factor interaction-term. Changing the order is only relevant if you remove the all interaction-terms with x1, x3 and x2, x3.

• So I should be able to have y ~ x1 + x2 + x1:x2 + x3 + x1:x3 + x2:x3? Or just y ~ x1 + x2 + x1:x2 + x3? Jun 17, 2021 at 10:03
• You should be able to do y ~ x1 + x2 + x1:x2 + x3 Jun 17, 2021 at 10:34
• Thanks - this is a useful answer, but a little more explanation on why would be helpful to people like me with a rather fuzzy understanding of what goes on behind an ANOVA :) Jun 17, 2021 at 14:54

As an addendum to Kirsten's answer, to get this to work in R you need to paste the two vectors together

x1x2 <- paste0(x1,x2)
y~x1+x2+x1x2+x3


otherwise anova will still put the interaction term last.

• Turns out you can also use a terms object, see here :) Jun 22, 2021 at 8:08