Bayesian Inference Using 'RTMB'

Provides tools for Markov chain Monte Carlo (MCMC) and Maximum A Posteriori (MAP) estimation utilizing the 'RTMB' package. It supports various statistical models including generalized linear mixed models, factor analysis, item response theory, and multidimensional unfolding. The package allows users to easily transition between frequentist and Bayesian paradigms using a unified interface. Automatic differentiation and Laplace approximation follow Kristensen et al. (2016) , and MCMC sampling uses the No-U-Turn Sampler described by Hoffman and Gelman (2014) < https://jmlr.org/papers/v15/hoffman14a.html>.


BayesRTMB

Language / 言語: English introduction | パッケージ紹介

BayesRTMB is an R package for writing and fitting statistical models with RTMB as the automatic differentiation engine.

You can start from wrapper functions such as rtmb_lm(), rtmb_glmer(), rtmb_corr(), and rtmb_ttest(), or write your own model with rtmb_code(). The same model object can then be used for MCMC, MAP estimation, variational inference, and frequency-oriented classical analyses where supported.

Key Features

  • Model code in R: Write models with setup, parameters, transform, model, and generate blocks.
  • Multiple estimation methods: Use $sample(), $optimize(), $variational(), and $classic() from a common model object.
  • Wrapper functions: Fit regression models, GLM/GLMMs, t tests, correlations, contingency tables, factor analysis, IRT, latent rank models, and multidimensional unfolding models.
  • Classical analyses from wrappers: Use $classic() for frequency-oriented outputs, including AIC(), BIC(), and anova() for supported fits.
  • Random effects and Laplace approximation: Declare parameters with random = TRUE and use RTMB’s Laplace machinery for latent variables and mixed models.
  • Diagnostics and visualization: Plot posterior draws, convergence diagnostics, forest plots, conditional effects, and related summaries.

Installation

You can install BayesRTMB from CRAN.

install.packages("BayesRTMB")

The development version can be installed from GitHub with either pak or remotes.

pak::pak("norimune/BayesRTMB")
remotes::install_github("norimune/BayesRTMB")

Windows Users

For ordinary use, Windows users can install the CRAN binary package without Rtools. Rtools is only needed for source installation, development, or compiling custom TMB C++ templates.

pkgbuild::check_build_tools(debug = TRUE)

If you install BayesRTMB from source and this check fails, install the Rtools version that matches your R version from the Rtools page, restart R, and try again.

Quick Example

For standard analyses, start with a wrapper function.

library(BayesRTMB)
data(debate)

mdl <- rtmb_lm(sat ~ talk * perf, data = debate)

fit_mcmc <- mdl$sample()
fit_map  <- mdl$optimize()
fit_lm   <- mdl$classic()

You can also write a model directly.

Y <- debate$sat
X <- debate[c("talk","perf")] |> as.matrix()

data_list <- list(Y = Y, X = X)

code <- rtmb_code(
  setup = {
    N <- length(Y)
    K <- ncol(X)
  },
  parameters = {
    Intercept <- Dim(1)
    b <- Dim(K)
    sigma = Dim(lower = 0)
  },
  model = {
    mu <- Intercept + X %*% b
    Y ~ normal(mu, sigma)
    Intercept ~ normal(0, 10)
    b ~ normal(0, 10)
    sigma ~ exponential(1)
  }
)

mdl_custom <- rtmb_model(data_list, code)
fit_custom <- mdl_custom$sample()

Articles

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("BayesRTMB")

0.4.0 by Hiroshi Shimizu, 25 days ago


https://github.com/norimune/BayesRTMB, https://norimune.github.io/BayesRTMB/


Report a bug at https://github.com/norimune/BayesRTMB/issues


Browse source code at https://github.com/cran/BayesRTMB


Authors: Hiroshi Shimizu [aut, cre]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports R6, MASS

Depends on RTMB

Suggests knitr, rmarkdown, GPArotation, future


See at CRAN