1

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?

Updates:

  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.
corn_bunting
  • 183
  • 6

2 Answers2

1

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.

Kirsten
  • 419
  • 8
  • 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`? – corn_bunting Jun 17 '21 at 10:03
  • You should be able to do y ~ x1 + x2 + x1:x2 + x3 – Kirsten Jun 17 '21 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 :) – corn_bunting Jun 17 '21 at 14:54
1

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.

Greg
  • 126
  • 2
  • Turns out you can also use a `terms` object, [see here](https://stackoverflow.com/questions/68007332/can-the-order-of-main-effect-vs-interaction-terms-in-lm-in-r-be-specified?noredirect=1#comment120201911_68007332) :) – corn_bunting Jun 22 '21 at 08:08