forked from dlusseau/scoti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_BPUE_metier.R
469 lines (314 loc) · 16.8 KB
/
monitor_BPUE_metier.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#####
##### this function monitors the simulated fishing year
####################################################################################################
# simulate sampling in a bycatch monitoring program
# sampling effort and allocation
# - vessels
# - hauls
# sampling design
# - stratify by metier
# - first sample vessels, then sample hauls
# properties of vessel / captain
# - refusal to accept onboard observer
# properties of monitoring event
# - observe haul or not
# - detection of bycatch
# - correct species identification
monitor_BPUE_metier <- function(
# note: sorted arguments on topic
# data frame with a simulated fishing year (see make_fishing_year_metier)
fishing = NA,
# overall monitoring params
# nsample: number of samples (monitoring events) to be taken from the fishing data
nsample = 1000,
# sampling effort
# p_monitor_boat: = pvessel??. Proportion of vessels monitored. In MS both terms are used.
p_monitor_boat = 0.1,
# pmonitor: proportion of hauls monitored for each vessel
pmonitor = 0.5,
# p_monitor_metier: proportion of monitoring allocated to (each?) métier
# Note: what is valid format? what if length > 1 (see also below when p_monitor_boat is multiplied by p_monitor_metier)
p_monitor_metier = 0.5,
# sampling design
# bymetier: is sampling stratified by metier?
bymetier = FALSE,
# boat_samp: ?? Guess: whether boat is sampled first, then events??
boat_samp = TRUE,
# refusal_rate: the probability that a vessel selected for monitoring refuses to engage
# refusal is replaced to introduce selective pressures (unclear)
# option 1 random, option 2 associated with nbycatch
refusal_rate = 0,
# quality of monitoring
# p_haul_obs: probability that a haul was observed by the observer
p_haul_obs = 1,
# detect_prob: detection probability of each individual in the bycatch event
# while at first, detect_prob is an independent draw on each individual in the haul, we can implement a decrease in detect_prob, with nbycatch increasing (on a log scale or similar)
detect_prob = 1,
# misclassification: probability of mis-identification of the bycaught species
# misclassification might come in later when we have multiple species which can be confused, and therefore individuals from nbycatch_sp_i can
#be taken to add to nbycatch_sp_j
misclassification = 0,
# BPUE_real: how is this parameterized?
# see simulation_example.R: BPUE_real = sum(fishing$nbycatch) / dim(fishing)[1]
BPUE_real = 0,
) {
# integrate observer procedure effect
# this is protocol influence process
# two potential effects:
# 1. haul level effect: unobserved bycatch when monitored
# lack of observation
# 2. decrease in the number of animal reported on a bycatch event
# incomplete observation, e.g. drop out, multiple location needed # unclear
# observation probability function post sampling
#second step: variance associated with observed ID # unclear
# number of metiers (n in MS)
nmetier = length(unique(fishing$metiers))
# pre-allocate array for result
# if sampling stratified by metier
if (bymetier == FALSE) {
# one row per sample
BPUE_est = array(NA, dim = nsample)
} else {
# one row per sample, one column per metier
BPUE_est = array(NA, dim = c(nsample, nmetier))
}
# sample / monitor the fishing data nsample times
for (j in 1:nsample) {
# if sampling not stratified by metier (?)
if (bymetier == FALSE) {
# if boat_samp is FALSE, sampling occurs at the fishing event level
# Note: what about sampling at trip level? I.e. all hauls during a fishing day?
if (boat_samp == FALSE) {
# sample monitored fishing events
monitored = sample(
# sample from row index of fishing data frame
x = c(1:dim(fishing)[1]), # or seq(nrow(fishing))
# number of items to sample:
# number of hauls (=rows of fishing) * proportion of hauls monitored for each vessel
n = floor(dim(fishing)[1] * pmonitor),
# sample without replacement
replace = FALSE)
# select hauls that were sampled for monitoring
fishing_monitored = fishing[monitored, ]
# sample hauls that observer failed to monitor
not_observed = sample(
# sample from row index of monitored fishing data frame
x = c(1:dim(fishing_monitored)[1]),
# number of haul to sample: number of rows * (1 - probability haul was observed)
n = floor(dim(fishing_monitored)[1] * (1 - p_haul_obs)),
# sample without replacement
replace = FALSE)
# for hauls that were not observed
# set bycatch (0/1) to 0
fishing_monitored$bycatch[not_observed] = 0
# set number of bycatch to 0
fishing_monitored$nbycatch[not_observed] = 0
# account for detection probability
# given true size of bycatch and detection probability of each individual
# calculate the number bycatch detected
fishing_monitored$nbycatch = sapply(fishing_monitored$nbycatch, function(x) rbinom(1, x, detect_prob))
# calculate estimated BPUE (for each monitor of the fishing data)
# sum number of bycatch / number of events monitored
BPUE_est[j] = (sum(fishing_monitored$nbycatch) / length(monitored))
# when boat_samp is TRUE: first sample vessels to be monitored, then hauls
} else {
# 1. sample vessels to be monitored
boat_sampled = sample(
# sample from vessel id
x = unique(fishing$boat),
# number of vessels to sample:
# number of vessels * Proportion of vessels monitored
size = floor(length(unique(fishing$boat)) * p_monitor_boat),
# sample without replacement
replace = FALSE)
## think about situations when boats are never going out in a year. at the moment, the observer programme allows to react and sample
## only those vessels that have been fishing at least once per year.
# 2. select vessels that accept observers
boat_sampled = sample(
# sample from sampled vessels
x = boat_sampled,
# number of vessels to sample:
# number of vessels * Proportion of vessels that accept (not refuse)
size = floor(length(boat_sampled) * (1 - refusal_rate)),
replace = FALSE)
# select sampled vessels from fishing data
fleet_sampled = fishing[fishing$boat %in% boat_sampled, ]
# from the selected vessels, select hauls to be monitored
monitored = sample(
# select from row index among the sampled vessels
c(1:dim(fleet_sampled)[1]),
# number of hauls to be monitored =
# number of rows in data of sampled vessels * proportion of hauls monitored for each vessel
size = floor(dim(fleet_sampled)[1] * pmonitor),
replace = FALSE) # sample without replacement (default, this arg can be removed)
# from data of selected vessels, select hauls to be monitored
fishing_monitored = fleet_sampled[monitored, ]
# among selects hauls determine hauls that where not observed
not_observed = sample(
# select from row index of hauls to be monitored
x = c(1:dim(fishing_monitored)[1]),
# number of haul to sample: number of rows * (1 - probability haul was observed)
size = floor(dim(fishing_monitored)[1] * (1 - p_haul_obs)))
# for unobserved hauls
# set bycatch (0/1) to 0
fishing_monitored$bycatch[not_observed] = 0
# set number of bycatch to 0
fishing_monitored$nbycatch[not_observed] = 0
# account for detection probability
# given true size of bycatch and detection probability of each individual
# calculate the number bycatch detected
fishing_monitored$nbycatch = sapply(fishing_monitored$nbycatch,function(x) rbinom(1, x, detect_prob))
# calculate estimated BPUE
# sum number of bycatch / number of events monitored
BPUE_est[j] = (sum(fishing_monitored$nbycatch) / length(monitored))
}
# if sampling stratified by metier (?)
} else {
# count number of metiers
nmetier = length(unique(fishing$metiers)) # for error catching later on
# if sampling occurs at the fishing event level
if (boat_samp == FALSE) {
# count number of metiers. Note: already calculated above
nmetier = length(unique(fishing$metiers)) # for error catching later on
# calculate probability that a haul in a certain metier is monitored
# proportion of hauls monitored for each vessel *
# proportion of monitoring allocated to (each?) métier
monitored_by_metier = pmonitor * p_monitor_metier
# sample monitored fishing events of metier 1
# note: why not loop over all metiers?
monitored = sample(
# sample from row index of fishing data frame
# Note: why not sample from row numbers?
x = as.numeric(row.names(fishing[fishing$metiers == 1, ])),
# number of items to sample:
# number of hauls ( =rows of fishing) * proportion of hauls monitored for each vessel
# Note: redundant calculation of row indexes
prob = floor(length(row.names(fishing[fishing$metiers == 1, ])) * monitored_by_metier[1]))
# for each metier, sample monitored fishing events
for (i in 2:nmetier) {
# combine with 'monitored'
monitored = c(
monitored,
# sample monitored fishing events
sample(
# sample from row index of fishing data frame.
x = as.numeric(row.names(fishing[fishing$metiers == i, ])),
# number of items to sample:
# number of hauls (rows in 'fishing') * proportion of hauls monitored for each vessel
# Note: redundant calculation of row indexes
size = floor(length(row.names(fishing[fishing$metiers == i, ])) * monitored_by_metier[i])
)
)
}
# select monitored hauls from fishing
fishing_monitored = fishing[monitored, ]
# among selects hauls determine hauls that where not observed
not_observed = sample(
# select from row index of hauls to be monitored
x = c(1:dim(fishing_monitored)[1]),
# number of haul to sample: number of rows * (1 - probability haul was observed)
size = floor(dim(fishing_monitored)[1] * (1-p_haul_obs)))
# for hauls that were not observed
# set bycatch (0/1) to 0
fishing_monitored$bycatch[not_observed] = 0
# set number of bycatch to 0
fishing_monitored$nbycatch[not_observed] = 0
# account for detection probability of individuals
# given true size of bycatch and detection probability of each individual
# calculate the number bycatch detected
fishing_monitored$nbycatch = sapply(fishing_monitored$nbycatch, function(x) rbinom(1, x, detect_prob))
# for each metier, calculated estimated BPUE
# note: this could be done outside the loop
BPUE_est[j, ] = (
# calculate estimated BPUE
# sum number of bycatch / number of events monitored
tapply(fishing_monitored$nbycatch, fishing_monitored$metiers, sum) /
tapply(fishing_monitored$nbycatch, fishing_monitored$metiers, length))
# when boat_samp is TRUE: first sample vessels to be monitored, then hauls
} else {
# joint probability that a boat is monitored AND a metier is monitored
# p_monitor_metier: proportion of monitoring allocated to (each?) métier
# Note: here p_monitor_metier is recycled over p_monitor_boat.
# will this work with p_monitor_metier as a matrix, like in 'simulation_example.R'?
boat_monitored_by_metier = p_monitor_boat * p_monitor_metier
# sample vessels to be monitored
# 1. sample vessels to be monitored from metier 1
# Note: this could be put in loop as well?
boat_sampled = sample(
# sample from vessel id
x = unique(fishing$boat[fishing$metiers == 1]),
# number of vessels to sample:
# number of vessels * Proportion of vessels monitored
size = ceiling(length(unique(fishing$boat[fishing$metiers == 1])) * boat_monitored_by_metier[1]))
# for each metier, sample monitored fishing events
for (i in 2:nmetier) {
# combine with 'boat_sampled'
boat_sampled = c(
boat_sampled,
# sample monitored boats
sample(
# sample from vessel id
x = unique(fishing$boat[fishing$metiers == i]),
# number of vessels to sample:
# number of vessels * Proportion of vessels monitored
size = ceiling(length(unique(fishing$boat[fishing$metiers == i])) * boat_monitored_by_metier[1]))
)
}
# 2. sample vessels that accept observers
boat_sampled = sample(
# sample from sampled vessels
x = boat_sampled,
# number of vessels to sample:
# number of vessels * Proportion of vessels that accept (not refuse)
size = ceiling(length(boat_sampled) * (1 - refusal_rate)))
# select sampled vessels from fishing data
fleet_sampled = fishing[fishing$boat %in% boat_sampled, ]
# sample hauls
# from the selected vessels, sample hauls to be monitored
monitored = sample(
# select from row index among the sampled vessels
x = c(1:dim(fleet_sampled)[1]),
# number of hauls to be monitored =
# number of rows in data of sampled vessels * proportion of hauls monitored for each vessel
size = floor(dim(fleet_sampled)[1] * pmonitor))
# from data of selected vessels, select hauls to be monitored
fishing_monitored = fleet_sampled[monitored, ]
# among selects hauls determine hauls that where not observed
not_observed = sample(
# select from row index of hauls to be monitored
x = c(1:dim(fishing_monitored)[1]),
# number of haul to sample: number of rows * (1 - probability haul was observed)
floor(dim(fishing_monitored)[1] * (1-p_haul_obs)))
# for unobserved hauls
# set bycatch (0/1) to 0
fishing_monitored$bycatch[not_observed] = 0
# set number of bycatch to 0
fishing_monitored$nbycatch[not_observed] = 0
# account for detection probability
# given true size of bycatch and detection probability of each individual
# calculate the number bycatch detected
fishing_monitored$nbycatch = sapply(fishing_monitored$nbycatch, function(x) rbinom(1, x, detect_prob))
# calculate estimated BPUE
# sum number of bycatch / number of events monitored
BPUE_est[j, ] = (tapply(fishing_monitored$nbycatch, fishing_monitored$metiers, sum) /
tapply(fishing_monitored$nbycatch, fishing_monitored$metiers, length))
}
} #metier else bracket
} # sample iteration loop
# calculate estimated BPUE
# not stratified by metier
if (bymetier == FALSE) {
# mean
BPUE_est_mean = mean(BPUE_est, na.rm = TRUE) #if bymetier is TRUE then 2 element vector
# CV
BPUE_est_CV = sd(BPUE_est, na.rm = TRUE) / mean(BPUE_est, na.rm = TRUE)
# stratified by metier
} else {
# mean
BPUE_est_mean = colMeans(BPUE_est, na.rm = TRUE) #if bymetier is TRUE then 2 element vector
# CV
# loop over columns to calculate sd
BPUE_est_CV = apply(BPUE_est, 2, function(x) sd(x, na.rm = TRUE)) / colMeans(BPUE_est, na.rm = TRUE)
}
return(list(BPUE_est = BPUE_est_mean, CV = BPUE_est_CV))
}