The coefficients I'm seeing from a merMod
model produced by lmer
are not what I'd expect. I made a reproducible example to illustrate.
I assumed that the Days coefficient reported for a nested effect (in this case, subject "308" within "Cohort1") would be the sum of the Days fixed effect plus the random effect coefficient for Days within Cohort and within Subject.
I can produce this coefficient myself by adding exactly this way (18.7) and confirm with predictions (18.7). But the reported coefficient is different (21.15).
Am I interpreting something wrong? Appreciate any help!
library(tidyverse)
library(lme4)
# add a categorical variable "Cohort" that Subject can be nested in
df <- sleepstudy %>%
mutate(Cohort = if_else(
as.numeric(as.character(Subject)) < 336,
"Cohort1",
"Cohort2"
))
# fit
sleep_model <- lmer(Reaction ~ Days + (Days | Cohort/Subject), df)
#> boundary (singular) fit: see help('isSingular')
# the report coef
coef(sleep_model)[["Subject:Cohort"]]["308:Cohort1", "Days"]
#> [1] 21.15208
# what I think the coef would be
fixef(sleep_model)["Days"] +
ranef(sleep_model)[["Cohort"]]["Cohort1", "Days"] +
ranef(sleep_model)[["Subject:Cohort"]]["308:Cohort1", "Days"]
#> Days
#> 18.70102
# predict and divide delta y / delta x
predict(
sleep_model,
tibble(Cohort = "Cohort1", Subject = "308", Days = c(1, 2))
) %>%
diff()
#> 2
#> 18.70102
Created on 2023-03-26 with reprex v2.0.2