Generalized linear mixed models with sommer
sommer development team
2026-10-06
Source:vignettes/sommer.glmm.Rmd
sommer.glmm.RmdOverview
mmes() fits generalized linear mixed models (GLMMs) when
given a non-Gaussian stats::family() object. The model has
conditional mean
where
is the link function,
is an optional offset, and the random effects retain sommer’s usual
structured Gaussian covariance model,
.
Thus, random, rcov, vsm(),
relationship precision matrices, and covariance structures are specified
exactly as for a Gaussian mixed model.
The default family = gaussian() with its identity link
takes the ordinary linear mixed-model path. Supply another family
explicitly for a GLMM:
fit <- mmes(
fixed = outcome ~ treatment,
random = ~ subject,
rcov = ~ units,
data = dat,
family = binomial()
)The initial implementation accepts one numeric response. Binomial responses may be a zero/one numeric vector; grouped-binomial matrix responses are not yet supported.
How the fit works
PQL and IRLS
Sommer uses penalized quasi-likelihood (PQL). At outer iteration , it linearizes the GLMM at the current link-scale predictor . With
the working response and diagonal IRLS precision are
Sommer then calls its existing weighted Gaussian Henderson/AI-REML solver on . This updates fixed effects, BLUPs, and covariance parameters; the new predictor is . The outer loop stops when the deviance change is sufficiently small.
This approach deliberately reuses ai_mme_sp2() as the
weighted Gaussian inner optimizer. It does not maximize an exact
marginal GLMM likelihood. PQL is practical for structured and large
random-effect models, but can be biased for binary or sparse count
responses, particularly with large random-effect variances. Treat
standard errors and variance components as quasi-likelihood
approximations in those settings.
Dispersion and covariance structures
For binomial() and poisson(), the working
residual dispersion is fixed to one. For other families, sommer
estimates the Gaussian working-scale residual covariance using the
supplied rcov structure. All existing random-effect
structures remain available. For example:
Offsets and precision weights
Use offset() in the fixed formula as in
glm(). A common Poisson rate model uses the logarithm of an
exposure:
fit <- mmes(
events ~ treatment + offset(log(exposure)),
random = ~ site,
rcov = ~ units,
data = dat,
family = poisson()
)W is an optional symmetric positive-definite observation
precision matrix. It may be sparse and non-diagonal. If
,
sommer combines it at each PQL iteration with IRLS precision as
This preserves both the user-supplied correlation/precision structure
and the family-dependent working precision. The sparse Cholesky factor
is reused across outer iterations. Missing observations are filtered
before this factorization, so W may be supplied either for
all original rows or for the retained rows.
Families and examples
The interface uses standard stats family objects. The
following examples use a random intercept to show the shared GLMM
syntax. They are illustrative and therefore not run while building this
vignette.
set.seed(2026)
n_group <- 30
n_per_group <- 8
dat <- data.frame(
group = factor(rep(seq_len(n_group), each = n_per_group)),
x = rep(c(0, 1), length.out = n_group * n_per_group),
exposure = runif(n_group * n_per_group, 0.5, 2)
)Gaussian
The Gaussian identity model is the ordinary mmes()
linear mixed model.
dat$y_gaussian <- 2 + 0.7 * dat$x + rnorm(nrow(dat), sd = 1)
fit_gaussian <- mmes(
y_gaussian ~ x, random = ~ group, rcov = ~ units, data = dat,
family = gaussian()
)A non-identity Gaussian link can also be requested, for example
gaussian(link = "log"), provided its domain is appropriate
for the response.
Inverse Gaussian
The inverse-Gaussian family also requires a strictly positive response. The response below is positive synthetic data for demonstrating the interface.
dat$y_inverse_gaussian <- exp(0.2 + 0.3 * dat$x + rnorm(nrow(dat), sd = 0.2))
fit_inverse_gaussian <- mmes(
y_inverse_gaussian ~ x, random = ~ group, rcov = ~ units, data = dat,
family = inverse.gaussian(link = "log")
)Quasi families
Quasi families use the same mean/link and variance definitions as their corresponding GLM families, but do not define a likelihood. Consequently, devience is useful for PQL convergence, while likelihood-based comparisons such as AIC or likelihood-ratio tests are not appropriate.
dat$y_quasi <- 1 + 0.5 * dat$x + rnorm(nrow(dat), sd = 0.5)
fit_quasi <- mmes(
y_quasi ~ x, random = ~ group, rcov = ~ units, data = dat,
family = quasi(link = "identity", variance = "constant")
)
fit_quasibinomial <- mmes(
y_binomial ~ x, random = ~ group, rcov = ~ units, data = dat,
family = quasibinomial()
)
fit_quasipoisson <- mmes(
y_poisson ~ x + offset(log(exposure)),
random = ~ group, rcov = ~ units, data = dat,
family = quasipoisson()
)Extracting results and controlling PQL
For a GLMM, fitted() returns conditional fitted means on
the response scale. Use type = "link" for
,
including any offset. Residuals can be requested on response,
signed-deviance, or final working scales.
fitted(fit_poisson)
fitted(fit_poisson, type = "link")
residuals(fit_poisson, type = "deviance")
residuals(fit_poisson, type = "working")
fit_poisson$pqlMonitor
fit_poisson$pqlConverged
fit_poisson$familyUse pqlControl to set the outer PQL maximum iterations
and relative devience tolerance. The usual nIters,
tolParConvLL, stepWeight,
emWeight, and solver arguments continue to control the
Gaussian variance-component fit inside each outer iteration.
Practical guidance
- Begin with a simple random-intercept GLMM and inspect
pqlMonitorbefore adding complex covariance structures. - Check
pqlConverged; reachingpqlControl$maxitindicates that the outer PQL criterion was not met. - Keep binomial responses away from complete separation where possible; very small derivatives can create unstable IRLS weights.
- For binary outcomes with few observations per random-effect level or large random-effect variances, compare conclusions with a method based on a Laplace approximation or Bayesian posterior simulation when feasible.
- Use response-scale fitted values for prediction and link-scale fitted values when interpreting additive fixed and random effects.