Learn R Programming

RTMBdist — AD distribution library

The RTMB package enables powerful and flexible statistical modelling with rich random effect structures using automatic differentiation (AD). However, its built-in support for probability distributions is limited to standard cases. RTMBdist fills this gap by providing a collection of non-standard, AD-compatible distributions, extending the range of models that can be implemented and estimated with RTMB. Most of the distributions implemented in RTMBdist allow for automatic simulation and residual calculation by RTMB.

The full list of distributions currently available is given in the List of distributions vignette. There are also a couple of Worked examples demonstrating how to use RTMBdist in practice.

Feel free to contribute or open an issue if you are missing a distribution!

Installation

You can install the released version from CRAN with:

install.packages("RTMBdist")

or the development version from GitHub with:

remotes::install_github("janolefi/RTMBdist")

Introductory example

library(RTMBdist)

Let’s do numerical maximum likelihood estimation with a gumbel distribution:

# simulate data
x <- rgumbel(100, location = 5, scale = 2)

# negative log-likelihood function
nll <- function(par) {
  x <- OBS(x) # mark x as the response
  loc <- par[1]; ADREPORT(loc)
  scale <- exp(par[2]); ADREPORT(scale)
  -sum(dgumbel(x, loc, scale, log = TRUE))
}

# RTMB AD object
obj <- MakeADFun(nll, c(5, log(2)), silent = TRUE)

# model fitting using AD gradient
opt <- nlminb(obj$par, obj$fn, obj$gr)

# model summary
summary(sdreport(obj))[3:4,]
#>       Estimate Std. Error
#> loc   5.001543  0.2065935
#> scale 1.960676  0.1502500

Through the magic of RTMB, we can also immediately simulate new data from the fitted model and calculate residuals:

# simulate new data
x_new <- obj$simulate()$x

# calculate residuals
osa <- oneStepPredict(obj, method = "cdf", trace = FALSE)
qqnorm(osa$res); abline(0, 1)

Copy Link

Version

Install

install.packages('RTMBdist')

Monthly Downloads

379

Version

1.0.1

License

MIT + file LICENSE

Maintainer

Jan-Ole Fischer

Last Published

February 24th, 2026

Functions in RTMBdist (1.0.1)

gumbel

Gumbel distribution
invgauss

Inverse Gaussian distribution
dcopula

Joint density under a bivariate copula
cfrank

Frank copula constructor
cclayton

Clayton copula constructors
pareto

Pareto distribution
oibeta2

Reparameterised one-inflated beta distribution
gamma2

Reparameterised gamma distribution
genpois

Generalised Poisson distribution
dirichlet

Dirichlet distribution
dirmult

Dirichlet-multinomial distribution
betabinom

Beta-binomial distribution
powerexp

Power Exponential distribution (PE and PE2)
mvt

Multivariate t distribution
exgauss

Exponentially modified Gaussian distribution
laplace

Laplace distribution
foldnorm

Folded normal distribution
pgweibull

Power generalized Weibull distribution
t2

Student t distribution with location and scale
ddcopula

Joint probability under a discrete bivariate copula
vmf2

Reparameterised von Mises-Fisher distribution
wishart

Wishart distribution
wrpcauchy

wrapped Cauchy distribution
zero_inflate

Zero-inflated density constructer
zipois

Zero-inflated Poisson distribution
zinbinom

Zero-inflated negative binomial distribution
vm

von Mises distribution
zilnorm

Zero-inflated log normal distribution
vmf

von Mises-Fisher distribution
truncnorm

Truncated normal distribution
zinbinom2

Zero-inflated and reparameterised negative binomial distribution
oibeta

One-inflated beta distribution
zibinom

Zero-inflated binomial distribution
skewnorm2

Reparameterised skew normal distribution
skewnorm

Skew normal distribution
zigamma2

Zero-inflated and reparameterised gamma distribution
nbinom2

Reparameterised negative binomial distribution
ztnbinom2

Reparameterised zero-truncated negative binomial distribution
trunct2

Truncated t distribution with location and scale
trunct

Truncated t distribution
ziinvgauss

Zero-inflated inverse Gaussian distribution
zigamma

Zero-inflated gamma distribution
ztpois

Zero-truncated Poisson distribution
zibeta

Zero-inflated beta distribution
dmvcopula

Joint density under a multivariate copula
erf

AD-compatible error function and complementary error function
skewt

Skewed students t distribution
skewt2

Moment-parameterised skew t distribution
zoibeta

Zero- and one-inflated beta distribution
zoibeta2

Reparameterised zero- and one-inflated beta distribution
ztnbinom

Zero-truncated Negative Binomial distribution
ztbinom

Zero-truncated Binomial distribution
zibeta2

Reparameterised zero-inflated beta distribution
bcpe

Box-Cox Power Exponential distribution (BCPE)
abs_smooth

Smooth approximation to the absolute value function
beta2

Reparameterised beta distribution
cgaussian

Gaussian copula constructor
bct

Box–Cox t distribution (BCT)
cgmrf

Multivariate Gaussian copula constructor parameterised by inverse correlation matrix
bccg

Box–Cox Cole and Green distribution (BCCG)
cgumbel

Gumbel copula constructors
cmvgauss

Multivariate Gaussian copula constructor