# Multiple comparisons with ANOVA including one between and one within-subject effect

I'm trying to look at multiple comparisons (across levels of my between subjects factor) for a model with one between-subject factor and one within-subject factor. I was trying to use TukeyHSD on an aovlist object, and then found out that I couldn't do that.

After some browsing, I found how to do what I wanted using lme() and glht():

options(contrasts=c("contr.sum", "contr.poly"))
lme_model=lme(dv ~ between*within, data frame, random=~1|ID,
correlation=corCompSymm(form=~1|ID))
anova(lme_model, type="marginal")
summary(glht(lme_model, linfct=mcp(between="Tukey")))


It seems to work (the anova() results seem to jive with what I was getting from aov()), but I guess the problem is that I don't really understand what lme() is, or what it's doing. Does anyone have any documentation that I can look at?

The lme() function, from the nlme package (or see the companion website), allows to fit mixed-effects models with gaussian errors. For an alternative framework, see this question: How to choose nlme or lme4 R library for mixed effects models? Your formula reads as follows: your model includes a between- by within-subject interaction assuming compound symmetry for the variance-covariance structure; subjects are considered random effects (in other words, this is a varying intercept model, as symbolized by the ~1|ID formula for the random term). The type="marginal" argument indicates that you want to compute Type III sum of squares for fixed effects. (See this or this question for issues with the different types of SSs, and discussion on the UCLA server on How can I get Type III tests of fixed effects in R?) This corresponds to what would be obtained using aov() with the appropriate error term (see the corresponding section in Notes on the use of R for psychology experiments and questionnaires). More details about R's formulas can be found on our R's lmer cheat-sheet.

(And if you have some background in psychology, I'd recommend Practical Data Analysis for the Language Sciences with R, by Baayen, which has a lot of illustrations on the analysis of crossed effects in psycholinguistic; there's also a R package, languageR.)

Finally, as you can see a lot has been discussed in related threads so it's worth browsing the or tags. I'd also like to put a direct link to a related thread that might be of interest: Why do lme and aov return different results for repeated measures ANOVA in R?

For documentation, type ?lme in your R console.

You also may want to try this:

anova_result <- aov(dv ~ between * within, data=dataframe)
TukeyHSD(anova_result)


(I admit I don't understand what all your arguments to lme are doing. You may be able to pass some of those same arguments to aov.)

• Such a model doesn't account for the within-subject term because it assumes uncorrelated errors.
– chl
Dec 9 '11 at 10:16