9 Toolbox

First read the Statistical Modelling section of Tutorial.

9.0.1 Non-normal latent variables (random effects)

The underlying latent random variables in TMB must be Gaussian for the Laplace approximation to be accurate. To obtain other distributions, say a gamma distribution, the “transformation trick” can be used. We start out with normally distributed variables u and transform these into new variables w via the pnorm and qgamma functions as follows:

PARAMETER_VECTOR(u);                             // Underlying latent random variables 
Type nll=Type(0.0);
nll -= sum(dnorm(u,Type(0),Type(1),true));       // Assign N(0,1) distribution u 
vector<Type> v = pnorm(u,Type(0),Type(1));  // Uniformly distributed variables (on [0,1])
vector<Type> w = qgamma(v,shape,scale);

w now has a gamma distribution.

9.0.2 Discrete latent variables

The Laplace approximation can not be applied to discrete latent variables that occur in mixture models and HMMs (Hidden Markov models). However, such likelihoods have analytic expressions, and may be coded up in TMB. TMB would still calculate the exact gradient of the HMM likelihood.

9.0.3 Mixture models

Although mixture models are a special case of discrete latent variable models, they do deserve special attention. Consider the case that we want a mixture of two zero-mean normal distributions (with different standard deviations). This can be implemented as:

DATA_VECTOR(x);              			
PARAMETER_VECTOR(sigma);      // sigma0 og sigma1
PARAMETER(p);                 // Mixture proportion of model 0
Type nll=Type(0.0);
nll -= sum( log(      p  * dnorm(x, Type(0), sigma(0), false)
               + (1.0-p) * dnorm(x, Type(0), sigma(1), false) ) );

9.0.4 Time series

Autoregressive (AR) processes may be implemented using the compact notation of section Densities. The resulting AR process may be applied both in the observational part and in the distribution of a latent variable.

Nonlinear time must be implemented from scratch, as in the example thetalog.cpp

9.0.5 Spatial models

TMB has strong support for spatial model and space-time models via the GMRF() and SEPARABLE() functions, and the notion of a distribution. The reader is referred to section Densities for details and examples.