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() ()
    {
      DATA_VECTOR(Y);
      DATA_VECTOR(x);
      PARAMETER(a);
      PARAMETER(b);
      PARAMETER(logSigma);
      ADREPORT(exp(2*logSigma));
      Type nll = -sum(dnorm(Y, a+b*x, exp(logSigma), true));
      return nll;
    }
  • Controlling R code

    library(TMB)
    compile("linreg.cpp")
    dyn.load(dynlib("linreg"))
    set.seed(123)
    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
    opt$hessian ## <-- FD hessian from optim
    obj$he()    ## <-- Analytical hessian
    sdreport(obj)
  • To run this example use the R command

    source("linreg.R")

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
fft.cpp Multivariate normal distribution with circulant covariance
hmm.cpp Inference in a ‘double-well’ stochastic differential equation using HMM filter.
laplace.cpp Laplace approximation from scratch demonstrated on ‘spatial’ example.
linreg_parallel.cpp Parallel linear regression.
linreg.cpp Simple 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.
mvrw_sparse.cpp Identical with random walk example. Utilizing sparse block structure so efficient when the number of states is high.
mvrw.cpp Random walk with multivariate correlated increments and measurement noise.
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)
register_atomic_parallel.cpp Parallel version of ‘register_atomic’
register_atomic.cpp Similar to example ‘adaptive_integration’ using CppAD Romberg integration. REGISTER_ATOMIC is used to reduce tape size.
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_compact.cpp Compact version of sdv_multi
sdv_multi.cpp Multivatiate SV model from Skaug and Yu 2013, Comp. Stat & data Analysis (to appear)
socatt.cpp socatt from ADMB example collection.
spatial.cpp Spatial poisson GLMM on a grid, with exponentially decaying correlation function
spde_aniso_speedup.cpp Speedup “spde_aniso.cpp” by moving normalization out of the template.
spde_aniso.cpp Anisotropic version of “spde.cpp.”
spde.cpp Illustration SPDE/INLA approach to spatial modelling via Matern correlation function
thetalog.cpp Theta logistic population model from Pedersen et al 2012, Ecol. Modelling.
TMBad/interpol.cpp Demonstrate 2D interpolation operator
TMBad/sam.cpp State space assessment model from Nielsen and Berg 2014, Fisheries Research.
TMBad/solver.cpp Demonstrate adaptive solver of TMBad
TMBad/spa_gauss.cpp Demonstrate saddlepoint approximation (SPA)
TMBad/spatial.cpp Spatial poisson GLMM on a grid, with exponentially decaying correlation function
TMBad/spde_epsilon.cpp Low-level demonstration of fast epsilon bias correction using ‘sparse plus lowrank’ hessian
TMBad/thetalog.cpp Theta logistic population model from Pedersen et al 2012, Ecol. Modelling.
transform_parallel.cpp Parallel version of transform
transform.cpp Gamma distributed random effects using copulas.
transform2.cpp Beta distributed random effects using copulas.
tweedie.cpp Estimating parameters in a Tweedie distribution.
validation/MVRandomWalkValidation.cpp Estimate and validate a multivariate random walk model with correlated increments and correlated observations.
validation/randomwalkvalidation.cpp Estimate and validate a random walk model with and without drift
validation/rickervalidation.cpp Estimate and validate a Ricker model based on data simulated from the logistic map