Skip to content

Instantly share code, notes, and snippets.

@bbolker
Created June 23, 2024 23:32
Show Gist options
  • Save bbolker/dafc1d11dfa89602ee5a1f1666bae247 to your computer and use it in GitHub Desktop.
Save bbolker/dafc1d11dfa89602ee5a1f1666bae247 to your computer and use it in GitHub Desktop.
reproduce example for CV question
## https://stats.stackexchange.com/questions/649052/selecting-the-correct-r-lme4-syntax-for-nested-models
library(lme4)
df <- data.frame(Subject = rep(factor(1:120), each = 200),
Group = rep(LETTERS[1:3], each = 40*200))
with(df, table(Group))
with(df, table(table(Subject)))
df$Accuracy <- simulate(~ Group + (1|Subject),
newdata = df,
newparams = list(beta = c(0, 1, 2),
theta = 1),
family = binomial,
seed = 101)[[1]]
model3 <- glmer(Accuracy ~ Group + (1|Group:Subject),
data=df,
family=binomial(link="logit"),
glmerControl(optimizer = "bobyqa"),
na.action=na.omit)
## replace 'Group:Subject' by 'Subject'
model4 <- update(model3, . ~ . - (1|Group:Subject) + (1|Subject))
identical(car::Anova(model3), car::Anova(model4)) ## TRUE
identical(coef(summary(model3)), coef(summary(model4))) ## TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment