Skip to content

Instantly share code, notes, and snippets.

@bbolker
Created May 3, 2023 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbolker/ddcb25c3cb90de06da4302103fe79475 to your computer and use it in GitHub Desktop.
Save bbolker/ddcb25c3cb90de06da4302103fe79475 to your computer and use it in GitHub Desktop.
illustration that dropping scalar singular terms makes no difference
library(lme4)
set.seed(101)
nn <- 100
ng <- 10
dd <- data.frame(x = rnorm(nn),
f = factor(rep(1:(nn/ng), ng)),
g = factor(rep(1:ng, each = ng)))
dd$y1 <- simulate( ~ x + (1|f),
newdata = dd,
newparams = list(beta = c(1,1),
theta = 0,
sigma = 1),
family = gaussian)[[1]]
m1 <- lmer(y1 ~ x + (1|f), data = dd)
m0 <- lm(y1 ~ x, data = dd)
all.equal(coef(summary(m1)),
coef(summary(m0))[,1:3])
## now try it with one singular and one non-singular
dd$y2 <- simulate( ~ x + (1|f) + (1|g),
newdata = dd,
newparams = list(beta = c(1,1),
theta = c(0,1),
sigma = 1),
family = gaussian,
## play with seed until Var(f) = 0
seed = 104)[[1]]
m3 <- lmer(y2 ~ x + (1|f) + (1|g), data = dd)
m4 <- update(m3, . ~ . - (1|f))
all.equal(coef(summary(m3)),
coef(summary(m4)),
tolerance = 1e-6)
all.equal(VarCorr(m3)["g"],
VarCorr(m4)["g"],
tolerance = 1e-5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment