Fits a linear mixed model via [lme4::lmer()], catching non-convergence, singular fit warnings, or missing package conditions and gracefully falling back to a fixed-effects linear model ([stats::lm()]).
Value
A list of class `"cbe_lmer_safely"` containing:
- model
The fitted `merMod` or `lm` object
- method
Character indicating `"lmer"` or `"lm"`
- is_singular
Logical indicating whether the mixed model had a singular fit
- formula
The formula used for the final fit
Examples
df <- data.frame(
id = rep(1:5, each = 4),
time = rep(1:4, times = 5),
y = rnorm(20)
)
fit <- fit_lmer_safely(y ~ time + (1 | id), data = df)
#> boundary (singular) fit: see help('isSingular')
#> Warning: `lme4::lmer()` produced a singular fit; falling back to `stats::lm()`.
fit$method
#> [1] "lm"
