Using the sleepstudy data from the lme4 package I want to do pairwise comparison using the emmeans package.
This is the model:
library(lme4)
lmm <- lmer(Reaction ~ Days + (1 + Days | Subject), sleepstudy)
Now when I want to do pairwise comparison like this, I only get NAs, no pairwise comparisons:
library(emmeans)
lmm.pw <- emmeans(lmm, "Days")
pairs(lmm.pw)
According to the emmeans FAQs, this is due to "Days" being numeric. However, when I try to fit the model with "Days" as factor it gives me the following error:
lmm <- lmer(Reaction ~ as.factor(Days) + (1 + as.factor(Days) | Subject), sleepstudy)
number of observations (=180) <= number of random effects (=180) for term (0 + fDays | Subject); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable
This was already solved on stackexchange but I don't really understand why I should be able to fit the model when "Days" is numeric, but not when it is a factor?
Likewise, why do I get different summary()
outputs for these two simpler models, when the only difference is that "Days" is either a factor or numeric?
lmm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy)
lmm1f <- lmer(Reaction ~ as.factor(Days) + (1|Subject), sleepstudy)
I am able to get pairwise comparisons from the lmm1f-model. But the fixed effects table that I get by running summary(lmm1f)
seems to treat every level of as.factor(Days)
as a separate fixed effect (EDIT Dec 30 2020: see here for an explanation of the summary output), which makes me wonder if the model truly does what I want it to do.
Pretty sure I'm overlooking/not understanding some basic stuff. Any help is appreciated, thanks!
I'm using R version 4.0.2 in RStudio Version 1.3.1073 on macOS 11.1.
Packages: lme4 (1.1-23), emmeans (1.5.0)