if (FALSE) { # require("lme4") && require("glmmTMB")
data(sleepstudy, package = "lme4")
set.seed(123)
sleepstudy$mygrp <- sample(1:5, size = 180, replace = TRUE)
sleepstudy$mysubgrp <- NA
for (i in 1:5) {
filter_group <- sleepstudy$mygrp == i
sleepstudy$mysubgrp[filter_group] <-
sample(1:30, size = sum(filter_group), replace = TRUE)
}
model <- lme4::lmer(
Reaction ~ Days + (1 | mygrp / mysubgrp) + (1 | Subject),
data = sleepstudy
)
check_singularity(model)
if (FALSE) {
# Fixing singularity issues using priors in glmmTMB
# Example taken from `vignette("priors", package = "glmmTMB")`
dat <- readRDS(system.file(
"vignette_data",
"gophertortoise.rds",
package = "glmmTMB"
))
model <- glmmTMB::glmmTMB(
shells ~ prev + offset(log(Area)) + factor(year) + (1 | Site),
family = poisson,
data = dat
)
# singular fit
check_singularity(model)
# impose Gamma prior on random effects parameters
prior <- data.frame(
prior = "gamma(1, 2.5)", # mean can be 1, but even 1e8
class = "ranef" # for random effects
)
model_with_priors <- update(model, priors = prior)
# no singular fit
check_singularity(model_with_priors)
}
}
Run the code above in your browser using DataLab