-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSWO_SA_prime_v1.2.R
266 lines (209 loc) · 9.5 KB
/
SWO_SA_prime_v1.2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
##><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><><><
## JABBA: Just Another Bayesian Biomass Assessment
## Input File for JABBA
## Developed by Henning Winker & Felipe Carvalho (Cape Town/Hawaii)
##><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
rm(list=ls())
# Install missing packages
list.of.packages <- c("gplots", "coda","rjags","R2jags","fitdistrplus","reshape")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# Load Packages
library(gplots);library(coda);library(rjags);library(R2jags);library("fitdistrplus");library(reshape)
#----------------------------------------------------------------
# Setup working directories and output folder labels
#-----------------------------------------------------------------
# Set Working directory file, where assessments are stored
File = "C:/Work/Research/GitHub/JABBA_testruns"
setwd(File) # Writes JABBA model in this file
# Set working directory for JABBA R source code
JABBA.file = "C:/Work/Research/GitHub/JABBAbeta"
# JABBA version
version = "v1.2beta"
# Set Assessment file: assement folder within File that includes .csv input files
assessment = "SWO_SA"
# add specifier for assessment (File names of outputs)
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# Graphic, Output, Saving (.RData) settings
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
KOBE.plot = TRUE # Produces JABBA Kobe plot
KOBE.type = c("ICCAT","IOTC")[2] # ICCAT uses 3 colors; IOTC 4 (incl. orange)
Biplot= TRUE # Produces a "post-modern" biplot with buffer and target zones (Quinn & Collie 2005)
SP.plot = c("standard","phase")[2] # Produces standard or 'Kobe phase' SP plot
save.trajectories =TRUE # saves posteriors of P=B/K, B/Bmsy and H/Hmsy as .RData object
harvest.label = c("Hmsy","Fmsy")[2] # choose label preference H/Hmsy versus Fmsy
CPUE.plot= TRUE # Runs state-tool to produce "alligned" multi-CPUE plot
meanCPUE = FALSE # Uses averaged CPUE from state-space tool instead of individual indices
Projection = TRUE # Use Projections: requires to define TACs vectors
save.projections = TRUE # saves projection posteriors as .RData object
catch.metric = "(t)" # Define catch input metric e.g. (tons) "000 t" etc
Reproduce.seed = FALSE # If FALSE a random seed assigned to each run, if TRUE set.seed(123)
# Save entire posterior as .RData object
save.all = FALSE # (if TRUE, a very large R object of entire posterior is saved)
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# Optional: Note Scenarios
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# S1: Model including Brazil1
# S2: Model excluding Brazil1
# S3: Base-case Model with time blocks on ESP and JPN
# S4: Add scenario as example for using average CPUE
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# Specify Scenario name for output file names
Scenarios = c(paste0("Scenario",1:4))
# Execute multiple JABBA runs in loop
for(s in 1:4){
Scenario = Scenarios[s]
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# Suplus Production model specifications
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# Choose model type:
# 1: Schaefer
# 2: Fox
# 3: Pella-Tomlinsson
# 4: Pella-Tomlinsson with m prior
Model = 3
Mod.names = c("Schaefer","Fox","Pella","Pella_m")[Model]
# Depensation opiton:
# Set Plim = Blim/K where recruitment may become impaired (e.g. Plim = 0.25)
# Choose Plim = 0 to reduce to conventional Schaefer, Fox, Pella models
Plim = 0
# Required specification for Pella-Tomlinson (Model = 3)
BmsyK = 0.4 # Set Surplus Production curve inflection point
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
#--------------------------------------------------
# Read csv files
#--------------------------------------------------
# Use SEs from csv file for abudance indices (TRUE/FALSE)
SE.I = TRUE
# Load assessment data
catch = read.csv(paste0(File,"/",assessment,"/catch",assessment,".csv"))
cpue = read.csv(paste0(File,"/",assessment,"/cpue",assessment,".csv"))#
if(SE.I ==TRUE){
se = read.csv(paste0(File,"/",assessment,"/se",assessment,".csv"))
}
names(cpue)
names(catch)
#--------------------------------------------------
# option to exclude CPUE time series or catch year
#--------------------------------------------------
# Set up Base-Case for SWO
if(s<3){ # Combine SPA and JAPAN
cpue[,4] = apply(cpue[,4:5],1,mean,na.rm=TRUE)
cpue[,6] = apply(cpue[,6:7],1,mean,na.rm=TRUE)
cpue = cpue[,-c(5,7)]
se[,4] = apply(se[,4:5],1,mean,na.rm=TRUE)
se[,6] = apply(se[,6:7],1,mean,na.rm=TRUE)
se = se[,-c(5,7)]
cpue[!is.finite(cpue[,4]),4]=NA
cpue[!is.finite(cpue[,5]),5]=NA
se[!is.finite(se[,4]),4]=NA
se[!is.finite(se[,5]),5]=NA
}
# Remove BrazilI
if(s>1){
cpue = cpue[,-c(2)]
se = se[,-c(2)]
}
names(cpue)
ncol(catch)
ncol(cpue)
#------------------------------------------------------
# Option use mean CPUE from state-space cpue averaging
#-----------------------------------------------------
meanCPUE = FALSE
if(s==4) meanCPUE = TRUE
#------------------------------------------------
# Prior for unfished biomass K
#------------------------------------------------
# The option are:
# a) Specify as a lognormal prior with mean and CV
# b) Specify as range to be converted into lognormal prior
K.dist = c("lnorm","range")[1]
# if lnorm use mean and CV; if range use lower,upper bound
K.prior = c(200000,1)
#-----------------------------------------------------------
# mean and CV and sd for Initial depletion level P1= SB/SB0
#-----------------------------------------------------------
# Set the initial depletion prior B1/K
# To be converted into a lognormal prior (with upper bound at 1.1)
psi.dist= c("lnorm","beta")[1]
# specify as mean and CV
psi.prior = c(1,0.25)
#--------------------------------------------------------------
# Determine estimation for catchability q and observation error
#--------------------------------------------------------------
# Assign q to CPUE
sets.q = 1:(ncol(cpue)-1)
#----------------------------------------------------
# Determine r prior
#----------------------------------------------------
# The option are:
# a) Specifying a lognormal prior
# b) Specifying a resiliance category after Froese et al. (2017; CMSY)
# Resilience: "Very low", "Low", "Medium", High" (requires r.range = TRUE)
# use [1] lognormal(mean,stdev) or [2] range (min,max) or
r.dist = c("lnorm","range")[1]
r.prior = c(0.42,0.37)
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>
# Observation Error
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>
#To Estimate additional observation variance set sigma.add = TRUE
sigma.est = TRUE
# Series
sets.var = 1:(ncol(cpue)-1) # estimate individual additional variace
# As option for data-weighing
# minimum fixed observation error for each variance set (optional choose 1 value for both)
fixed.obsE = c(0.2) # Important if SE.I is not availble
# Total observation error: TOE = sqrt(SE^2+sigma.est^2+fixed.obsE^2)
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>
# Process Error
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>
#Estimate set sigma.proc == True
sigma.proc = TRUE
# Determines if process error deviation are estimated for all years (TRUE)
# or only from the point the first abundance index becomes available (FALSE)
proc.dev.all = FALSE
#------------------------------------------
if(sigma.proc == TRUE){
igamma = c(4,0.01) #specify inv-gamma parameters
# Process error check
gamma.check = 1/rgamma(1000,igamma[1],igamma[2])
# check mean process error + CV
mu.proc = sqrt(mean(gamma.check)); CV.proc = sd(sqrt(gamma.check))/mean(sqrt(gamma.check))
# check CV
round(c(mu.proc,CV.proc),3)
quantile(sqrt(gamma.check),c(0.1,0.9))
}else{
sigma.proc = 0.07 #IF Fixed: typicallly 0.05-0.15 (see Ono et al. 2012)
}
#--------------------------------------------
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>
# Optional: Do TAC Projections
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>>
Projection = TRUE # Switch on by Projection = TRUE
# Check final year catch
catch[nrow(catch),]
# Set range for alternative TAC projections
TACs = seq(10000,18000,1000) #example
# Intermitted TAC to get to TAC implementation year
#TACint = mean(catch[nrow(catch)-3,2]:catch[nrow(catch),2]) # avg last 3 years
TACint = 10058 # Catch for 2016
# Set year of first TAC implementation
imp.yr = 2018
# Set number of projections years
pyrs = 10
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# Execute model and produce output
#><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>><>
# MCMC settings
ni <- 30000 # Number of iterations
nt <- 5 # Steps saved
nb <- 5000 # Burn-in
nc <- 2 # number of chains
nsaved = (ni-nb)/nt*nc
# Run model (BSPSPexe file must be in the same working directory)
source(paste0(JABBA.file,"/JABBA",version,".R"))
# Run plot function
source(paste0(JABBA.file,"/JABBA_plots_",version,".R"))
}# THE END