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

Commit

Permalink
Added Week 3 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Apr 9, 2018
1 parent 0d9b8a8 commit 277575c
Show file tree
Hide file tree
Showing 24 changed files with 313 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Week 3 -- Temporal Models/Lab/Gompertz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 3 -- Temporal Models/Lab" )
Use_REML = FALSE
set.seed(2)

#####################
# Explore Gompertz model
#####################

beta= 0.2
alpha = 1
d_equil = exp(alpha/beta)

d_1 = seq(0,d_equil*2,length=1e4)
d_2 = d_1 * exp(alpha) * exp( - beta*log(d_1))

# Dynamics
png( file="Gompertz_dynamics.png", width=8, height=4, res=200, units="in")
par( mfrow=c(1,2), mar=c(3,3.5,2,0), mgp=c(1.75,0.25,0), tck=-0.02)
plot( x=d_1, y=d_2, type="l", lwd=3, xlab=expression(Biomass[t]), ylab=expression(Biomass[t+1]), main="Production")
abline( a=0, b=1, lty="dotted")
# Log-dynamics
plot( x=log(d_1[-1]), y=log(d_2[-1]/d_1[-1]), type="l", lwd=3, xlab=expression(log(Biomass[t])), ylab=expression(log(Biomass[t+1]/Biomass[t])), main="log-Biomass ratio" )
abline( a=1, b=0, lty="dotted")
dev.off()

######################
# Simulate data
######################

nt = 100
log_d0 = 3
sigmaP = 0.5
sigmaM = 0.5
alpha = 1
beta = 0.1

# Simulate predictors
log_d_t = log_b_t = rep(NA, nt)
log_d_t[1] = log_d0
for( t in 2:nt ){
log_d_t[t] = alpha + (1-beta)*log_d_t[t-1] + rnorm( 1, mean=0, sd=sigmaP )
}
for( t in 1:nt ){
log_b_t[t] = log_d_t[t] + rnorm( 1, mean=0, sd=sigmaM )
}



######################
# Run in TMB
######################

library(TMB)

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

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

# Build object
dyn.load( dynlib("gompertz") )
Obj = MakeADFun(data=Data, parameters=Parameters, random=Random) #

# Prove that function and gradient calls work
Obj$fn( Obj$par )
Obj$gr( Obj$par )

# Optimize
start_time = Sys.time()
Opt = nlminb( start=Obj$par, objective=Obj$fn, gradient=Obj$gr, control=list("trace"=1) )
Opt[["final_gradient"]] = Obj$gr( Opt$par )
Opt[["total_time"]] = Sys.time() - start_time

# Get reporting and SEs
Report = Obj$report()
SD = sdreport( Obj )

