I am fitting a very simple model where group, time and id are factors:
> model <- afex::lmer(depvar ~ group * time + (1 | id), data = df)
> model
Linear mixed model fit by REML ['lmerModLmerTest']
Formula: depvar ~ group * time + (1 | id)
Data: df
REML criterion at convergence: 684.9437
Random effects:
Groups Name Std.Dev.
id (Intercept) 26.95
Residual 10.71
Number of obs: 84, groups: dSubjects, 28
Fixed Effects:
(Intercept) groupA time2 time3 groupA:time2 groupA:time3
104.286 -11.643 -10.071 -5.214 3.643 -10.500
> anova(model)
Type III Analysis of Variance Table with Satterthwaite's method
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
group 203.53 203.53 1 26 1.7758 0.194229
time 1703.02 851.51 2 52 7.4292 0.001452 **
group:time 754.93 377.46 2 52 3.2933 0.045013 *
Signif. codes: 0 ‘*’ 0.001 ‘’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
When I use emmeans to calculate the marginal means, I obtain exactly the same means as if I would simply aggregate the original data df.
> emmeans(model, "group", by="time")
time = 1:
group emmean SE df lower.CL upper.CL
P 104.28571 7.751323 31.3 88.48290 120.08852
A 92.64286 7.751323 31.3 76.84005 108.44567
time = 2:
group emmean SE df lower.CL upper.CL
P 94.21429 7.751323 31.3 78.41148 110.01710
A 86.21429 7.751323 31.3 70.41148 102.01710
time = 3:
group emmean SE df lower.CL upper.CL
P 99.07143 7.751323 31.3 83.26862 114.87424
A 76.92857 7.751323 31.3 61.12576 92.73138
Degrees-of-freedom method: kenward-roger
Confidence level used: 0.95
> aggregate(df$depvar~group*time,data=df,mean)
group time df$depvar
1 P 1 104.28571
2 A 1 92.64286
3 P 2 94.21429
4 A 2 86.21429
5 P 3 99.07143
6 A 3 76.92857
Shouldn't the estimated marginal means from emmeans take into account the effect of the random intercept? I apologize if this is trivial as I am a novice with linear mixed models.