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!
You can install the development version of RTMBdist
from
GitHub with:
::install_github("janoleko/RTMBdist") remotes
library(RTMBdist)
Let’s do numerical maximum likelihood estimation with a
gumbel
distribution:
# simulate data
<- rgumbel(100, location = 5, scale = 2)
x
# negative log-likelihood function
<- function(par) {
nll <- OBS(x) # mark x as the response
x <- par[1]; ADREPORT(loc)
loc <- exp(par[2]); ADREPORT(scale)
scale -sum(dgumbel(x, loc, scale, log = TRUE))
}
# RTMB AD object
<- MakeADFun(nll, c(5, log(2)), silent = TRUE)
obj
# model fitting using AD gradient
<- nlminb(obj$par, obj$fn, obj$gr)
opt
# 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
<- obj$simulate()$x
x_new
# calculate residuals
<- oneStepPredict(obj, method = "cdf", trace = FALSE)
osa qqnorm(osa$res); abline(0, 1)