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

Commit

Permalink
small changes to Week 7 lab
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed May 10, 2018
1 parent 10e84a7 commit c406953
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
Binary file not shown.
1 change: 1 addition & 0 deletions Week 7 -- spatiotemporal models/Lab/Sim_Gompertz_Fn.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ function( n_years, n_stations=100, phi=NULL, SpatialScale=0.1, SD_O=0.5, SD_E=0.

# Return stuff
Sim_List = list("DF"=DF, "phi"=phi, "Loc"=Loc, "Omega"=Omega, "Epsilon"=Epsilon, "Theta"=Theta)
Sim_List[["Parameters"]] = c('SpatialScale'=SpatialScale, 'SigmaO'=SD_O, 'SigmaE'=SD_E, 'rho'=rho, 'phi'=phi)
return(Sim_List)
}
3 changes: 2 additions & 1 deletion Week 7 -- spatiotemporal models/Lab/spatial_gompertz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Type objective_function<Type>::operator() ()
// Likelihood contribution from observations
vector<Type> log_chat_i(n_i);
for (int i=0; i<n_i; i++){
log_chat_i(i) = phi*pow(rho,t_i(i)) + Epsilon_xt(x_s(s_i(i)),t_i(i)) + (eta_x(x_s(s_i(i))) + Omega_x(x_s(s_i(i))) ) / (1-rho);
log_chat_i(i) = phi*pow(rho,t_i(i)) + (eta_x(x_s(s_i(i))) + Omega_x(x_s(s_i(i)) + Epsilon_xt(x_s(s_i(i)),t_i(i))) ) / (1-rho);
if( !isNA(c_i(i)) ){
jnll_comp(2) -= dpois( c_i(i), exp(log_chat_i(i)), true );
}
Expand All @@ -100,6 +100,7 @@ Type objective_function<Type>::operator() ()
REPORT( Epsilon_xt );
REPORT( Omega_x );
REPORT( Equil_x );
REPORT( phi );

return jnll;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


setwd( "C:/Users/James.Thorson/Desktop/Project_git/2016_classes_private/Spatio-temporal models/Week 7 -- spatiotemporal models/Lab" )
setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 7 -- spatiotemporal models/Lab" )

#########################
# Spatial Gompertz model
Expand All @@ -11,12 +11,11 @@ setwd( "C:/Users/James.Thorson/Desktop/Project_git/2016_classes_private/Spatio-t
library(INLA)
library(TMB)
library(RandomFields)
library(TMBdebug)

source( "Sim_Gompertz_Fn.R" )

# Read data
# n_years=10; n_stations=100; SpatialScale=0.1; SD_O=0.5; SD_E=0.2; SD_extra=0; rho=0.8; logMeanDens=1; phi=NULL; Loc=NULL
set.seed( 2 )
Sim_List = Sim_Gompertz_Fn( n_years=10, n_stations=100, SpatialScale=0.1, SD_O=0.4, SD_E=0.2, SD_extra=0, rho=0.5, logMeanDens=1, phi=0.0, Loc=NULL )
DF = Sim_List[["DF"]]
loc_xy_orig = loc_xy = Sim_List[["Loc"]]
Expand All @@ -32,7 +31,7 @@ if( n_knots < nrow(loc_xy) ){
}

plot( loc_xy_orig, cex=2, pch=20 )
points( loc_xy, cex=2, pch=20, col="red")
points( loc_xy, cex=2, pch=3, col="red")

# Build SPDE object using INLA (must pass mesh$idx$loc when supplying Boundary)
mesh = inla.mesh.create( loc_xy )
Expand All @@ -43,10 +42,15 @@ spde = inla.spde2.matern( mesh )


###################
#
# Parameter estimation
#
###################

##### Version 0 -- Sweep upstream to downstream through time
#####################
# Version 0 -- Sweep upstream to downstream through time
#####################

Version = "spatial_gompertz_state_as_random"

# Compile
Expand All @@ -60,19 +64,19 @@ Parameters = list(alpha=c(0.0), phi=0.0, log_tau_U=1.0, log_tau_O=1.0, log_kappa
Random = c("log_D_xt","Omega_input")

# Make object
obj <- MakeADFun(data=Data, parameters=Parameters, random=Random, hessian=FALSE, DLL=Version)
Obj = MakeADFun(data=Data, parameters=Parameters, random=Random, hessian=FALSE, DLL=Version)

# Run optimizer
start_time = Sys.time()
opt0 = nlminb(obj$par, objective=obj$fn, gradient=obj$gr, lower=c(rep(-20,2),rep(-10,3),-0.999), upper=c(rep(20,2),rep(10,3),0.999), control=list(eval.max=1e4, iter.max=1e4, trace=1))
opt0[["final_gradient"]] = obj$gr( opt0$par )
opt0[["total_time"]] = Sys.time() - start_time
Opt0 = TMBhelper::Optimize( obj=Obj, lower=c(rep(-Inf,5),-0.999), upper=c(rep(Inf,5),0.999), getsd=TRUE, newtonsteps=1 )

# Get standard errors
Report0 = obj$report()
SD0 = try( sdreport(obj) )
Report0 = Obj$report()
H0 = Obj$env$spHess()

##################
# Version 3 -- Joint analysis using TMB functions
##################

##### Version 3 -- Joint analysis using TMB functions
Version = "spatial_gompertz"

# Compile
Expand All @@ -86,21 +90,32 @@ Parameters = list(alpha=c(0.0), phi=0.0, log_tau_E=1.0, log_tau_O=1.0, log_kappa
Random = c("Epsilon_input","Omega_input")

# Make object
obj <- MakeADFun(data=Data, parameters=Parameters, random=Random, hessian=FALSE, DLL=Version)
Obj <- MakeADFun(data=Data, parameters=Parameters, random=Random, hessian=FALSE, DLL=Version)

# Run optimizer
start_time = Sys.time()
opt3 = nlminb(obj$par, objective=obj$fn, gradient=obj$gr, lower=c(rep(-20,2),rep(-10,3),-0.999), upper=c(rep(20,2),rep(10,3),0.999), control=list(eval.max=1e4, iter.max=1e4, trace=1))
opt3[["final_gradient"]] = obj$gr( opt3$par )
opt3[["total_time"]] = Sys.time() - start_time
Opt3 = TMBhelper::Optimize( obj=Obj, lower=c(rep(-Inf,5),-0.999), upper=c(rep(Inf,5),0.999), getsd=TRUE, newtonsteps=1 )

# Get standard errors
Report3 = obj$report()
SD3 = try( sdreport(obj) )
Report3 = Obj$report()
H3 = Obj$env$spHess()

######## Compare results

# Report
unlist( Report0[c('Range','SigmaO','SigmaU','SigmaE','rho')] )
unlist( Report3[c('Range','SigmaO','SigmaU','SigmaE','rho')] )
# Check parameter estimates
unlist(Report0[c('Range','SigmaO','SigmaU','rho','phi')])
unlist(Report3[c('Range','SigmaO','SigmaE','rho','phi')])
Sim_List[["Parameters"]][c('SpatialScale','SigmaO','SigmaE','rho','phi')]

# Compare sparseness
sum( H0!=0 ) / prod(dim(H0))
sum( H3!=0 ) / prod(dim(H0))

# Show inner hessian
image( H0, main="Version0: Sweep through time" );
dev.new(); image(H3, main="Version3: TMB functions")

# Run times
Opt0$run_time
Opt3$run_time


Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Type objective_function<Type>::operator() ()
REPORT( log_Dpred_xt );
REPORT( Omega_x );
REPORT( Equil_x );
REPORT( phi );

return jnll;
}

0 comments on commit c406953

Please sign in to comment.