-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecast.R
267 lines (194 loc) · 7.69 KB
/
forecast.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
## Forecast Central GA black bear dynamics and viability
library(coda)
source("forecast_fn.R")
args(forecast)
## Load posterior samples
load("jc4.gzip")
jc4 <- as.mcmc.list(jc4.out)
ps <- window(jc4, thin=10)
niter(ps)*nchain(ps)
## debugonce(forecast)
## No harvest
system.time({
forc.h0 <- forecast(ps, nFuture=50, M=15000,
harvest=0, stochastic=TRUE,
NbarSims=100,
report=100)
})
save(forc.h0, file="forc-h0.gzip")
## 5 additional females per year
system.time({
forc.h5 <- forecast(ps, nFuture=50, M=15000,
harvest=5, stochastic=TRUE,
NbarSims=100,
report=100)
})
save(forc.h5, file="forc-h5.gzip")
## 10 additional females per year
system.time({
forc.h10 <- forecast(ps, nFuture=50, M=15000,
harvest=10, stochastic=TRUE,
NbarSims=100,
report=100)
})
save(forc.h10, file="forc-h10.gzip")
## 15 additional females per year
system.time({
forc.h15 <- forecast(ps, nFuture=50, M=15000,
harvest=15, stochastic=TRUE,
NbarSims=100,
report=100)
})
save(forc.h15, file="forc-h15.gzip")
## 20 additional females per year
system.time({
forc.h20 <- forecast(ps, nFuture=50, M=15000,
harvest=20, stochastic=TRUE,
NbarSims=100,
report=100)
})
save(forc.h20, file="forc-h20.gzip")
## ## Load saved results (if the forecasting above has already been completed)
## load("forc-h0.gzip")
## load("forc-h5.gzip")
## load("forc-h10.gzip")
## load("forc-h15.gzip")
## load("forc-h20.gzip")
## Extinction risk curves for each posterior draw
## These are samples from the posterior predictive distribution of
## extinction risk at each future year
EX21.h0 <- apply(forc.h0$Nbar==0, c(1, 3), mean)
EX21.h5 <- apply(forc.h5$Nbar==0, c(1, 3), mean)
EX21.h10 <- apply(forc.h10$Nbar==0, c(1, 3), mean)
EX21.h15 <- apply(forc.h15$Nbar==0, c(1, 3), mean)
EX21.h20 <- apply(forc.h20$Nbar==0, c(1, 3), mean)
matplot(EX21.h15[,1:100])
## Posterior means and 95% CIs
EX21.h0.mean <- rowMeans(EX21.h0)
EX21.h0.low <- apply(EX21.h0, 1, quantile, prob=0.025)
EX21.h0.upp <- apply(EX21.h0, 1, quantile, prob=0.975)
EX21.h5.mean <- rowMeans(EX21.h5)
EX21.h5.low <- apply(EX21.h5, 1, quantile, prob=0.025)
EX21.h5.upp <- apply(EX21.h5, 1, quantile, prob=0.975)
EX21.h10.mean <- rowMeans(EX21.h10)
EX21.h10.low <- apply(EX21.h10, 1, quantile, prob=0.025)
EX21.h10.upp <- apply(EX21.h10, 1, quantile, prob=0.975)
EX21.h15.mean <- rowMeans(EX21.h15)
EX21.h15.low <- apply(EX21.h15, 1, quantile, prob=0.025)
EX21.h15.upp <- apply(EX21.h15, 1, quantile, prob=0.975)
EX21.h20.mean <- rowMeans(EX21.h20)
EX21.h20.low <- apply(EX21.h20, 1, quantile, prob=0.025)
EX21.h20.upp <- apply(EX21.h20, 1, quantile, prob=0.975)
plot(EX21.h10.mean, ylim=0:1, type="l")
lines(EX21.h10.upp, col=gray(0.8))
## Annual abundance for each posterior draw, averaged
## over stochastic harvest
EN21.h0 <- apply(forc.h0$Nbar, c(1, 3), median)
EN21.h5 <- apply(forc.h5$Nbar, c(1, 3), median)
EN21.h10 <- apply(forc.h10$Nbar, c(1, 3), median)
EN21.h15 <- apply(forc.h15$Nbar, c(1, 3), median)
EN21.h20 <- apply(forc.h20$Nbar, c(1, 3), median)
matplot(EN21.h0, type="l")
matplot(EN21.h20, type="l")
quantile(EN21.h0[51,], c(0.025, 0.5, 0.975))
quantile(EN21.h5[51,], c(0.025, 0.5, 0.975))
quantile(EN21.h10[51,], c(0.025, 0.5, 0.975))
quantile(EN21.h15[51,], c(0.025, 0.5, 0.975))
quantile(EN21.h20[51,], c(0.025, 0.5, 0.975))
## Harvest rates
5/rowMeans(EN21.h5)
10/rowMeans(EN21.h10)
15/rowMeans(EN21.h15)
20/rowMeans(EN21.h20)
## Years
fyrs <- 2017:(2017+50)
iyrs <- 1:51 ## Reduce the length of this sequence to shorten the
## time horizon in the figure
## Colors
pclr <- rgb(0,0,1,0.6)
lclr <- rgb(0,0,1)
## Figure 6 in the paper
png("../figs/fig-6_forecasts_h0-h20.png", width=6, height=9,
units="in", res=400)
par(mfrow=c(5,2), mai=c(0.6, 0.6, 0.2, 0.1))
## h0
matplot(fyrs[iyrs], EN21.h0[iyrs,], type="l", xlab="",
ylab="Female abundance", ylim=c(0, 600),
col=gray(0.8), cex.lab=1.3,
lwd=0.5)
polygon(c(fyrs[iyrs], rev(fyrs[iyrs])),
c(apply(EN21.h0[iyrs,], 1, quantile, prob=0.025),
rev(apply(EN21.h0[iyrs,], 1, quantile, prob=0.975))),
border=NA, col=pclr) #col=rgb(1,1,0,0.6))
lines(fyrs[iyrs], rowMeans(EN21.h0)[iyrs], lwd=3, col=lclr)
plot(fyrs[iyrs], EX21.h0.mean[iyrs], type="l", col="red", lwd=3,
ylim=c(0,0.5),
xlab="", ylab="Extinction risk", cex.lab=1.3)
mtext("Status quo", side=3, line=0.4, outer=F, cex=1, at=c(2005,1))
text(2047, 0.45, "Extinction risk at year 50 < 0.001%")
## h5
matplot(fyrs[iyrs], EN21.h5[iyrs,], type="l", xlab="",
ylab="Female abundance", ylim=c(0, 600),
col=gray(0.8), cex.lab=1.3,
lwd=0.5)
polygon(c(fyrs[iyrs], rev(fyrs[iyrs])),
c(apply(EN21.h5[iyrs,], 1, quantile, prob=0.025),
rev(apply(EN21.h5[iyrs,], 1, quantile, prob=0.975))),
border=NA, col=pclr) #col=rgb(1,1,0,0.6))
lines(fyrs[iyrs], rowMeans(EN21.h5)[iyrs], lwd=3, col=lclr)
plot(fyrs[iyrs], EX21.h5.mean[iyrs], type="l", col="red", lwd=3,
ylim=c(0,0.5),
xlab="", ylab="Extinction risk", cex.lab=1.3)
mtext("5 additional females harvested each year", side=3, line=0.4,
outer=F, cex=1, at=c(2005,1))
text(2047, 0.45, "Extinction risk at year 50 = 0.13%")
## h10
matplot(fyrs[iyrs], EN21.h10[iyrs,], type="l", xlab="",
ylab="Female abundance", ylim=c(0, 600),
col=gray(0.8), cex.lab=1.3,
lwd=0.5)
polygon(c(fyrs[iyrs], rev(fyrs[iyrs])),
c(apply(EN21.h10[iyrs,], 1, quantile, prob=0.025),
rev(apply(EN21.h10[iyrs,], 1, quantile, prob=0.975))),
border=NA, col=pclr) #col=rgb(1,1,0,0.6))
lines(fyrs[iyrs], rowMeans(EN21.h10)[iyrs], lwd=3, col=lclr)
plot(fyrs[iyrs], EX21.h10.mean[iyrs], type="l", col="red", lwd=3,
ylim=c(0,0.5),
xlab="", ylab="Extinction risk", cex.lab=1.3)
mtext("10 additional females harvested each year", side=3, line=0.4,
outer=F, cex=1, at=c(2005,1))
text(2047, 0.45, "Extinction risk at year 50 = 1.15%")
## h15
matplot(fyrs[iyrs], EN21.h15[iyrs,], type="l", xlab="",
ylab="Female abundance", ylim=c(0, 600),
col=gray(0.8), cex.lab=1.3,
lwd=0.5)
polygon(c(fyrs[iyrs], rev(fyrs[iyrs])),
c(apply(EN21.h15[iyrs,], 1, quantile, prob=0.025),
rev(apply(EN21.h15[iyrs,], 1, quantile, prob=0.975))),
border=NA, col=pclr) #col=rgb(1,1,0,0.6))
lines(fyrs[iyrs], rowMeans(EN21.h15)[iyrs], lwd=3, col=lclr)
plot(fyrs[iyrs], EX21.h15.mean[iyrs], type="l", col="red", lwd=3,
ylim=c(0,0.5),
xlab="", ylab="Extinction risk", cex.lab=1.3)
mtext("15 additional females harvested each year", side=3, line=0.4,
outer=F, cex=1, at=c(2005,1))
text(2047, 0.45, "Extinction risk at year 50 = 9.0%")
## h20
matplot(fyrs[iyrs], EN21.h20[iyrs,], type="l", xlab="Year",
ylab="Female abundance", ylim=c(0, 600),
col=gray(0.8), cex.lab=1.3,
lwd=0.5)
polygon(c(fyrs[iyrs], rev(fyrs[iyrs])),
c(apply(EN21.h20[iyrs,], 1, quantile, prob=0.025),
rev(apply(EN21.h20[iyrs,], 1, quantile, prob=0.975))),
border=NA, col=pclr) #col=rgb(1,1,0,0.6))
lines(fyrs[iyrs], rowMeans(EN21.h20)[iyrs], lwd=3, col=lclr)
plot(fyrs[iyrs], EX21.h20.mean[iyrs], type="l", col="red", lwd=3,
ylim=c(0,0.5),
xlab="Year", ylab="Extinction risk", cex.lab=1.3)
mtext("20 additional females harvested each year", side=3, line=0.4,
outer=F, cex=1, at=c(2005,1))
text(2047, 0.45, "Extinction risk at year 50 = 37.0%")
dev.off()
system("open ../figs/fig-6_forecasts_h0-h20.png")