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

Commit

Permalink
last minute changes to Week 7 lab code
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed May 10, 2018
1 parent c406953 commit bf0d39b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Week 7 -- spatiotemporal models/Lab/Sim_Gompertz_Fn.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function( n_years, n_stations=100, phi=NULL, SpatialScale=0.1, SD_O=0.5, SD_E=0.
DF = NULL
for(s in 1:n_stations){
for(t in 1:n_years){
Tmp = c("Site"=s, "Year"=t, "Simulated_example"=rpois(1,lambda=exp(Theta[s,t]+SD_extra*rnorm(1))))#, 'Lon..DDD.DDDDD.'=Loc[s,1], 'Lat..DD.DDDDD.'=Loc[s,2] )
Tmp = c("Site"=s, "Year"=t, "Simulated_example"=rpois(1,lambda=exp(Theta[s,t]+SD_extra*rnorm(1))))
DF = rbind(DF, Tmp)
}}
DF = cbind( DF, 'Longitude'=Loc[DF[,'Site'],1], 'Latitude'=Loc[DF[,'Site'],2] )
DF = data.frame(DF, row.names=NULL)

# Return stuff
Sim_List = list("DF"=DF, "phi"=phi, "Loc"=Loc, "Omega"=Omega, "Epsilon"=Epsilon, "Theta"=Theta)
Sim_List = list("DF"=DF, "phi"=phi, "Loc"=Loc, "Omega"=Omega, "Epsilon"=Epsilon, "Theta"=Theta, "n_years"=n_years, "n_stations"=n_stations)
Sim_List[["Parameters"]] = c('SpatialScale'=SpatialScale, 'SigmaO'=SD_O, 'SigmaE'=SD_E, 'rho'=rho, 'phi'=phi)
return(Sim_List)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 7 -- spatiot
library(INLA)
library(TMB)
library(RandomFields)
library(raster)
library(RANN)

source( "Sim_Gompertz_Fn.R" )

# Read data
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 )
Sim_List = Sim_Gompertz_Fn( n_years=10, n_stations=1000, SpatialScale=0.1, SD_O=0.4, SD_E=1, SD_extra=0, rho=0.5, logMeanDens=1, phi=-2, Loc=NULL )
DF = Sim_List[["DF"]]
loc_xy_orig = loc_xy = Sim_List[["Loc"]]

# Reduce sample sizes to 100 per year
Which2Keep = sample(1:nrow(DF), size=100*Sim_List$n_years, replace=FALSE)
Which2Drop = setdiff(1:nrow(DF),Which2Keep)
DF[Which2Drop,'Simulated_example'] = NA

# Reduce number of stations -- OPTIONAL
n_knots = 50
if( n_knots < nrow(loc_xy) ){
Expand All @@ -37,8 +44,17 @@ points( loc_xy, cex=2, pch=3, col="red")
mesh = inla.mesh.create( loc_xy )
spde = inla.spde2.matern( mesh )

# display stations
#plot( x=loc_xy[,'x'], y=loc_xy[,'y'])
# Generate grid to visualize density
vizloc_xy = expand.grid( x=seq(0,1,by=0.001), y=seq(0,1,by=0.001) )
knots_xy = nn2( data=loc_xy_orig, query=vizloc_xy, k=1 )

# Plot densities
par( mfrow=c(2,5), mar=c(2,2,2,0), mgp=c(1.5,0.25,0) )
for( tI in 1:Sim_List$n_years ){
vizTheta_xy = array(Sim_List$Theta[ cbind(knots_xy$nn.idx,tI) ], dim=c(1001,1001) )
rasterTheta_xy = raster( vizTheta_xy )
plot( rasterTheta_xy, xlim=c(0,1), ylim=c(0,1), main=paste0("Year ",tI) )
}


###################
Expand Down

0 comments on commit bf0d39b

Please sign in to comment.