Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
added in class-example code
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Apr 12, 2018
1 parent 893aef9 commit 90335d1
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 3 -- Temporal Models/Lab/In-class exercise" )

######################
# Modify to add sampling variance to measurement error
######################

# Modify to decompose measurement error into sampling and additional error
sd_B_t = sqrt( tapply( CPUE[,'Wt'], INDEX=CPUE[,'Year'], FUN=var ) / tapply( CPUE[,'Wt'], INDEX=CPUE[,'Year'], FUN=length ) )
CV_b_t = sd_B_t / B_t
SD_log_b_t = sqrt(log( CV_b_t^2 + 1 ))

# Compile model
Version = "gompertz_2"
compile( paste0(Version,".cpp") )

# Build inputs
Data = list( "nt"=length(B_t), "log_b_t"=log(B_t), "SD_log_b_t"=SD_log_b_t )
Parameters = list( "log_d0"=0, "log_sigmaP"=1, "log_sigmaM"=1, "alpha"=0, "rho"=0, "log_d_t"=rep(0,Data$nt) )
Random = c("log_d_t")
if( Use_REML==TRUE ) Random = union( Random, c("log_d0","alpha","rho") )

# Build object
dyn.load( dynlib("gompertz_2") )
Obj = MakeADFun(data=Data, parameters=Parameters, random=Random, DLL="gompertz_2") #
Opt2 = TMBhelper::Optimize( obj=Obj )

# Plot again
for( z in 1:2 ){
png( file=paste0("pollock_",z,"-with_measurement_error.png"), width=8, height=5, res=200, units="in" )
par( mar=c(3,3,1,1), mgp=c(2,0.5,0), tck=-0.02 )
plot( x=1:Data$nt, y=Data$log_b_t, col="blue", cex=1.2, xlab="Time", ylab="Value", pch=20 )
if(z>=2){
points( x=1:Data$nt, y=as.list(Opt2$SD,"Estimate")$log_d_t, col="red" )
for( t in 1:Data$nt) lines( x=rep(t,2), y=as.list(Opt2$SD,"Estimate")$log_d_t[t]+c(-1.96,1.96)*as.list(Opt2$SD,"Std. Error")$log_d_t[t], col="red" )
}
dev.off()
}

write.csv( cbind(Opt2$diagnostics, summary(Opt$SD,"fixed")[,'Std. Error']), file=paste0(getwd(),"/pollock_2.csv") )

###############
# Fix SigmaM at zero
###############

Map = list()
Map$log_sigmaM = factor(NA)
Parameters$log_sigmaM = log(1e-10)
Obj = MakeADFun(data=Data, parameters=Parameters, random=Random, map=Map, DLL="gompertz_2") #
Opt3 = TMBhelper::Optimize( obj=Obj )
Report = Obj$report()

# Compare AIC
c(Opt2$AIC, Opt3$AIC)
47 changes: 47 additions & 0 deletions Week 3 -- Temporal Models/Lab/In-class exercise/gompertz_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
// Data
DATA_INTEGER( nt );
DATA_VECTOR( log_b_t );
DATA_VECTOR( SD_log_b_t );

// Parameters
PARAMETER( log_d0 );
PARAMETER( log_sigmaP );
PARAMETER( log_sigmaM );
PARAMETER( alpha );
PARAMETER( rho );
PARAMETER_VECTOR( log_d_t );

// Objective funcction
Type jnll = 0;

// Reporting
Type sigmaP = exp(log_sigmaP);
vector<Type> sigmaM_t(nt);
for( int t=0; t<nt; t++){
sigmaM_t(t) = pow( pow(exp(log_sigmaM),2) + pow(SD_log_b_t(t),2), 0.5 );
}

// Probability of random coefficients
jnll -= dnorm( log_d_t(0), log_d0, exp(log_sigmaP), true );
for( int t=1; t<nt; t++){
jnll -= dnorm( log_d_t(t), alpha + rho*log_d_t(t-1), sigmaP, true );
}

// Probability of data conditional on fixed and random effect values
for( int t=0; t<nt; t++){
jnll -= dnorm( log_b_t(t), log_d_t(t), sigmaM_t(t), true );
}

REPORT( sigmaP );
REPORT( sigmaM_t );
REPORT( log_d_t );

ADREPORT( sigmaP );
ADREPORT( sigmaM_t );

return jnll;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Week 3 -- Temporal Models/Lab/In-class exercise/pollock_2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"","Param","starting_value","Lower","MLE","Upper","final_gradient","summary(Opt$SD, ""fixed"")[, ""Std. Error""]"
"log_d0","log_d0",0,-Inf,4.2042152454022,Inf,1.92605013971382e-07,0.260513687545267
"log_sigmaP","log_sigmaP",1,-Inf,-1.52772207486349,Inf,-4.45955622083714e-07,0.117851155182634
"log_sigmaM","log_sigmaM",1,-Inf,-12.602926982783,Inf,2.75469399300347e-10,26029.8440043728
"alpha","alpha",0,-Inf,2.12296833291583,Inf,1.01415665495722e-06,0.6652356191063
"rho","rho",0,-Inf,0.534989073577766,Inf,4.91719614775057e-06,0.145993116527693
5 changes: 5 additions & 0 deletions Week 3 -- Temporal Models/Lab/In-class exercise/pollock_3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"","Param","starting_value","Lower","MLE","Upper","final_gradient","summary(Opt$SD, ""fixed"")[, ""Std. Error""]"
"log_d0","log_d0",0,-Inf,4.2042152330218,Inf,5.70944991666547e-08,0.252015718084337
"log_sigmaP","log_sigmaP",1,-Inf,-1.52772206019661,Inf,4.02960706804333e-08,0.156722880918147
"alpha","alpha",0,-Inf,2.12296839434053,Inf,-1.14044039858778e-07,0.753630593218351
"rho","rho",0,-Inf,0.534989059737808,Inf,-5.77914213419342e-07,0.165465200289098
Binary file modified Week 3 -- Temporal Models/Lab/Lab 3 -- Gompertz model.pptx
Binary file not shown.

0 comments on commit 90335d1

Please sign in to comment.