1 of 11

MAR 580: Models for Marine Ecosystem-Based Management

TMB workshop

Session IV

(Acknowledgements: Mollie Brooks, Kasper Kristensen, Arni Magnusson, Anders Nielsen, André Punt)

08 September 2022

2 of 11

Functions

  • We sometimes like to re-use chunks of our own code that perform a common task
  • Solution: put this code in a function!
  • Call the function rather than writing the code each time.
  • In TMB, we can add functions via separate files (that we point to), or include them before the objective function object definition.

e.g. ‘square()’

template <class Type> Type square(Type x){return x*x;}

3 of 11

posfun(). (constrain a quantity to be positive)

Type posfun(Type x, Type eps, Type &pen){

pen += CppAD::CondExpLt(x, eps, Type(0.01) * pow(x-eps,2), Type(0));

return CppAD::CondExpGe(x, eps, x, eps/(Type(2)-x/eps));

}

Using it in the model:

number<Type> bpen = 0.;

biomass(year) = biomass(year-1) + posfun(biomass(year-1)*r*(1.- biomass(year-1)/K),0.1,bpen);

nll += 1000*bpen;

4 of 11

Likelihood profile

  • We obtained estimates of variance this morning by asymptotic methods
  • TMB can also do likelihood profiles

prof <- tmbprofile(model,”par”)

print confint(prof, level = 0.95)

5 of 11

namespaces, Multivariate normal

  • TMB comes with many probability distributions

  • However there are others we can use by loading different namespaces

e.g. multivariate normal

using namespace density;

MVNORM_t<Type> neg_log_dmvnorm(Sigma);

nll += neg_log_dmvnorm(residual);

6 of 11

simple MVNORM example

7 of 11

Spatial random effects example

  • Extending the Poisson GLMM
  • We might expect summer flounder catch rates to be spatially correlated
    • e.g. neighboring states have similar catch rates
  • Add simple neighborhood-based correlation structure to our Poisson GLMM
  • e.g. Corr(i,j) = e-⍺d(i,j)
  • Distance matrix:

coords <- tibble(x = rep(1,9), y = 1:9)

dd <- as.matrix(dist(coords,

upper = TRUE,

diag = TRUE),nrow=9)

8 of 11

Spatial random effects example

  • Modifying the GLMM
  • .R:
    • Need to add the distance matrix to our data object
    • Add correlation and spatial variance parameters
  • .cpp:
    • Add code to calculate the Variance Covariance matrix given the distance matrix and the correlation/variance parameters
    • Include the contribution for the spatial random effects (using the multivariate normal)

9 of 11

Exercise: Growth estimation from lengths at release and recapture

  • Data set of bluegill recapture lengths
  • head(FSAdata::BluegillIL)

  • Laslett et al. (2002, CJFAS)
    • ‘LEP’ method of estimating growth
    • Fit to Lengths at release/recapture instead of growth increment, time at liberty
    • Challenge: Growth models include Age, but these are not known!
    • Solution: Integrate across the age at release using random effects!

10 of 11

Parameters:

  • VBK�Linf_mu
  • sigma_Linf
  • Sigma_obs
  • A_i (random effect)
  • Sigma_A
  • Mu_A

Data

Length 1

Length 2

dt

For each fish:

Mu_L1 (eq 3.5a)

Mu_L2 (eq 3.5b)

Sigma_L1 (eq 3.5c)

Sigma_L2 (eq 3.5d)

Cov_L1L2 (eq 3.5e)

MVNORM LIKELIHOOD

11 of 11

Exercise: fitting population model

  • Fit a dynamic Schaefer model to the Catch and Survey abundance data for Pleuronectes electronica
  • Step 1: Draw the model graph with relationships between variables
  • Step 2: Create a TMB model to fit to the available data
    • Assume the logs of both the catch data and survey indices are normally distributed with respect to their mean (model-predicted) value.
  • 3: Fit the model, plot the model fits, population trajectory
  • 4: Compute a likelihood profile for the growth rate r