7 Example collection

  • A list of all examples is found on the “Examples” tab on the top of the page.

  • Locations of example files: adcomp/tmb_examples and adcomp/TMB/inst/examples.

  • For each example there is both a .cpp and a .R file. Take for instance the linear regression example:

  • C++ template

    // Simple linear regression.
    #include <TMB.hpp>
    template<class Type>
    Type objective_function<Type>::operator() ()
      Type nll = -sum(dnorm(Y, a+b*x, exp(logSigma), true));
      return nll;
  • Controlling R code

    data <- list(Y = rnorm(10) + 1:10, x=1:10)
    parameters <- list(a=0, b=0, logSigma=0)
    obj <- MakeADFun(data, parameters, DLL="linreg")
    obj$hessian <- TRUE
    opt <- do.call("optim", obj)
    opt$hessian ## <-- FD hessian from optim
    obj$he()    ## <-- Analytical hessian
  • To run this example use the R command


7.1 Example overview

Example Description
adaptive_integration.cpp Adaptive integration using ‘tiny_ad’
ar1_4D.cpp Separable covariance on 4D lattice with AR1 structure in each direction.
compois.cpp Conway-Maxwell-Poisson distribution
hmm.cpp Inference in a ‘double-well’ stochastic differential equation using HMM filter.
laplace.cpp Laplace approximation from scratch demonstrated on ‘spatial’ example.
linreg.cpp Simple linear regression.
linreg_parallel.cpp Parallel linear regression.
longlinreg.cpp Linear regression - 10^6 observations.
lr_test.cpp Illustrate map feature of TMB to perform likelihood ratio tests on a ragged array dataset.
matern.cpp Gaussian process with Matern covariance.
MVRandomWalkValidation.cpp Estimate and validate a multivariate random walk model with correlated increments and correlated observations.
mvrw.cpp Random walk with multivariate correlated increments and measurement noise.
mvrw_sparse.cpp Identical with random walk example. Utilizing sparse block structure so efficient when the number of states is high.
nmix.cpp nmix example from https://groups.nceas.ucsb.edu/non-linear-modeling/projects/nmix
orange_big.cpp Scaled up version of the Orange Tree example (5000 latent random variables)
randomwalkvalidation.cpp Estimate and validate a random walk model with and without drift
register_atomic.cpp Similar to example ‘adaptive_integration’ using CppAD Romberg integration. REGISTER_ATOMIC is used to reduce tape size.
register_atomic_parallel.cpp Parallel version of ‘register_atomic’
rickervalidation.cpp Estimate and validate a Ricker model based on data simulated from the logistic map
sam.cpp State space assessment model from Nielsen and Berg 2014, Fisheries Research.
sde_linear.cpp Inference in a linear scalar stochastic differential equation.
sdv_multi.cpp Multivatiate SV model from Skaug and Yu 2013, Comp. Stat & data Analysis (to appear)
sdv_multi_compact.cpp Compact version of sdv_multi
socatt.cpp socatt from ADMB example collection.
spatial.cpp Spatial poisson GLMM on a grid, with exponentially decaying correlation function
spde.cpp Illustration SPDE/INLA approach to spatial modelling via Matern correlation function
spde_aniso.cpp Anisotropic version of “spde.cpp.”
spde_aniso_speedup.cpp Speedup “spde_aniso.cpp” by moving normalization out of the template.
thetalog.cpp Theta logistic population model from Pedersen et al 2012, Ecol. Modelling.
transform.cpp Gamma distributed random effects using copulas.
transform_parallel.cpp Parallel version of transform
transform2.cpp Beta distributed random effects using copulas.
tweedie.cpp Estimating parameters in a Tweedie distribution.