Edward Patzelt
2013-May-14 17:20 UTC
[R] Changing Order of Factor Levels in Mixed Model (nlme)
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?> tmp.dat4$trialType2 <- sort(tmp.dat4$trialType, decreasing = TRUE) > mod2c <- lme(proportion.down ~ trialType2, data = tmp.dat4, random = ~ 1| subject, na.action = na.omit, method = "ML")> summary(mod2c)Linear mixed-effects model fit by maximum likelihood Data: tmp.dat4 AIC BIC logLik 27.92306 48.23003 -7.961531 Random effects: Formula: ~1 | subject (Intercept) Residual StdDev: 0.3800017 0.1530272 Fixed effects: proportion.down ~ trialType2 Value Std.Error DF t-value p-value (Intercept) 0.6788875 0.08613476 141 7.881690 0.0000 trialType2DS 0.0287062 0.11267357 141 0.254773 0.7993 trialType2SD -0.0197194 0.12142018 141 -0.162406 0.8712 trialType2SS -0.0941918 0.12204707 141 -0.771766 0.4415 Correlation: (Intr) trT2DS trT2SD trialType2DS -0.658 trialType2SD -0.709 0.467 trialType2SS -0.706 0.464 0.571 Standardized Within-Group Residuals: Min Q1 Med Q3 Max -3.67176824 -0.22090663 0.08677971 0.14331603 2.86301670 Number of Observations: 218 Number of Groups: 74> tmp.dat4$trialType2 <- sort(tmp.dat4$trialType, decreasing = FALSE) > mod2c <- lme(proportion.down ~ trialType2, data = tmp.dat4, random = ~ 1| subject, na.action = na.omit, method = "ML")> summary(mod2c)Linear mixed-effects model fit by maximum likelihood Data: tmp.dat4 AIC BIC logLik 27.92306 48.23003 -7.961531 Random effects: Formula: ~1 | subject (Intercept) Residual StdDev: 0.3800017 0.1530272 Fixed effects: proportion.down ~ trialType2 Value Std.Error DF t-value p-value (Intercept) 0.5846957 0.08646554 141 6.762181 0.0000 trialType2DS 0.0744724 0.11277773 141 0.660347 0.5101 trialType2SD 0.1228981 0.12175741 141 1.009368 0.3145 trialType2SS 0.0941918 0.12204707 141 0.771766 0.4415 Correlation: (Intr) trT2DS trT2SD trialType2DS -0.660 trialType2SD -0.710 0.469 trialType2SS -0.708 0.468 0.573 Standardized Within-Group Residuals: Min Q1 Med Q3 Max -3.67176824 -0.22090663 0.08677971 0.14331603 2.86301670 Number of Observations: 218 Number of Groups: 74 -- *Edward H Patzelt | Research Assistant Psychology | University of Minnesota | Elliott Hall, 75 East River Road | Minneapolis, MN 55455 Email: patze003@umn.edu | Main: 612.626.0072 | Mobile: 651.315.3410 | Office: S355 ** * [[alternative HTML version deleted]]
Ben Bolker
2013-May-15 01:11 UTC
[R] Changing Order of Factor Levels in Mixed Model (nlme)
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).