title: Can the order of main effect vs interaction terms in lm() in R be specified?
body: <p>I have a linear model and it is important the terms are entered in a specific order because I plan to use a type I ANOVA. I would like the model to include the first two main effects and their interaction before the third main effect.</p>
  <p>However when I enter this as a formula into <code>lm()</code>, it still gives me the output with all three main effects first and then the interactions. Can this be changed?</p>
  <pre><code>## Example data:
  df &lt;- structure(list(x1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
   1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,3L, 3L, 3L, 3L, 3L), 
  .Label = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;), class = &quot;factor&quot;), 
  x2 = c(0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L,
   0L, 1L, 0L, 1L, 1L, 0L, 0L),
  x3 = c(1L, 3L, 4L, 5L, 2L, 3L, 3L, 4L, 5L, 1L, 3L, 4L, 5L, 6L, 4L, 3L, 4L,
   1L, 2L, 2L, 1L, 1L, 3L, 2L),
  y = c(49.5, 62.8, 46.8, 57, 59.8, 58.5, 55.5, 56, 62.8, 55.8, 69.5, 55, 62,
   48.8, 45.5, 44.2, 52, 51.5, 49.8, 48.8, 57.2, 59, 53.2, 56)), 
  class = &quot;data.frame&quot;, row.names = c(NA, -24L))
  
  ## formula using desired order:
  mod1 &lt;- lm(y ~ x1 + x2 + x1:x2 + x3 + x1:x3 + x2:x3 + x1:x2:x3, data=df)
  
  ## standard formula:
  mod2 &lt;- lm(y ~ x1*x2*x3, data=df)
  
  ## same order in anova output:
  anova(mod1)
  anova(mod2)
  
  ## test to show coefficient order affects outputs:
  ## (but can only change main effects around)  
  anova(mod2 &lt;- lm(y ~ x1*x2*x3, data=df))
  anova(mod3 &lt;- lm(y ~ x1*x3*x2, data=df))
  
  </code></pre>