Many R packages exist for fitting generalised linear mixed models, using different estimation methods:
lme4::glmer (most widely used): Frequentist GLMMs via Laplace approximation (nAGQ = 1) or adaptive Gauss-Hermite quadrature (nAGQ > 1)
MASS::glmmPQL: Penalized quasi-likelihood (PQL; approximate, fast, but can be biased for binary/sparse outcomes)
MCMCglmm: Bayesian GLMMs fitted by Markov chain Monte Carlo (MCMC)
glmmTMB (very popular modern alternative): Frequentist GLMMs via Template Model Builder/Laplace; especially strong for count models, zero inflation, and overdispersion
ordinal::clmm: Cumulative link mixed models for ordinal outcomes (an ordinal GLMM family)
glmmML: Classical random-intercept GLMMs using adaptive Gauss-Hermite quadrature (more limited model scope)
ASREML-R: Commercial software with broad mixed-model/GLMM capability, widely used in biostatistics/agricultural genetics
glmmADMB: GLMMs via AD Model Builder/Laplace (historically important, but largely superseded and no longer actively maintained)
glmm (from Jim Lindsey’s repeated package): Older AGHQ-based approach for repeated-measures GLMMs (now uncommon)
sabreR, mcemGLM, gamlss.mx: Specialized/less commonly used frameworks that can fit mixed or latent/random-effect extensions in narrower settings
Ohio Respiratory Data Fahrmeir and Tutz (2001) Example 7.1 and 7.3
Setup
The Ohio respiratory dataset contains repeated respiratory status measurements (yes/no) for children in a longitudinal study, with covariates including age and smoking status. We’ll fit GLMM models to account for within-child correlation using random intercepts. Our goal is to reproduce some of Table 7.2 from Fahrmeir and Tutz (2001).
Load required packages and do the standard preparation of the Ohio respiratory dataset:
When fitting a generalised linear mixed model (GLMM), the likelihood involves integrating over the random effects, which typically has no closed-form solution. Therefore we need to choose a numerical integration method to approximate the likelihood. The lme4::glmer() function provides two main methods for this: the Laplace approximation and adaptive Gauss-Hermite quadrature (AGHQ). The default lme4::glmer() approach uses the Laplace approximation. Once we have chosen the integration method, we also need to specify the optimization algorithm; the Nelder-Mead optimizer is recommended by Faraway (2016).
Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model failed to converge with max|grad| = 0.0589875 (tol = 0.002, component 1)
See ?lme4::convergence and ?lme4::troubleshooting.
ImportantSELF TEST: What does the (1 | id) formula mean?
This specifies a random intercept model: (1 | id) means each subject (id) has its own random intercept drawn from a normal distribution with mean 0 and estimated standard deviation. The 1 represents the intercept term; | id conditions on subject ID. This allows each subject to have a different baseline probability of respiratory status while maintaining a common effect of the fixed covariates.
We can see that there is an error message about convergence. Checking ?lme4::troubleshooting suggests that this type of error may be fixed by trying a different optimiser. The other optimiser available is Bound Optimization BY Quadratic Approximation (BOBYQA). Let’s try that:
There are no longer convergence issues, and comparing the log-likelihoods, we see that the log-likelihood is slightly higher with the BOBYQA optimizer. The coefficient estimates are slightly different, but the overall pattern of results is the same.
The output is very similar to that in the linear mixed model fit, with fixed effects estimates, as well as a correlation matrix for the fixed effects, and an estimate of the random effect variance.
The coefficient estimates are in the right ballpark, but ultimately don’t match very closely with those in the book. Instead we follow the book’s approach and try using adaptive Gauss-Hermite quadrature (AGHQ) to estimate the integration.
CautionStandard Errors in Table 7.2
Although strictly speaking the mixed effects model is not in the exponential family, it is still a likelihood-based model, and so in theory, we should be able to approximate a Fisher information matrix to compute standard errors. Unfortunately the lme4::glmer() function is not well documented enough for us to reproduce the standard errors in the book.
lme4::glmer() Fitting with Adaptive Gauss-Hermite Quadrature
In order to get more accurate estimates, we can improve the numerical integration by using adaptive Gauss-Hermite quadrature (AGHQ) instead of the Laplace approximation. The nAGQ argument in lme4::glmer() controls the number of quadrature points used for the integration. The default nAGQ = 1 corresponds to the Laplace approximation, while nAGQ > 1 uses multiple quadrature points for a more accurate approximation of the likelihood.
Here we try nAGQ = 10, which is what Fahrmeir and Tutz (2001) claim to use in their analysis.
Note that now we are no longer dealing with normally distributed errors, and so the modes are not necessarily the same as the means of the random effects distribution.
Penalized quasi-likelihood (PQL) is an alternative (and much faster) method that doesn’t require numerical integration. See Chapter 13 in Faraway (2016) for more details.
Wine Bitterness: Fahrmeir and Tutz (2001) Example 7.2 and 7.4
Now we demonstrate ordinal regression with random effects. For this we consider the ordinal::wine dataset, described in Example 7.2 of Fahrmeir and Tutz (2001), which contains ratings of wine bitterness by different judges under different conditions.
As always, we should inspect the data help page
Code
?wine
The covariates are - response: a continuous measure of bitterness - rating: an ordered factor rating of bitterness (1-5) - temp: temperature (cold/warm) - contact: contact with skin (yes/no) - judge: judge ID
We will regress the ordinal rating on the covariates temp and contact, while including a random intercept for judge to account for judge-specific differences in rating tendencies, in order to reproduce some of the results in Table 7.4 of Fahrmeir and Tutz (2001).
Data Setup and Contrast Coding
Even though the book says that no contact is the reference level, the analysis appears to be done with contact as the reference level.
Call:
polr(formula = rating ~ temp + contact, data = wine_tbl, Hess = TRUE,
method = "logistic")
Coefficients:
Value Std. Error t value
tempcold -2.503 0.5287 -4.735
contactno -1.528 0.4766 -3.205
Intercepts:
Value Std. Error t value
1|2 -5.3753 0.7680 -6.9991
2|3 -2.7801 0.5306 -5.2397
3|4 -0.5640 0.4076 -1.3837
4|5 0.9755 0.4593 2.1238
Residual Deviance: 172.9838
AIC: 184.9838
Our coefficient estimates for the non-threshold coefficients are relatively close to the book’s (up to the signs as we expect, explained in the first ordinal regression script).
The book reports thresholds differently to how we do, but we can easily convert our results to the book’s formulation (see the equation (7.5.1) on page 320 to see why this is the case). Let’s check:
log(diff(thresholds)): log-transforms those positive gaps so they can be modelled on the real line.
c(...): concatenates the first cutpoint and the logged gaps into one vector.
So if thresholds = (theta_1, theta_2, theta_3, theta_4), then this creates \[
\alpha = \left(\theta_1,\log(\theta_2-\theta_1),\log(\theta_3-\theta_2),\log(\theta_4-\theta_3)\right).
\]
This guarantees ordered thresholds when converting back, because exponentiating the gap terms makes each gap strictly positive.
These are also close but not exact. For a final sanity check, we can compare our log-likelihood with the book’s:
Code
logLik(wine_fixed_fit_polr)
'log Lik.' -86.49192 (df=6)
The log-likelihood is close to the reported log-likehood in Table 7.4, but not exact. In fact, ours is marginally higher than the book’s. The reason for all the small mismatches is not clear.
TipAlternative Fit with ordinal::clm()
In order to make sure that the mismatches aren’t due to the specific implementation of MASS::polr(), we can try a different function ordinal::clm():
Code
wine_fixed_fit_clm <-clm(rating ~ temp + contact, data = wine_tbl)summary(wine_fixed_fit_clm)
We can extract fitted values and contrast structure from the fitted model. Note that the fitted values are evaluated at the conditional modes of the random effects distribution (see ?clmm).
Code
# Fitted values for first few observationsfitted(wine_mixed_fit)
conflicted: Wickham H (2023). conflicted: An Alternative Conflict Resolution Strategy. R package version 1.2.0, https://github.com/r-lib/conflicted, https://conflicted.r-lib.org/.
dplyr: Wickham H, François R, Henry L, Müller K, Vaughan D (2026). dplyr: A Grammar of Data Manipulation. R package version 1.2.0, https://github.com/tidyverse/dplyr, https://dplyr.tidyverse.org.
geepack:
Halekoh U, Højsgaard S, Yan J (2006). “The R Package geepack for Generalized Estimating Equations.” Journal of Statistical Software, 15/2, 1-11.
Yan J, Fine JP (2004). “Estimating Equations for Association Structures.” Statistics in Medicine, 23, 859-880.
Yan J (2002). “geepack: Yet Another Package for Generalized Estimating Equations.” R-News, 2/3, 12-14.
Xu, Z., Fine, P. J, Song, W., Yan, J. (2025). “On GEE for mean-variance-correlation models: Variance estimation and model selection.” Statistics in Medicine, 44, 1-2.
lme4: Bates D, Mächler M, Bolker B, Walker S (2015). “Fitting Linear Mixed-Effects Models Using lme4.” Journal of Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01 https://doi.org/10.18637/jss.v067.i01.
MASS: Venables WN, Ripley BD (2002). Modern Applied Statistics with S, Fourth edition. Springer, New York. ISBN 0-387-95457-0, https://www.stats.ox.ac.uk/pub/MASS4/.
Fahrmeir, Ludwig, and Gerhard Tutz. 2001. Multivariate Statistical Modelling Based on Generalized Linear Models. Second Edition. Springer Series in Statistics. Springer. https://doi.org/10.1007/978-1-4757-3454-6.
Faraway, Julian J. 2016. Extending the Linear Model with r : Generalized Linear, Mixed Effects and Nonparametric Regression Models, Second Edition. Second edition. Chapman & Hall/CRC Texts in Statistical Science Series. Chapman; Hall/CRC. https://research.ebsco.com/plink/3806c0de-f79d-330d-9c4e-7bb136efaa53.