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
Functions
e.g. ‘square()’
template <class Type> Type square(Type x){return x*x;}
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;
Likelihood profile
prof <- tmbprofile(model,”par”)
print confint(prof, level = 0.95)
namespaces, Multivariate normal
e.g. multivariate normal
using namespace density;
MVNORM_t<Type> neg_log_dmvnorm(Sigma);
nll += neg_log_dmvnorm(residual);
simple MVNORM example
Spatial random effects example
coords <- tibble(x = rep(1,9), y = 1:9)
dd <- as.matrix(dist(coords,
upper = TRUE,
diag = TRUE),nrow=9)
Spatial random effects example
Exercise: Growth estimation from lengths at release and recapture
Parameters:
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
Exercise: fitting population model