I am analyzing the results of a reaction time experiment in R.
I ran a repeated measures ANOVA (1 within-subject factor with 2 levels and 1 between-subject factor with 2 levels). I ran a similar linear mixed model and I wanted to summarize the lmer results in the form of ANOVA table using lmerTest::anova
.
Don't get me wrong: I did not expect the identical results, however I am not sure about the degrees of freedom in lmerTest::anova
results. It seems to me it rather reflects an ANOVA with no aggregation on subject-level.
I am aware of the fact that calculating degrees of freedom in mixed-effect models is tricky, but lmerTest::anova
is mentioned as one possible solution in the updated ?pvalues
topic (lme4
package).
Is this calculation correct? Do the results of lmerTest::anova
correctly reflect the specified model?
Update: I made the individual differences larger. The degrees of freedom in lmerTest::anova
are more different from simple anova, but I am still not sure, why they are so large for the within-subject factor/interaction.
# mini example with ANT dataset from ez package
library(ez); library(lme4); library(lmerTest)
# repeated measures ANOVA with ez package
data(ANT)
ANT.2 <- subset(ANT, !error)
# update: make individual differences larger
baseline.shift <- rnorm(length(unique(ANT.2$subnum)), 0, 50)
ANT.2$rt <- ANT.2$rt + baseline.shift[as.numeric(ANT.2$subnum)]
anova.ez <- ezANOVA(data = ANT.2, dv = .(rt), wid = .(subnum),
within = .(direction), between = .(group))
anova.ez
# similarly with lmer and lmerTest::anova
model <- lmer(rt ~ group * direction + (1 | subnum), data = ANT.2)
lmerTest::anova(model)
# simple ANOVA on all available data
m <- lm(rt ~ group * direction, data = ANT.2)
anova(m)
Results of the code above [updated]:
anova.ez
$ANOVA
Effect DFn DFd F p p<.05 ges
2 group 1 18 2.6854464 0.11862957 0.1294475137
3 direction 1 18 0.9160571 0.35119193 0.0001690471
4 group:direction 1 18 4.9169156 0.03970473 * 0.0009066868
lmerTest::anova(model)
Analysis of Variance Table of type 3 with Satterthwaite
approximation for degrees of freedom
Df Sum Sq Mean Sq F value Denom Pr(>F)
group 1 13293 13293 2.6830 18 0.1188
direction 1 1946 1946 0.3935 5169 0.5305
group:direction 1 11563 11563 2.3321 5169 0.1268
anova(m)
Analysis of Variance Table
Response: rt
Df Sum Sq Mean Sq F value Pr(>F)
group 1 1791568 1791568 242.3094 <2e-16 ***
direction 1 728 728 0.0985 0.7537
group:direction 1 12024 12024 1.6262 0.2023
Residuals 5187 38351225 7394
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Best Answer
I think that
lmerTest
is getting it right andezanova
is getting it wrong in this case.lmerTest
agree with my intuition/understandinglmerTest
(Satterthwaite and Kenward-Roger) agreenlme::lme
ezanova
gives a warning, which I don't entirely understand, but which should not be disregarded ...Re-running example:
Figure out experimental design
So it looks like individuals (
subnum
) are placed in either control or treatment groups, and each is tested for both directions -- i.e. direction can be tested within individuals (denominator df is large), but group and group:direction can only be tested among individualsHere I get
Warning: collapsing data to cell means. *IF* the requested effects are a subset of the full design, you must use the "within_full" argument, else results may be inaccurate.
The denominator DF look a little funky (all equal to 18): I think they should be larger for direction and group:direction, which can be tested independently (but would be smaller if you added(direction|subnum)
to the model)?the
Df
column here refers to the numerator df,Denom
(second-to-last) gives the estimated denominator df; they agree with the classical intuition. More important, we also get different answers for the F values ...We can also double-check with Kenward-Roger (very slow because it involves refitting the model several times)
The results are identical.
For this example
lme
(from thenlme
package) actually does a perfectly good job guessing the appropriate denominator df (the F and p-values are very slightly different):If I fit an interaction between
direction
andsubnum
the df fordirection
andgroup:direction
are much smaller (I would have thought they would be 18, but maybe I'm getting something wrong):