Created
May 3, 2023 21:13
-
-
Save bbolker/ddcb25c3cb90de06da4302103fe79475 to your computer and use it in GitHub Desktop.
illustration that dropping scalar singular terms makes no difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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