-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathjabbaz.R
223 lines (180 loc) · 6.51 KB
/
jabbaz.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
setwd("C:/Work/Research/Laurie/JabbaZ")
load("C:/Work/Research/Laurie/JabbaZ/om.75.RData",verbose=T)
library(FLCore)
library(FLBRP)
library(JABBA)
plot(om)
stk = iter(om,1)
plot(stk)
stk = window(stk,start=70,end=115)
# priors
fmsy = an(refpts(eq)["msy","harvest"])
bmsy = an(refpts(eq)["msy","ssb"])
b0 = an(refpts(eq)["virgin","ssb"])
msy = an(refpts(eq)["msy","yield"])
shape = bmsy/b0
# Always convert Z to rate z = 1-exp(-Z)
zobs = function(flq,frange = "missing"){
out=NULL
CN = flq
if(missing(frange)){
Amin = which(apply(CN,1,median)==max(apply(CN,1,median)))
Amax = nrow(CN)-1
frange= Amin:Amax
}
for(t in 1:ncol(CN)){
Ct= CN[,t]
df = as.data.frame(log(Ct[frange,]))
Z = -coef(lm(data~age,df))[[2]]
df = df[1,]
df$data = 1-exp(-Z)
df$age = "all"
out = rbind(out,df)
}
return(out)
}
years = an(dimnames(stk)$year)
# Prepare df
# Important don't use too many age-class because of lag
# If I choose ages 5:15 the default lag 1 works - for 5:20 I need lag2+
set.seed(1234)
df = as.data.frame(FLQuants(Catch=catch(stk),
Index= vb(stk)*ar1rlnorm(0,years,sdlog=0.25),
Z1=zobs(catch.n(stk),frange=5:15)*ar1rlnorm(0,years,sdlog=0.1),
Z2=zobs(catch.n(stk),frange=5:15)*ar1rlnorm(0,years,sdlog=0.2)
))
om = NULL
om$stock = as.data.frame(ssb(stkfw)/bmsy)
om$harvest = as.data.frame(fbar(stkfw)/fmsy)
om$catch = as.data.frame(catch(stkfw))
om$fmsy = fmsy
om$bmsy = bmsy
om$msy = c(refpts(eq)["msy","yield"])
catch = Catch
index = Index
catch.n = as.data.frame(catch.n(stk))
catch.n = catch.n[catch.n$year>=85,]
save(catch,index,catch.n,om,file="data/pol.sim.rdata")
ggplot(df[df$qname%in%c("Z1","Z2"),],aes(year,data,color=qname))+geom_line()
inp = reshape2::dcast(df,year~qname,value.var="data",fun.aggregate=sum)
Index = inp[,c("year","Index")]
Index[Index==0] = NA
Index$Index[1:15] = NA # exclude observations
Index$Index = Index$Index/mean(Index$Index,na.rm=T)
Catch = inp[,c("year","Catch")]
Z= inp[,c("year","Z1","Z2")]
Z.se = Z
Z.se[,2:3] = 0.1
Z[Z==0] = NA
Z[1:15,-1] = NA # exclude observations
# Fit with Catch + Index: Simple Fox with r = Fmsy
jbinput= build_jabba(catch=Catch,cpue=Index,auxiliary=Z,
auxiliary.se = Z.se,
model.type = "Fox",scenario="Index",
r.prior = c(fmsy,0.2),
auxiliary.type = "z",
auxiliary.lag = 4,
shape.CV = 0.2,
auxiliary.obsE = 0.01,auxiliary.sigma = T,verbose=F,
psi.prior=c(0.7,0.3))
fI= fit_jabba(jbinput,quickmcmc = T,verbose=F)
jabba=fI
jbplot_cpuefits(fI,index=1:2)
jbplot_runstest(fI,index=1:2)
jbplot_residuals(fI)
prj1 = fw_jabba(fI,quant="F",type="ratio",imp.yr =2,stochastic = T,imp.values=1,nyears = 15,AR1=T )
jbplot_ensemble(prj1)
# Compare: Black dashed line is TRUE pop dyn from OM
stkfw = iter(om,1)
jbpar(mfrow=c(2,2))
jbplot_ensemble(prj1,subplots = 1,add=T)
lines(data~year,data=as.data.frame(ssb(stkfw)/bmsy),lwd=2,lty=2)
jbplot_ensemble(prj1,subplots = 2,add=T)
lines(data~year,data=as.data.frame(fbar(stkfw)/fmsy),lwd=2,lty=2)
jbplot_ensemble(prj1,subplots = 5,add=T)
jbplot_ensemble(prj1,subplots = 6,add=T)
lines(data~year,data=as.data.frame(catch(stkfw)),lwd=2,lty=2)
# Fit Catch + Index + Z, testing with lag = 4
jbIZ.lag4 = build_jabba(catch=Catch,cpue=Index,auxiliary = Z,
model.type = "Fox",scenario="Ind+Z.lag4",
r.prior = c(fmsy,0.3),
auxiliary.type = "z",
psi.prior=c(0.7,0.3),
auxiliary.obsE = 0.1,
auxiliary.sigma=TRUE,
auxiliary.lag = 4, # Lag4
qA.cv=0.1,
verbose=F)
# Laurie - how to objectively check lag of F to Z response in stock?
error = ar1rlnorm(0,years,sdlog=0.)
Z35 =(zobs(stk,frange=5:35)*error)
Z20 =(zobs(stk,frange=5:20)*error)
Z15 =(zobs(stk,frange=5:15)*error)
Z10 =(zobs(stk,frange=5:10)*error)
Zom = apply(1-exp(-z(stk)[2:29]),2,median)
invI= 1/(vb(stk)*error)
invI = invI/mean(invI)*mean(Z35)
plot(invI,Z35,Z10,Zom)
# Fit Catch + Index + Z, testing with lag = 4
jbIZ.lag4 = build_jabba(catch=Catch,cpue=Index,auxiliary = Z,
model.type = "Fox",scenario="Ind+Z.lag4",
r.prior = c(fmsy,0.3),
auxiliary.type = "z",
psi.prior=c(0.7,0.3),
auxiliary.obsE = 0.2,
auxiliary.lag = 4, # Lag4
verbose=F)
fIZ= fit_jabba(jbIZ.lag4,quickmcmc = T,verbose=F)
# Fit only relative H ~ qZ, H is effort proxy with lag 4
jbIE.lag4 = build_jabba(catch=Catch,cpue=Index,auxiliary = Z,
model.type = "Fox",scenario="Ind+Eff.lag4",
r.prior = c(fmsy,0.3),
auxiliary.type = "effort",
auxiliary.obsE = 0.2,
auxiliary.lag = 4,
verbose=F,
psi.prior=c(0.7,0.3))
fIE= fit_jabba(jbIE.lag4,quickmcmc = T,verbose=F)
# Compare: Black dashed line is TRUE pop dyn from OM
jbpar(mfrow=c(2,2))
jbplot_ensemble(list(fI,fIZ,fIE),subplots = 1,add=T)
lines(data~year,data=as.data.frame(ssb(stk)/bmsy),lwd=2,lty=2)
jbplot_ensemble(list(fI,fIZ,fIE),subplots = 2,add=T)
lines(data~year,data=as.data.frame(fbar(stk)/fmsy),lwd=2,lty=2)
jbplot_ensemble(list(fI,fIZ,fIE),subplots = 5,add=T)
jbplot_ensemble(list(fI,fIZ,fIE),subplots = 6,add=T)
# Check fits
jbpar(mfrow=c(2,2))
jbplot_cpuefits(fIZ,add=T)
jbplot_cpuefits(fIE,add=T)
# Compare Lags
lags = 1:4
jbs = lapply(lags,function(x){
jb = build_jabba(catch=Catch,cpue=Index,auxiliary = Z,
model.type = "Fox",
scenario=paste0("Ind+Z.lag",x),
r.prior = c(fmsy,0.3),
auxiliary.type = "z",
psi.prior=c(0.7,0.3),
auxiliary.obsE = 0.2,
auxiliary.lag = x, # Lag2
verbose=F)
fit_jabba(jb,quickmcmc = T,verbose=F)
})
jbpar(mfrow=c(2,2))
jbplot_ensemble(jbs,subplots = 1,add=T)
lines(data~year,data=as.data.frame(ssb(stk)/bmsy),lwd=2,lty=2)
jbplot_ensemble(jbs,subplots = 2,add=T)
lines(data~year,data=as.data.frame(fbar(stk)/fmsy),lwd=2,lty=2)
jbplot_ensemble(jbs,subplots = 5,add=T)
jbplot_ensemble(jbs,subplots = 6,add=T)
jbpar(mfrow=c(2,2))
for(i in 1:4){
jbplot_runstest(jbs[[i]],index=2,add=T)
legend("topleft",paste("Lag",i),bty="n")
}
jbpar(mfrow=c(2,2))
for(i in 1:4){
jbplot_cpuefits(jbs[[i]],index=2,add=T)
legend("topleft",paste("Lag",i),bty="n")
}