Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

glmmTMB in R: Syntax, Zero-Inflation and a Salamanders Example

glmmTMB in R: family and zero-inflation choices, a Salamanders example, and a table of convergence warnings with documented fixes.

Written and maintained by CASRAI Editorial Board

Last updated

glmmTMB is an R package that fits linear and generalized linear mixed models with extensions such as zero-inflation, using the same formula syntax as lme4. This page is for researchers and analysts who need to fit a count, binomial or continuous mixed model in R, choose the family and zero-inflation arguments, and read the result and its warnings.

What glmmTMB is used for in research

The CRAN description is “Fit linear and generalized linear mixed models with various extensions, including zero-inflation.” In practice one function, glmmTMB(), covers models with random effects (for example repeated measures, sites or clusters) where the response is a count, a binomial outcome or a continuous measure, and where you may also need to model excess zeros or a dispersion that differs between groups. For the model class itself see generalized linear mixed models explained; for the lme4 route see lmer in R.

Choosing family, ziformula and dispformula

The table maps a data situation to the arguments documented in the package reference (checked 2026-11-08).

Data situation Arguments Documented behaviour
Counts with excess zeros family = poisson or nbinom2, plus ziformula = ~ predictors Default ziformula = ~0 means no zero-inflation; ~. reuses the right-hand side of the main formula; zero-inflation uses a logit link.
Hurdle model for counts family = truncated_poisson with ziformula Used in the package reference examples.
Binomial proportions family = binomial with cbind(successes, failures) as the response The reference example uses the cbpp data from lme4.
Variance that differs between groups dispformula = ~ predictor Fixed effects only; default ~1; log link; ignored for families without a dispersion parameter.
Overdispersed or hard-to-converge counts nbinom1, nbinom2, compois The troubleshooting vignette suggests these as alternatives to genpois, which has known convergence problems.

All available families are listed in ?family_glmmTMB.

Install or access (CRAN)

glmmTMB is distributed on CRAN, with binaries for Windows and macOS. At the time of reading (2026-11-08) CRAN lists version 1.1.15.2, published 2026-09-29, which depends on R 3.6.0 or later. Installing from source requires GNU make and compiles against TMB and RcppEigen.

install.packages("glmmTMB")
library(glmmTMB)

Step-by-step: a zero-inflated count model with the Salamanders data

These steps use examples from the package reference. The Salamanders data ships with the package.

  1. Load the package: library(glmmTMB)
  2. Fit a Poisson model with a random intercept for site and zero-inflation depending on mining status:
    m1 <- glmmTMB(count ~ mined + (1|site), zi = ~mined, family = poisson, data = Salamanders)
  3. Inspect it: summary(m1)
  4. Try a negative binomial family with more predictors:
    m2 <- glmmTMB(count ~ spp + mined + (1|site), zi = ~spp + mined, family = nbinom2, data = Salamanders)
  5. For a binomial outcome, give successes and failures as a two-column response:
    data(cbpp, package = "lme4")
    glmmTMB(cbind(incidence, size - incidence) ~ period + (1|herd), family = binomial, data = cbpp)
  6. To model dispersion separately: glmmTMB(x ~ sd + (1|t), dispformula = ~sd, data = dat), then fixef(m)$disp returns the dispersion coefficients.

The formula combines fixed and random effects in lme4 syntax, so (1|site) is a random intercept by site.

Output and how to read it

summary() reports the fitted model; fixef() returns fixed-effect estimates, and the dispersion part is reached as fixef(m)$disp. The zero-inflation part uses a logit link and the dispersion part a log link, so coefficients for those components are on those scales, not the scale of the count. Before interpreting any model, confirm it converged properly: the troubleshooting vignette states that models with a non-positive definite Hessian “should be excluded from further consideration”, and that sdr$pdHess can be checked. NA or NaN standard errors, log-likelihood or AIC are the visible symptom. For count models in general see Poisson regression analysis.

Troubleshooting

From the package troubleshooting vignette (read 2026-11-08):

Warning or symptom Documented causes Documented fixes
Non-positive-definite Hessian; NA/NaN standard errors, logLik or AIC Overparameterization; singular fit with too few random-effect levels; zero-inflation or dispersion near zero; complete separation in binomial models; a dispersion model on equidispersed data Look for extreme coefficients (absolute value above 10 on a logit or log link is suspect); simplify the model; check sdr$pdHess
Eigenvalue problems Predictors on very different scales Rescale and center predictors; consider nbinom1, nbinom2 or compois instead of genpois
NA/NaN function evaluation, or Cholmod “not positive definite” Usually a transient optimizer step Ignorable if the final fit has no warnings
False convergence (nlminb) Optimizer stopped early Restart at the estimated values or try another optimizer such as optim BFGS, then confirm results are similar
NA/NaN gradient Response does not fit the family (for example negative values with nbinom) Check the response against the family
Gradient length error Model too complex for the data Rescale, simplify, then build the model up step by step

Free alternatives

Tool Relationship to glmmTMB More on CASRAI
lme4 (lmer, glmer) glmmTMB formulas use lme4 syntax, so formulas carry over lmer in R
nlme (lme) Another free R package for mixed models LME in R with nlme
afex R package for factorial ANOVA designs afex R package guide

FAQ

Does glmmTMB use the same formula syntax as lme4?

Yes. The reference states that the formula combines fixed and random effects in lme4 syntax.

How do I turn zero-inflation off or on?

The default ziformula = ~0 fits no zero-inflation. Supply a one-sided formula such as ~mined to model it, or ~. to reuse the main formula.

Where can I find worked examples?

The CRAN vignettes include Basic examples, Covariance structures, Model evaluation, Priors, Simulate and Troubleshooting.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about glmmTMB in R: Syntax, Zero-Inflation and a Salamanders Example

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →