Skip to content

estimating contrasts and their standard errors from mixed effects models in lme4

1 message · Nicholas Lewin-Koh

#
Hi Eva,
Be careful when estimating contrasts when you have interactions in the
model. When doing contrasts on
the fixed effects you need to account for the interactions when looking
at the main effects.
Since I invariably screw up the contrast matrices, I like to use both
the contrast package to get the contrast matrix,
and then multcomp to calculate the contrasts and adjust for multiple
comparisons. Here is 
an example

library(contrasts)
library(multcomp)
library(mlmRev)
library(lme4)
data(egsingle)
 
fm1 <- lmer(math~year*size+female+(1|childid)+(1|schoolid), egsingle) ##
The mixed model
fm2 <- lm(math~year*size+female, data=egsingle) ## Main effects to get
contrast matrix, there is no method for lme4 objects

cc<-contrast(fm2,a=list(year=c(.5,1.5,2.5),size=380, female="Male"),
b=list(year=c(.5,1.5,2.5),size=800, female="Male"))

summary(glht(fm1, linfct = cc$X)) ## Plug in the contrast matrix from
contrast, multcomp will use the variance covariance matrix

	 Simultaneous Tests for General Linear Hypotheses

Fit: lmer(formula = math ~ year * size + female + (1 | childid) + 
    (1 | schoolid), data = egsingle, verbose = 1)

Linear Hypotheses:
       Estimate Std. Error z value Pr(>|z|)  
1 == 0  0.12774    0.08018   1.593   0.1271  
2 == 0  0.15323    0.08065   1.900   0.0661 .
3 == 0  0.17871    0.08177   2.185   0.0341 *
---
Signif. codes:  0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 
(Adjusted p values reported -- single-step method)


Note,the degrees of freedom in these tests is still a contentious issue.
But I think this is what
you are looking for.

Nicholas