# [R] Keep factor order in lmer

Pascal A. Niklaus pascal.niklaus at ieu.uzh.ch
Wed Feb 3 15:34:04 CET 2016

```I would like to keep a specific order of fixed effects in a model passed
to lmer. In particular, I would like to prevent that interactions are
automatically moved after all main effects.

In aov and lme, this is possible with terms(..., keep.order=TRUE).
Unfortunately, I have not found a way to achieve this behaviour in lmer.

Here is an example that illustrates the problem:

d <- data.frame(
R=factor(rep(1:4,each=8)),
A=factor(rep(1:2,each=4)),
B=factor(rep(1:2,each=2)),
C=factor(1:2))

d\$y <- rnorm(nrow(d))

## example using aov, with intended output

summary(aov(terms(y~A*B+C,keep.order = TRUE),data=d))

Df Sum Sq Mean Sq F value Pr(>F)
A            1  0.831  0.8308   0.832  0.370
B            1  0.356  0.3557   0.356  0.556
A:B          1  0.103  0.1035   0.104  0.750
C            1  0.992  0.9919   0.993  0.328
Residuals   27 26.970  0.9989

## works also in lme

anova(lme(terms(y~A*B+C,keep.order=TRUE),random=~1|R,data=d))

## but how do I achieve this in lmer?

anova(lmer(y~A*B+C+(1|R),data=d))

Of course, I could recode the interactions into new single factors, but
that seems rather cumbersome.