1 of 32

MAR 580: Models for Marine Ecosystem-Based Management

TMB workshop

Session II

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

06 September 2022

2 of 32

Schedule

gavinfay.github.io/mebm-models/tmb

Rstudio Cloud project�

Session I Questions?

3 of 32

When should I use TMB?

  • TLDR: when you need to.
  • Several packages that leverage TMB for calculations but don’t require you to program the C++ (knowing TMB might help with debugging).
    • {glmmTMB}
    • {sdmTMB}
    • {VAST}
  • TMB (or other custom model) might be needed for nonlinear latent variable dynamic models, and/or multiple observation models (multiple data types). TMB very good for models with lots of random effects (e.g. spatial models)
  • If you can dream it (and write your objective function), TMB can fit it?

4 of 32

When are numerical methods used?

Main types of problems:

  • Differentiation
  • Integration
  • Interpolation / Extrapolation
  • Roots of equations
  • Solution of simultaneous equations
  • Nonlinear minimization
  • Matrix problems (nothing to do with Neo)

5 of 32

Calculus

  • Differentiation
    • How steep is the curve at point x?
  • Integration
    • How much area is under the�curve between a and b?

a

b

x

6 of 32

Numerical integration

  • We wish to evaluate the integral:

7 of 32

Numerical integration: single step methods

  • We could approximate the integral by its height at its first point, at its last point, or at the middle.

  • Approximating the integral by the average of the first two approaches is the “Trapezoidal rule”.

8 of 32

Numerical integration

  • Trapezoidal rule: 0.68394
  • Simpson’s rule: 0.74718
  • Boole’s rule: 0.74683
  • Exact: 0.74682!

9 of 32

The error function, or how I learned to stop worrying & use someone else’s code.

  • Commonly required integral in fisheries is the “error function”:

  • Very accurate approximations to this integral have been programmed and are available in software libraries (e.g. NORMDIST in EXCEL, cumd_norm in ADMB, pnorm in R).
  • Someone may have already solved your problem; before writing your own code check if this is the case!

10 of 32

Mixed effects models

11 of 32

Linear modeling review LMs, GLMs, NLMs

  • LM and GLM are used to describe whether and how a response variable depends on a combination of predictors.
  • Predictors can be numeric (continuous) or factors (categorical).

  • Linear

Quite flexible as it is

  • Generalized

Any error distribution

  • Nonlinear

Any relationship between Y and X

closed�form�solution

iterative�solution

12 of 32

What are Fixed and Random Effects?

  • Fixed Effects:
    • Parameters associated with the entire population or with certain repeatable levels of experimental units.
  • Random Effects:
    • Parameters associated with individual experimental units drawn at random from a population.
  • A mixed effects model is a model that has both fixed effects and random effects.

  • Classical (frequentist) and Bayesian methods can be used to estimate the parameters of mixed effects models.
  • Often the same random effect is assigned to observations sharing a common classification factor:
    • Consider length-at-age data for several (randomly selected) stocks of a species. The random factor is stock and the observations are length-at-age data.
    • Random effects can the thought of as ways of modeling the covariance structure of the data.

13 of 32

Linear Mixed Effects Models

Vector of fixed effects

Vector of random effects

Observations for group i

The traditional linear modeling framework is a special case of

of this model in which there are no random effects.

14 of 32

Example 1: Streams�(sensu Pinherio and Bates, Chapter 1)

  • We are estimating fish density in streams.
  • We select six streams at random and determine the density at each stream three times.
  • The questions:
    • What is the density in a typical stream?
    • What is the variation in density among streams?
    • What is the variation in density estimates within a stream?
  • We would like to separately �estimate measurement �and between-site �variability.

15 of 32

Example 1: Streams mixed effects model

  • We construct a mixed effects model as follows:

  • β is the mean density across the population,
  • bi is a random variable representing the deviation from the population mean for stream i

  • εi,j is a random variable representing the deviation for observation j from mean density for stream i, i.e.

16 of 32

Fitting linear mixed effects models

These estimates are similar to the linear model with separate “stream effects” (but would not be if this was an unbalanced design).

17 of 32

Mixed Effects Models (Some notes)

  • Residuals are assumed to be independent, normally distributed random variables with constant variance.
  • Random effects are assumed to be normally distributed.
  • This only works if the streams have been selected at random from the population of streams.
  • Mixed effects models are also called hierarchical models.
  • This model has 3 parameters (β, σ and σb).
  • There are ways to estimate the random effects (we will often be interested in their values).

18 of 32

Estimation of mixed effects models

Maximum Likelihood Estimation (MLE)

is the vector of fixed effects parameters.

are the parameters controlling the distribution for the random effects.

We integrate across the random effects.

Effectively weight the probability of the observations given values for random effects by the probability of those values.

In the linear case, closed form solutions exist. For nonlinear models, we evaluate numerically or use approximations.

19 of 32

Estimating using R

  • lme() in package nlme, lmer() in lme4.
  • nlme() used to fit nonlinear models
  • The call to lme() first gives the fixed effects (the intercept in this case), and then the random component of the model.

20 of 32

lme() output

among stream variation

within stream variation

fixed effects

21 of 32

Extending the linear mixed model

  1. A non-linear relationship between the response variable and the covariates:

  • A non-normal distribution for the random effects.
  • A non-normal distribution for the within-group variability.

22 of 32

Non-linear Mixed Effects Models

  1. Modeling age and length data for animals that are measured multiple times.
  2. A meta-analysis for the parameters of the stock-recruitment relationship for many stocks.
  3. A surplus production model with process and observation error.
  4. An age-structured population dynamics model with recruitment variability (Stock Synthesis).

23 of 32

Fitting non-linear mixed effects models

  • R can be used to fit non-linear mixed effects models (function nlme).
  • To fit non-linear mixed effects models using TMB or EXCEL you need to evaluate the integral numerically, or apply a method such as the Laplace approximation.

  • is the value of ε obtained by maximizing�given fixed values of θ and τ, and is the determinant of the matrix of second derivatives.

  • This approximation is exact when the distribution of random effects (given their hyper-prior and the available data) is exactly proportional to a multivariate normal distribution.�(It is a convenient approximation in other cases)

Skaug & Fournier (2006), Thorson & Minto (2015)

24 of 32

Random effects in TMB

  • In MakeADFun, we add argument random
  • Names of parameters that are to be considered random effects
  • e.g. random=“stream”

  • Need to make sure we add the contribution to the likelihood function for the random effects in our .cpp file.

25 of 32

Streams model

26 of 32

Exercise: Poisson GLMM�Summer flounder recreational catch per trip

We have data on the number of flounder caught on observed samples of angling trips across the Atlantic states. The number of sampled trips is uneven by state.

  • “summer_flounder_catch_per_trip.csv”

Fit a mixed-effects model for the catch per trip by state. Assume that the number of fish caught on a trip is Poisson distributed, with an intercept and a state-specific random effect.�Report values for estimated parameters and their variance. Compare the fit to a model with a single intercept and no random effects for state.

27 of 32

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!

28 of 32

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

29 of 32

30 of 32

31 of 32

32 of 32