[R] Changing Order of Factor Levels in Mixed Model (nlme)

Ben Bolker bbolker at gmail.com
Wed May 15 03:11:52 CEST 2013


Edward Patzelt <patze003 <at> umn.edu> writes:

> 
> R Help -
> 
> Why is that in the results below, changing the order of the factor
> (trialType2: levels - DD, SD, DS, SS) changes the estimates in the fixed
> effects tests?

  I think you're not doing what you expected.  By sorting the factor,
you are _not_ changing the order of the factor levels (which you might
have been trying to do in order to change the parameterization); rather,
you're changing the actual order of the observations of the factor,
which is scrambling their association with the other variables
(response=proportion.down and the grouping variable, subject).
  I can't think of a scenario under which sorting the order of only one
of the variables in the data frame is not a mistake, unless you're
trying to randomize the order to do a permutation test.

  What you might have meant to do is to change the order of
the _levels_ of the factor, which you can do via

tmp.dat4$trialType2 <- factor(tmp.dat4$trialType2,
                        levels=c("DD","SD","DS","SS"))

or perhaps

tmp.dat4 <- transform(tmp.dat4,
   trialType2=factor(trialType2,levels=sort(levels(trialType2))))

(see also ?relevel and ?reorder)

  Changing the order of the factor levels will also change
the specific estimates of the fixed-effect parameters, in this
case by changing the parameterization (contrasts), which are
by default based on differences from the first factor level
(although see also ?contr.SAS), but not the overall meaning/fit
of the model.

  By the way, this isn't specifically a mixed-effects model
question -- the same issues would apply with just about any
statistical model in R (see e.g. Faraway's books on linear
and generalized models -- some early drafts are available in
the contributed documentation section).



More information about the R-help mailing list