Download presentation
Presentation is loading. Please wait.
1
Template Model Builder-I
Fish 559; Lecture 13 a
2
Template Model Builder
TMB is alternative to ADMB that uses a model written in a C++ structure, along with the minimization and plotting features of R. TMB appears to be a more efficient way to implement mixed effects models.
3
Installing TMB TMB is available is from githib:
Download the ZIP file and UNZIP it to a folder call “adcomp”. You can clone the web-site using: git clone Go into R and source the install.windows.R routine. Do not download TMB from CRAN (if you are using Windows). Hint: You may need to install Rtools (if did – do this FIRST)
4
The R link into things List of Data List of Estimated Parameters
setwd(“D:\\courses\\FISH 559_16\\Tmb\\") data <- list(x=rivers) parameters <- list(mu=0,logSigma=0) require(TMB) compile('mini.cpp') dyn.load(dynlib('mini')) model <- MakeADFun(data,parameters) fit <- nlminb(model$par, model$fn, model$gr) rep <- sdreport(model) List of Data List of Estimated Parameters Create a DLL Standard call to nlminb This code can be found in “mini.R”
5
C++ Code (mini.cpp) The Data Estimated parameters Objective function
#include <TMB.hpp> template<class Type> Type objective_function<Type>::operator() () { DATA_VECTOR(x); PARAMETER(mu); PARAMETER(logSigma); Type f; f = -sum(dnorm(x,mu,exp(logSigma), true)); return f; } The Data Estimated parameters Objective function
6
Overview of the Process (see STREAMS.CCP / STREAMS.R)
Write the model and likelihood in XX.CPP Define the data inputs as a list and the parameters as a list Compile the cpp file. Construction a model object using “MakeADFun” Minimize the function (e.g. using nlminb) Look at the results.
7
Understanding MakeADFun
data: the data to be passed to the function (as a list) parameters: a list of estimable parameters (including random and fixed effects) map: provides a way to fix parameters (not estimate, or make parameters equal) random: which variables are random DLL: name of the DLL
8
Two phases (and map) NA here means fixed ## Phase 1
map <- list(u=factor(rep(NA,data$nG)),log_sigma=factor(NA)) obj <- MakeADFun(data,parameters,map=map,DLL="nmix") opt <- nlminb(obj$par,obj$fn,obj$gr) pl <- obj$env$parList(opt$par) ## Parameter estimate after phase 1 ## Phase 2 obj <- MakeADFun(data,pl,random="u",DLL="nmix")
9
More on map Lets say you have an vector beta and you want the 2nd and 4th elements to be same. You can achieve this using: map(beta=factor(c(1,2,3,2)))
10
Sdreport (see Schaefer.cpp)
By definition TMB will return asymptotic variances for the parameters. These can be viewed using sdreport(obj). To compute standard errors for derived quantities, you need to define an ADREPORT variable. Type gamma = alpha*alpha; ADREPORT(gamma);
11
Getting variance information
To extract the variance-covariance matrix for “model”: solve(model$he()) To extract the correlation matrix for “model” cov2cor(solve(model$he())) To extract the standard errors for “model” sqrt(diag(solve(model$he()))))
12
Reporting-I To simply report results back to R (no variances), use
Type delta = alpha*alpha*alpha; REPORT(delta); To access the results being reported back to R use: obj$report();
13
Reporting-II To access the parameter estimates (fixed and random) use:
rep <- sdreport(obj) summary(rep,select=“fixed”) summary(rep,select=“random”) xx <- summary(rep) Use <- row.names(xx) == "SSB" SSB <- xx[Use,]
14
Data types For passing data DATA_VECTOR(x) DATA_MATRIX(x)
DATA_SCALAR(x) DATA_INTEGER(x) DATA_FACTOR(x) or DATA_IVECTOR(x) DATA_SPARSE_MATRIX(x) DATA_ARRAY(x) Important note: ALL arrays start at 0.
15
Estimable parameters PARAMETER(x) PARAMETER_VECTOR(x)
PARAMETER_MATRIX(x) PARAMETER_ARRAY(x)
16
Local variables Type p; [scalar] vector<Type> q(5); [vector]
matrix<Type> z(5,5); [matrix] array<Type>k(5,5,5); [array] int I; [integer] vector<int>jj(5); [integer vector]
17
3d arrays This is a little complicated – you first declare a vector of
matrices: vector<matrix<Type> > x(3); Then declare the matrices themselves: matrix<Type> Temp(3,4); Then assign the matrices to each element: X(1) = Temp; x(2) = Temp;
18
Loops and if statement TMB includes the standard “for” loop.
for(int i=0;i<n;i++) statements Note that the loop starts at 0. The “if” statement is: if (x>=y) statements As usual, try to avoid “ifs” that involve parameters or derived variables
19
Example application The file Lect13.Dat lists the sex, age and length of 100 individuals. You need to fit the von Bertalanffy growth curve to these data. Write one cpp file based on the most general model (all three parameters depend on sex). Now use map to fit the following three sub-models: t0 is zero for both sexes is the same for males and females All three parameters are the same for males and females Also: Plot the fits of each sub-model to the data Compute AIC for each sub-model Estimate the length of an individual of sex 1 and age 10 from the best model, and report its standard error.
20
Getting more information
The Wiki on the web-site provides several useful hints, especially for people who are already familiar with R and ADMB The snippets are really helpful! Look at how to create packages
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.