for( z in 1:3 ){
png( file=paste0("gompertz_",z,".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:nt, y=log_b_t, col="blue", cex=1.2, xlab="Time", ylab="Value", pch=20 )
if(z>=2){
points( x=1:nt, y=as.list(SD,"Estimate")$log_d_t, col="red" )
for( t in 1:nt) lines( x=rep(t,2), y=as.list(SD,"Estimate")$log_d_t[t]+c(-1.96,1.96)*as.list(SD,"Std. Error")$log_d_t[t], col="red" )
}
if(z>=3) lines( x=1:nt, y=log_d_t, col="black", lwd=2 )
dev.off()
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
43 changes: 43 additions & 0 deletions Week 3 -- Temporal Models/Lab/gompertz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
// Data
DATA_INTEGER( nt );
DATA_VECTOR( 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;

// 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), exp(log_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), exp(log_sigmaM), true );
}

// Reporting
Type sigmaP = exp(log_sigmaP);
Type sigmaM = exp(log_sigmaM);

REPORT( sigmaP );
REPORT( sigmaM );
REPORT( log_d_t );

ADREPORT( sigmaP );
ADREPORT( sigmaM );

return jnll;
}
Binary file added Week 3 -- Temporal Models/Lab/gompertz_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lab/gompertz_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lab/gompertz_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file added Week 3 -- Temporal Models/Lecture/gam_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/gam_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/gam_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/kalman_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/kalman_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/kalman_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
132 changes: 132 additions & 0 deletions Week 3 -- Temporal Models/Lecture/kalman_filter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@

setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 3 -- Temporal Models/Lecture" )
Use_REML = FALSE
set.seed(2)

######################
# Simulate data
######################

nt = 100
x0 = 3
sigmaP = 0.2
sigmaM = 0.2
alpha = 0.04

# Simulate predictors
x_t = y_t = rep(NA, nt)
x_t[1] = x0
for( t in 2:nt ){
x_t[t] = x_t[t-1] + rnorm( 1, mean=alpha, sd=sigmaP )
}
for( t in 1:nt ){
y_t[t] = x_t[t] + rnorm( 1, mean=0, sd=sigmaM )
}

######################
# Linear model in R
######################

Lm = lm( y_t ~ I(1:nt) )
ypred_t = predict(Lm, se=TRUE)

for( z in 1:3 ){
png( file=paste0("lm_",z,".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:nt, y=y_t, col="blue", cex=1.2, xlab="Time", ylab="Value", pch=20 )
if(z>=2){
points( x=1:nt, y=ypred_t$fit, col="red" )
for( t in 1:nt) lines( x=rep(t,2), y=ypred_t$fit[t]+c(-1.96,1.96)*ypred_t$se.fit[t], col="red" )
}
if(z>=3) lines( x=1:nt, y=x_t, col="black", lwd=2 )
dev.off()
}


######################
# Loess smoother in R
######################

Loess = loess( y_t ~ I(1:nt) )
ypred_t = predict(Loess, se=TRUE)

for( z in 1:3 ){
png( file=paste0("loess_",z,".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:nt, y=y_t, col="blue", cex=1.2, xlab="Time", ylab="Value", pch=20 )
if(z>=2){
points( x=1:nt, y=ypred_t$fit, col="red" )
for( t in 1:nt) lines( x=rep(t,2), y=ypred_t$fit[t]+c(-1.96,1.96)*ypred_t$se.fit[t], col="red" )
}
if(z>=3) lines( x=1:nt, y=x_t, col="black", lwd=2 )
dev.off()
}


######################
# Smoother in R
######################

library(mgcv)

Gam = gam( y_t ~ s(I(1:nt)) )
ypred_t = predict(Gam, se=TRUE)

for( z in 1:3 ){
png( file=paste0("gam_",z,".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:nt, y=y_t, col="blue", cex=1.2, xlab="Time", ylab="Value", pch=20 )
if(z>=2){
points( x=1:nt, y=ypred_t$fit, col="red" )
for( t in 1:nt) lines( x=rep(t,2), y=ypred_t$fit[t]+c(-1.96,1.96)*ypred_t$se.fit[t], col="red" )
}
if(z>=3) lines( x=1:nt, y=x_t, col="black", lwd=2 )
dev.off()
}

######################
# Run in TMB
######################

library(TMB)

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

# Build inputs
Data = list( "nt"=nt, "y_t"=y_t )
Parameters = list( "x0"=0, "log_sigmaP"=1, "log_sigmaM"=1, "alpha"=0, "x_t"=rep(0,nt) )
Random = c("x_t")
if( Use_REML==TRUE ) Random = union( Random, c("x0","alpha") )

# Build object
dyn.load( dynlib("kalman_filter") )
Obj = MakeADFun(data=Data, parameters=Parameters, random=Random) #

# Prove that function and gradient calls work
Obj$fn( Obj$par )
Obj$gr( Obj$par )

# Optimize
start_time = Sys.time()
Opt = nlminb( start=Obj$par, objective=Obj$fn, gradient=Obj$gr, control=list("trace"=1) )
Opt[["final_gradient"]] = Obj$gr( Opt$par )
Opt[["total_time"]] = Sys.time() - start_time

# Get reporting and SEs
Report = Obj$report()
SD = sdreport( Obj )

for( z in 1:3 ){
png( file=paste0("kalman_",z,".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:nt, y=y_t, col="blue", cex=1.2, xlab="Time", ylab="Value", pch=20 )
if(z>=2){
points( x=1:nt, y=as.list(SD,"Estimate")$x_t, col="red" )
for( t in 1:nt) lines( x=rep(t,2), y=as.list(SD,"Estimate")$x_t[t]+c(-1.96,1.96)*as.list(SD,"Std. Error")$x_t[t], col="red" )
}
if(z>=3) lines( x=1:nt, y=x_t, col="black", lwd=2 )
dev.off()
}

43 changes: 43 additions & 0 deletions Week 3 -- Temporal Models/Lecture/kalman_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
// Data
DATA_INTEGER( nt );
DATA_VECTOR( y_t );

// Parameters
PARAMETER( x0 );
PARAMETER( log_sigmaP );
PARAMETER( log_sigmaM );
PARAMETER( alpha );
PARAMETER_VECTOR( x_t );

// Objective funcction
Type jnll = 0;

// Probability of random coefficients
jnll -= dnorm( x_t(0), x0, exp(log_sigmaP), true );
for( int t=1; t<nt; t++){
jnll -= dnorm( x_t(t), x_t(t-1) + alpha, exp(log_sigmaP), true );
}

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

// Reporting
Type sigmaP = exp(log_sigmaP);
Type sigmaM = exp(log_sigmaM);

REPORT( sigmaP );
REPORT( sigmaM );
REPORT( x_t );

ADREPORT( sigmaP );
ADREPORT( sigmaM );
ADREPORT( x_t );

return jnll;
}
Binary file added Week 3 -- Temporal Models/Lecture/lm_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/lm_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/lm_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/loess_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/loess_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/Lecture/loess_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week 3 -- Temporal Models/week3-intro.pptx
Binary file not shown.

0 comments on commit 277575c

Please sign in to comment.