-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMB_Gomp.R
65 lines (46 loc) · 1.88 KB
/
TMB_Gomp.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Load and install necessary packages
install.packages("TMB")
library(TMB)
library(devtools)
install_github("kaskr/TMB_contrib_R/TMBhelper")
library(TMBhelper)
tmb_model="
// Gompertz growth function
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() () {
// DATA_SECTION
DATA_VECTOR(Age); //Vector of age data
DATA_VECTOR(Length); //Vector of length data
int n = Age.size(); //Number of individuals
// PARAMETER_SECTION
PARAMETER(Linf); // Asymptoptic length
PARAMETER(G); // Instantaneous rate of growth
PARAMETER(t0); // Inflection point of the curve
PARAMETER(logSigma); // Fit sigma on a log scale to keep it > 0
// PRELIMINARY_CALCS_SECTION: (Transformed parameters)
Type sigma = exp(logSigma);
ADREPORT(sigma)
// PROCEDURE_SECTION
vector<Type> La_i(n);
La_i = Linf*exp(-exp(-G*(Age-t0))); //Gompertz growth model
Type nll = 0.0; // Initialize negative log-likelihood
for(int i = 0; i < n; i++){ // C++ starts loops at 0!
// negative log likelihood
nll -= dnorm(Length[i], La_i[i], sigma, TRUE);
}
return nll;
}"
#Write model to C++
write(tmb_model, file = "NLL_Gompertz.cpp")
dyn.load(dynlib("NLL_Gompertz"))#Load object file
#Run the model
parameters<-list(Linf=650,G=0.4,t0=0,logSigma=5) #Starting values
obj = MakeADFun(data, parameters, DLL = "NLL_Gompertz",silent=TRUE) #Prepare model objects for fitting
Gomp_opt = nlminb(start=obj$par, obj=obj$fn, gr=obj$gr) #Fit model using nlminb optimizer (quasi-Newton method)
#Likelihood profiles
Linf_CI_G=confint(tmbprofile(obj,"Linf",trace=FALSE))
G_CI_G=confint(tmbprofile(obj,"G",trace=FALSE))
t0_CI_G=confint(tmbprofile(obj,"t0",trace=FALSE))
logSigma_CI_G=confint(tmbprofile(obj,"logSigma",trace=FALSE))
(fit_Gomp = summary(sdreport(obj))) #Summary of parameter estimates, Std. Errors, and 95% Confidence Intervals