forked from KathrynVenning/FeralCatEradication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Supplementary code2.0.R
327 lines (230 loc) · 11.8 KB
/
Supplementary code2.0.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
###### Supplementary code for
##### Predicting feral cat-reduction targets and costs on large islands using stochastic population models
##### Venning, Saltre, Bradshaw 2021
###### can only be used after running lines 9-136 of main code
####################################################
## iterations and quasi ext for each following model
####################################################
iter <- 10000 #final model run at 10 000
itdiv <- iter/100 #final model rate at iter/1000
################################################################################################################
## untreated population with leakage
###############################################################################################################
## stochatic projection with density feedback
## set storage matrices & vectors
stray.cat.vec <- seq(0,100,10) #stray cats added 0 - 100 cats increasing by 10
final.md.out <- final.n.up.out <- final.n.lo.out <- rep(NA,length(stray.cat.vec)) #storage matrix
for (s in 1:length(stray.cat.vec)) {
n.sums.mat <- matrix(data = 0, nrow = iter, ncol = (t+1)) #storage matrix
for (e in 1:iter) {
popmat <- popmat.orig
n.mat <- matrix(0, nrow=age.max,ncol=(t+1))
n.mat[,1] <- init.vec
for (i in 1:t) {
## stochastic survival values
s.alpha <- estBetaParams(s.vec, s.sd.vec^2)$alpha
s.beta <- estBetaParams(s.vec, s.sd.vec^2)$beta
s.stoch <- rbeta(length(s.alpha), s.alpha, s.beta)
# stochastic fertilty sampler (gaussian)
fert.stch <- rnorm(length(popmat[,1]), popmat[1,], m.sd.vec)
fert.stoch <- ifelse(fert.stch < 0, 0, fert.stch)
totN.i <- sum(n.mat[,i])
pred.red <- a.lp/(1+(totN.i/b.lp)^c.lp)
popmat[1,] <- fert.stoch
diag(popmat[2:age.max,]) <- s.stoch*pred.red
n.mat[,i+1] <- popmat %*% n.mat[,i]
n.mat[,i+1] <- n.mat[,i+1] + round(rnorm(1,mean=stray.cat.vec[s],sd=(0.05*stray.cat.vec[s]))*ssd, 0) #adding stray cats into the population vector
} # end i loop
n.sums.mat[e,] <- ((as.vector(colSums(n.mat))/pop.found))
if (e %% itdiv==0) print(e)
} # end e loop
n.md <- apply(n.sums.mat, MARGIN=2, median, na.rm=T) # mean over all iterations
n.up <- apply(n.sums.mat, MARGIN=2, quantile, probs=0.975, na.rm=T) # upper over all iterations
n.lo <- apply(n.sums.mat, MARGIN=2, quantile, probs=0.025, na.rm=T) # lower over all iterations
final.md.out[s] <- n.md[t+1]
final.n.up.out[s] <- n.up[t+1]
final.n.lo.out[s] <- n.lo[t+1]
print(s)
} # s vec
untreated.leakage <- matrix(data=0, nrow = 4, ncol = length(yrs)) #results, proportional pop increase relative to year, empty matrix
untreated.leakage[1,] <- stray.cat.vec #fill matrix, column 1 = stay cats leaked into pop
untreated.leakage[2,] <- final.md.out #column 2 = median final N
untreated.leakage[3,] <- final.n.up.out #column 3 = upper 95% confidence
untreated.leakage[4,] <- final.n.lo.out# column 4 = lower 95% confidence
untreated.leakage
plot(stray.cat.vec, n.md, pch=19, type="l", xlab="mean number of stray cats added/year", ylab="final N at end of projection interval", lwd=2, ylim=c(0.95*min(n.lo),1.05*max(n.up))) #plot
lines(stray.cat.vec,final.n.lo.out,lty=2,col="red",lwd=1.5)
lines(stray.cat.vec,final.n.up.out,lty=2,col="red",lwd=1.5)
##############################################################################################################
### main two-phase cull with leakage
###############################################################################################################
## stochatic projection with density feedback
## set storage matrices & vectors
stray.cat.vec <- seq(0,100,10) #stray cats added 0 - 100 cats increasing by 10
final.md.out <- rep(NA,length(stray.cat.vec)) #storage matrix
for (s in 1:length(stray.cat.vec)) {
n.sums.mat <- matrix(data = 0, nrow = iter, ncol = (t+1)) #storage matrix
for (e in 1:iter) {
popmat <- popmat.orig
n.mat <- matrix(0, nrow=age.max,ncol=(t+1))
n.mat[,1] <- init.vec
for (i in 1:t) {
# stochastic survival values
s.alpha <- estBetaParams(s.vec, s.sd.vec^2)$alpha
s.beta <- estBetaParams(s.vec, s.sd.vec^2)$beta
s.stoch <- rbeta(length(s.alpha), s.alpha, s.beta)
# stochastic fertilty sampler (gaussian)
fert.stch <- rnorm(length(popmat[,1]), popmat[1,], m.sd.vec)
fert.stoch <- ifelse(fert.stch < 0, 0, fert.stch)
totN.i <- sum(n.mat[,i])
pred.red <- a.lp/(1+(totN.i/b.lp)^c.lp)
popmat[1,] <- fert.stoch
diag(popmat[2:age.max,]) <- s.stoch*pred.red
#popmat[age.max,age.max] <- 0
n.mat[,i+1] <- popmat %*% n.mat[,i]
n.mat[,i+1] <- n.mat[,i+1] + round(rnorm(1,mean=stray.cat.vec[s],sd=(0.05*stray.cat.vec[s]))*ssd, 0) #add stray cats into population vector
## harvest
if (i < 3) {
n.mat[,i+1] <- n.mat[,i+1] - round(stable.stage.dist(popmat) * round(sum(n.mat[,i+1])*0.6, 0), 0)
} else {
n.mat[,i+1] <- n.mat[,i+1] - round(stable.stage.dist(popmat) * round(sum(n.mat[,i+1])*0.5, 0), 0)
}
if (length(which(n.mat[,i+1] < 0)) > 0) {
n.mat[which(n.mat[,i+1] < 0), i+1] <- 0
}
} # end i loop
n.sums.mat[e,] <- ((as.vector(colSums(n.mat))/pop.found))
if (e %% itdiv==0) print(e)
} # end e loop
n.md <- apply(n.sums.mat, MARGIN=2, median, na.rm=T) # mean over all iterations
n.up <- apply(n.sums.mat, MARGIN=2, quantile, probs=0.975, na.rm=T) # upper over all iterations
n.lo <- apply(n.sums.mat, MARGIN=2, quantile, probs=0.025, na.rm=T) # lower over all iterations
final.md.out[s] <- n.md[t+1]
final.n.up.out[s] <- n.up[t+1]
final.n.lo.out[s] <- n.lo[t+1]
print(s)
} # s vec
plot(stray.cat.vec, final.md.out, pch=19, type="l", xlab="mean number of stray cats added/year", ylab="final N at end of projection interval")
lines(stray.cat.vec, final.n.up.out, lty=2, col="red")
lines(stray.cat.vec, final.n.lo.out, lty=2 , col="red")
totalN <- final.md.out*pop.found
final.md.table <- matrix(0, nrow = 5, ncol = length(stray.cat.vec)) #results matrix
final.md.table[1,] <- stray.cat.vec #how many stray cats added
final.md.table[2,] <- final.md.out #final median N proportionate
final.md.table[3,] <- totalN #final median N
final.md.table[4,] <- final.n.lo.out #final lower 95% confidence
final.md.table[5,] <- final.n.up.out #final upper 95% confidence
final.md.table #display table
###############################################################################################################################
##stopping early
###############################################################################################################################
##############################################################################################################
## main two-phase cull
###############################################################################################################
## stochatic projection with density feedback
## set storage matrices & vectors
n.sums.mat <- matrix(data = 0, nrow = iter, ncol = (t+1)) #storage matrix
for (e in 1:iter) {
popmat <- popmat.orig
n.mat <- matrix(0, nrow=age.max,ncol=(t+1))
n.mat[,1] <- init.vec
for (i in 1:t) {
# stochastic survival values
s.alpha <- estBetaParams(s.vec, s.sd.vec^2)$alpha
s.beta <- estBetaParams(s.vec, s.sd.vec^2)$beta
s.stoch <- rbeta(length(s.alpha), s.alpha, s.beta)
# stochastic fertilty sampler (gaussian)
fert.stch <- rnorm(length(popmat[,1]), popmat[1,], m.sd.vec)
fert.stoch <- ifelse(fert.stch < 0, 0, fert.stch)
totN.i <- sum(n.mat[,i])
pred.red <- a.lp/(1+(totN.i/b.lp)^c.lp)
popmat[1,] <- fert.stoch
diag(popmat[2:age.max,]) <- s.stoch*pred.red
n.mat[,i+1] <- popmat %*% n.mat[,i]
#harvest
if (i <3 ) {
n.mat[,i+1] <- n.mat[,i+1] - round(stable.stage.dist(popmat) * round(sum(n.mat[,i+1])*0.6, 0), 0)
} else {
n.mat[,i+1] <- n.mat[,i+1] - round(stable.stage.dist(popmat) * round(sum(n.mat[,i+1])*0.5, 0), 0)
}
if (length(which(n.mat[,i+1] < 0)) > 0) {
n.mat[which(n.mat[,i+1] < 0), i+1] <- 0
}
} # end i loop
n.sums.mat[e,] <- ((as.vector(colSums(n.mat))/pop.found))
if (e %% itdiv==0) print(e)
} # end e loop
n.md <- apply(n.sums.mat, MARGIN=2, median, na.rm=T) # mean over all iterations
n.up <- apply(n.sums.mat, MARGIN=2, quantile, probs=0.975, na.rm=T) # upper over all iterations
n.lo <- apply(n.sums.mat, MARGIN=2, quantile, probs=0.025, na.rm=T) # lower over all iterations
plot(yrs,n.md,type="l", main = "Min N with SD for untr pop", xlab="year", ylab="Minimum population", lwd=2, ylim=c(0.95*min(n.lo),1.05*max(n.up)))
lines(yrs,n.lo,lty=2,col="red",lwd=1.5)
lines(yrs,n.up,lty=2,col="red",lwd=1.5)
two.phase <- matrix(data=0, nrow = 2, ncol = length(yrs)) #results, proportional pop relative to year
two.phase[1,] <- yrs
two.phase[2,] <- n.md
two.phase
two.phase.N <- matrix (data = 0, nrow = 2, ncol = length(yrs)) #results, total pop relative to year
two.phase.N[1,] <- yrs
two.phase.N[2,] <- n.md*pop.found
two.phase.N
####################################################
## Stopping culling early
####################################################
iter <- 10000
itdiv <- iter/100
stopped.yrs.vec <- seq(3,11,1) #stopping cull after years 3-11, intervals of 1
t <- seq(2020,2100,1) #time
recov.med <- recov.lo <- recov.up <- rep(NA,length(stopped.yrs.vec)) #storage
for (s in 1:length(stopped.yrs.vec)) {
recovery.time <- rep(NA,iter) #storage for loop
for (e in 1:iter) {
n.mat <- matrix(0, nrow=age.max, ncol=(length(t)+1))
n.mat[,1] <- init.vec
popmat <- popmat.orig
#for (i in 1:4) {
for (i in 1:length(t)) {
# stochastic survival values
s.alpha <- estBetaParams(s.vec, s.sd.vec^2)$alpha
s.beta <- estBetaParams(s.vec, s.sd.vec^2)$beta
s.stoch <- rbeta(length(s.alpha), s.alpha, s.beta)
# stochastic fertilty sampler (gaussian)
fert.stch <- rnorm(length(popmat[,1]), popmat[1,], m.sd.vec)
fert.stoch <- ifelse(fert.stch < 0, 0, fert.stch)
totN.i <- sum(n.mat[,i])
pred.red <- a.lp/(1+(totN.i/b.lp)^c.lp)
popmat[1,] <- fert.stoch
diag(popmat[2:age.max,]) <- s.stoch*pred.red
# projection
n.mat[,i+1] <- popmat %*% n.mat[,i]
#harvest
if (i <= 11) {
harv.initial <- round(stable.stage.dist(popmat.orig) * round(sum(n.mat[,i+1])*0.6, 0), 0)
if (i <= 2) {
n.mat[,i+1] <- ifelse(n.mat[,i+1] - harv.initial < 0, 0, n.mat[,i+1] - harv.initial) #apply initial harvest
}
if (i > 2) {
harv.maint <- round(stable.stage.dist(popmat.orig) * round(sum(n.mat[,i+1])*0.5, 0), 0)
if (i <= stopped.yrs.vec[s]) {
n.mat[,i+1] <- ifelse(n.mat[,i+1] - harv.maint < 0, 0, n.mat[,i+1] - harv.maint)}
} #stop maintenance harvest at years 3-11 (stooped.yrs.vec)
} # end if
} # end i loop
recovery.time[e] <- which(colSums(n.mat[,-1]) >= sum(init.vec))[1]
if (e %% itdiv==0) print(e)
} # end e loop
recov.med[s] <- median(recovery.time, na.rm = T)
recov.lo[s] <- quantile(recovery.time, probs=0.025, na.rm=T)
recov.up[s] <- quantile(recovery.time, probs=0.975, na.rm=T)
print("#################")
print(paste("stop year = ", s, sep=""))
print("#################")
}
plot(stopped.yrs.vec, recov.med, type="l", xlab="stopped year", ylab="years to initial recovery",ylim=c(min(recov.lo),max(recov.up)))
lines(stopped.yrs.vec, recov.lo, lty=2, col="red")
lines(stopped.yrs.vec, recov.up, lty=2, col="red")
recovermat <- matrix(0, nrow = 4, ncol = length(stopped.yrs.vec))
recovermat[1,] <- stopped.yrs.vec
recovermat[2,] <- recov.lo
recovermat[3,] <- recov.med
recovermat[4,] <- recov.up