RTMB: An R Package for Fitting Latent Variable Models With Template Model Builder

Hierarchical and latent variable models are fundamental in ecology, fisheries science, and epidemiology, but fitting them efficiently relies on automatic differentiation (AD) combined with approximations such as the Laplace approximation. Template Model Builder (TMB) has become a standard tool for this kind of inference in R, but it requires users to write the objective function in C++, which is a potential obstacle for those unfamiliar with it, slows model iteration through compilation, and complicates debugging and simulation. We present RTMB, an R package that exposes the full power of TMB's AD machinery directly from plain R. Users write the joint negative log-likelihood as an ordinary R function; RTMB traces it onto an AD tape and hands the result to TMB's existing optimization, Laplace approximation, and reporting routines. We describe the design of the package, illustrate its use through a worked example, and provide a side-by-side comparison with the equivalent TMB implementation of a theta-logistic state-space population model. The result is a transparent, debuggable, and pedagogically accessible interface to state-of-the-art latent variable inference, with runtime performance comparable to native TMB.

Authors

Institutions

Publication Details

Journal
Ecology and Evolution
Published
2026-10-29
DOI
https://doi.org/10.1002/ece3.74572
Primary Topic
Data Analysis with R
Type
article
Field-Weighted Citation Impact
0.00

Funders

Controls
|||
ALL TIME
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
article

RTMB: An R Package for Fitting Latent Variable Models With Template Model Builder

Anders Nielsen, Casper Willestofte Berg, Vanessa Trijoulet, Kasper Kristensen et al.
Ecology and Evolution
Data Analysis with R
article

RTMB: An R Package for Fitting Latent Variable Models With Template Model Builder

Anders Nielsen, Casper Willestofte Berg, Vanessa Trijoulet, Kasper Kristensen, Christoffer Moesgaard Albertsen
article en

Abstract

Hierarchical and latent variable models are fundamental in ecology, fisheries science, and epidemiology, but fitting them efficiently relies on automatic differentiation (AD) combined with approximations such as the Laplace approximation. Template Model Builder (TMB) has become a standard tool for this kind of inference in R, but it requires users to write the objective function in C++, which is a potential obstacle for those unfamiliar with it, slows model iteration through compilation, and complicates debugging and simulation. We present RTMB, an R package that exposes the full power of TMB's AD machinery directly from plain R. Users write the joint negative log-likelihood as an ordinary R function; RTMB traces it onto an AD tape and hands the result to TMB's existing optimization, Laplace approximation, and reporting routines. We describe the design of the package, illustrate its use through a worked example, and provide a side-by-side comparison with the equivalent TMB implementation of a theta-logistic state-space population model. The result is a transparent, debuggable, and pedagogically accessible interface to state-of-the-art latent variable inference, with runtime performance comparable to native TMB.

Ecology and EvolutionVol. 16(11)
Technical University of Denmark (DK)
Danmarks Tekniske Universitet
Openalex Percentile: Top 13%
Data Analysis with R
AI Navigator

Ask Laika to Summarize, Analyze, and Connect papers live on the map.

Summarize Papers & Methodologies

Extract key findings, datasets, and comparative methods across publications.

Benchmark Rankings & Visual Analytics

Rank top research institutions, authors, funders, topics, and journals by Field-Weighted Citation Impact (FWCI) and paper volume with instant charts.

Connect Distant Disciplines

Bridge topological clusters on the map to find hidden collaborative intersections.

Laika access coming soon
RTMB: An R Package for Fitting Latent Variable Models With Template Model Builder — Anders Nielsen, Casper Willestofte Berg, et al. · Ecology and Evolution (2026) | TGRS Research Map | TGRS