singmann / afex

Analysis of Factorial EXperiments (R package)
119 stars 32 forks source link

Differences in output between `lmer` and `mixed` #4

Open smithdanielle opened 9 years ago

smithdanielle commented 9 years ago

I've noticed that when specifying a model using the lmer function in the lme4 package which contains factor-type predictors, the suffix indicating the level of the predictor is a character string of that factor level, as is the case for treatment here:

library(afex)

data(obk.long)

m1 <- lmer(value ~ treatment + (1|id), obk.long)
summary(m1)

Fixed effects:
        Estimate Std. Error t value
(Intercept)    4.200      0.654    6.43
treatmentA     2.050      0.980    2.09
treatmentB     1.800      0.856    2.10

However, when using the mixed function in the afex package, the suffix is numeric:

m2 <- mixed(value ~ treatment + (1|id), obk.long)
summary(m2$full.model) # this should be the same as the lmer output... it's er, not

Fixed effects:
            Estimate Std. Error t value
(Intercept)    5.483      0.375   14.62
treatment1    -1.283      0.532   -2.41
treatment2     0.767      0.565    1.36

Firstly, could you comment on what causes the difference in the predictor label level suffix? Secondly, what's up with the differences in the fixed effects?

singmann commented 9 years ago

The reason for this difference is that mixed per default uses sum-to-zero contrasts whereas base R uses treatment contrasts (in which the first factor level is the baseline). When asking mixed to not enforce sum-to-zero contrasts, the results replicate the lmer results:

m3 <- mixed(value ~ treatment + (1|id), obk.long, check.contrasts = FALSE)
summary(m3)

Fixed effects:
            Estimate Std. Error t value
(Intercept)   4.2000     0.6537   6.425
treatmentA    2.0500     0.9805   2.091
treatmentB    1.8000     0.8558   2.103

Sum-to-zero contrasts are a better choice for a situation with interactions of categorical variables and Type III sums-of-squares tests of fixed effect terms (which is quite a common situation). In such a situation treatment contrasts will report tests at the reference level for lower-order effects (commonly known as simple main effects) instead of the intended omnibus test of the term (e.g., a normal main effect).

The difference in the fixed effects estimate is given by their different interpretation. For the treatment-contrast case they reflect the difference of this factor level from the reference level. For sum-to-zero contrast they represent the difference from the (unweighted) grand mean (and for the last group the difference from the grand mean it is the negative sum of all the estimates).

I have written more on this and will post it here, once the corresponding paper is accepted (and will keep the issue open until then).

smithdanielle commented 9 years ago

Aha, all is (mostly) clear. Thanks for the explanation. If you'd be willing to share a pre-print of the paper I'd be very grateful, but if not, no problem.

I am currently trying to create tables for publication; my current plan is to have two, a fixed-effects table with an additional column with standardised beta values, and a table with the ANOVA output by mixed(). Do you have an opinion on the best way to label the factor levels in the fixed-effects table?

From what I can understand, treatment1 is the control treatment vs all other treatments, and treatment2 is treatment A vs all other treatments. Would it be wrong to relabel treatment1 and treatment2 as Control treatment vs all treatments and `Treatment A vs all treatments'?

Apologies, this question is more suited to CrossValidated, but I figured that the creator of afex would have an opinion worth listening to in this case!

singmann commented 9 years ago

I would avoid posting the fixed effects estimates as they will most likely only cause confusion. For example, your interpretation of the sum-to-zero contrast estimates is not correct. They represent the difference from the (unweighted) grand mean and not from all other groups. And they do not directly relate to only one level, but to one level and the last level (which makes their interpretation really difficult). If you send me a mail, so I have your address, I will send you a copy of the paper.