-
Notifications
You must be signed in to change notification settings - Fork 1
/
funs.R
1285 lines (1062 loc) · 43.4 KB
/
funs.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### ------------------------------------------------------------------------ ###
### run fmle for FLSR in parallel ####
### ------------------------------------------------------------------------ ###
fmle_parallel <- function(sr, cl, seed = NULL, ...) {
### split into parts
it_parts <- split(1:dim(sr)[6], cut(1:dim(sr)[6], length(cl)))
names(it_parts) <- NULL
### split sr into junks
sr_chunks <- lapply(it_parts, function(x) {
FLCore::iter(sr, x)
})
### set RNG seed
if (!is.null(seed)) {
clusterEvalQ(cl = cl, expr = {set.seed(seed)})
}
### fit model to each junk
fits <- foreach(sr_chunk = sr_chunks, .packages = "FLCore") %dopar% {
fmle(sr_chunk, ...)
}
### combine into single object
### slots rec, ssb, covar, logerrror, model, logl, gr, distribution, inital,
### name, desc, range
### do not need to be changed
logLik(sr) <- logLik(fits[[1]])
sr@logLik[seq(dims(sr)$iter)] <- c(sapply(fits, logLik))
fitted(sr) <- do.call(FLCore::combine, lapply(fits, "fitted"))
residuals(sr) <- do.call(FLCore::combine, lapply(fits, "residuals"))
vcov(sr) <- array(data = unlist(lapply(fits, vcov)),
dim = c(dim(fits[[1]]@vcov)[1],
dim(fits[[1]]@vcov)[2],
dims(sr)$iter),
dimnames = list(dimnames(fits[[1]]@vcov)[[1]],
dimnames(fits[[1]]@vcov)[[2]],
iter = ac(dimnames(sr)$iter)))
params(sr) <- propagate(params(sr), dims(sr)$iter)
params(sr)[] <- unlist(lapply(fits, function(x) {
split(x@params@.Data, slice.index(x@params, MARGIN = 2))
}))
hessian(sr) <- array(data = unlist(lapply(fits, hessian)),
dim = c(dim(fits[[1]]@hessian)[1],
dim(fits[[1]]@hessian)[2],
dims(sr)$iter),
dimnames = list(dimnames(fits[[1]]@hessian)[[1]],
dimnames(fits[[1]]@hessian)[[2]],
iter = ac(dimnames(sr)$iter)))
details(sr) <- details(fits[[1]])
return(sr)
}
### ------------------------------------------------------------------------ ###
### calculate survey index/indices from stock ####
### ------------------------------------------------------------------------ ###
#' calculate survey index/indices from FLStock
#'
#' This function calculates survey indices from the numbers at age of an
#' FLStock object
#'
#' @param stk Object of class \linkS4class{FLStock} with stock and fishery data.
#' @param idx Object of class \linkS4class{FLIndices} or \linkS4class{FLIndex}.
#' @param use_q If \code{TRUE} index numbers are calculated by multiplying
#' numbers at age in the stock with the catchability.
#' @param use_time If \code{TRUE} observed numbers in the survey are corrected
#' for fishing and natural mortality.
#'
#' @return An object of class \code{FLIndex} or \code{FLIndex} with the survey
#' index stored in the \code{index} slot.
#'
#' @export
setGeneric("calc_survey", function(stk, idx, use_q = TRUE, use_time = TRUE,
use_wt = FALSE) {
standardGeneric("calc_survey")
})
### stk = FLStock, idx = FLIndices
#' @rdname calc_survey
setMethod(f = "calc_survey",
signature = signature(stk = "FLStock", idx = "FLIndices"),
definition = function(stk, idx, use_q = TRUE, use_time = TRUE,
use_wt = FALSE) {
### apply function to every element of FLIndices
lapply(X = idx, FUN = calc_survey_ind, stk = stk, use_q = use_q,
use_time = use_time, use_wt = use_wt)
})
### stk = FLStock, idx = FLIndex
#' @rdname calc_survey
setMethod(f = "calc_survey",
signature = signature(stk = "FLStock", idx = "FLIndex"),
definition = function(stk, idx, use_q = TRUE, use_time = TRUE,
use_wt = FALSE) {
calc_survey_ind(stk = stk, idx = idx, use_q = use_q,
use_time = use_time, use_wt = use_wt)
})
calc_survey_ind <- function(stk, idx,
use_q = TRUE, ### catchability
use_time = TRUE, ### timing of survey
use_wt = FALSE ### use weights?
) {
### find ranges for years, ages & iters
ages <- intersect(dimnames(index(idx))$age, dimnames(stock.n(stk))$age)
years <- intersect(dimnames(index(idx))$year, dimnames(stock.n(stk))$year)
iter <- intersect(dimnames(index(idx))$iter, dimnames(stock.n(stk))$iter)
### timing of survey
if (isTRUE(use_time)) {
### use mean of fishing period
time <- mean(range(idx)[c("startf", "endf")])
} else {
### otherwise assume beginning of year
time <- 0
}
### extract stock numbers for requested/available dimensions
index.n <- stock.n(stk)[ac(ages), ac(years),,,, ac(iter)]
### get Z = M & F
Z <- m(stk)[ac(ages), ac(years),,,, ac(iter)] +
harvest(stk)[ac(ages), ac(years),,,, ac(iter)]
### estimate stock numbers at time of survey
index.n <- index.n * exp(-time * Z)
catch.n(idx)[ac(ages), ac(years),,,, ac(iter)] <- index.n
### add catchability, if requested
if (isTRUE(use_q)) {
index.n <- index.n * index.q(idx)[ac(ages), ac(years),,,, ac(iter)]
}
### use biomass (i.e. include weights)?
if (isTRUE(use_wt)) {
index.n <- index.n * catch.wt(idx)[ac(ages), ac(years),,,, ac(iter)]
}
### insert values into index
index(idx)[ac(ages), ac(years),,,, ac(iter)] <- index.n
return(idx)
}
### ------------------------------------------------------------------------ ###
### observations ####
### ------------------------------------------------------------------------ ###
obs_generic <- function(stk, observations, deviances, args, tracking,
biomass = FALSE,
simple_idx = FALSE, ### simple index calculation
ssb_idx = FALSE, tsb_idx = FALSE, ### use SSB idx
idx_dev = FALSE, ### add index deviation?
PA_status = FALSE, ### for PA buffer
PA_status_dev = FALSE,
PA_Bmsy = FALSE, PA_Fmsy = FALSE,
use_stk_oem = FALSE, ### biological parameters, wts etc
use_catch_residuals = FALSE,
use_idx_residuals = FALSE,
cut_idx = FALSE, ### cut off indices years after ay
catch_timing = -1,
idx_timing = -1,
use_wt = FALSE,
use_age_idcs = NULL, ### survey to use calc_survey()
alks = NULL, ### age-length keys
biomass_index = NULL, ### which survey used for biomass?
length_idx = FALSE, ### estimate length index?
lngth = FALSE, ### simpler length index
lngth_dev = FALSE, ### deviation for lngth
Lc = 0, ### length at first capture
lngth_samples = 100, ### number of length samples
dd_M = FALSE, ### density dependent M -> keyruns
dd_M_relation = NULL,
dd_M_yr = NULL,
dd_M_period = 3,
...) {
ay <- args$ay
### Density-dependent M
### Calculate 3-year means of M from the OM on key-run years
### to simulate the SAM process
if (isTRUE(dd_M)) {
if (isTRUE(((ay - dd_M_yr) %% dd_M_period) == 0)) {
m(observations$stk)[, ac(ay:(ay + 2))] <-
yearMeans(m(stk)[, ac((ay - 3):(ay - 1))])
}
}
### create object for observed stock
if (!isTRUE(use_stk_oem)) {
### use OM as template
stk0 <- stk
} else {
### otherwise use observed stock provided in observations object
### this can include different biological data, e.g. weights at age
stk0 <- observations$stk
### update fishery data
catch.n(stk0) <- catch.n(stk)
catch(stk0) <- catch(stk)
discards.n(stk0) <- discards.n(stk)
discards(stk0) <- discards(stk)
landings.n(stk0) <- landings.n(stk)
landings(stk0) <- landings(stk)
}
### add uncertainty to catch
if (isTRUE(use_catch_residuals)) {
### implement for catch at age
catch.n(stk0) <- catch.n(stk) * deviances$stk$catch.dev
### split catch into discards and landings, based on landing fraction
landings.n(stk0) <- catch.n(stk0) * (landings.n(stk) / catch.n(stk))
discards.n(stk0) <- catch.n(stk0) * (1 - landings.n(stk) / catch.n(stk))
### update total catch/discards/landings
catch(stk0) <- computeCatch(stk0)
landings(stk0) <- computeLandings(stk0)
discards(stk0) <- computeDiscards(stk0)
}
### calculate age indices
if (is.null(use_age_idcs)) use_age_idcs <- names(observations$idx)
idcs_new <- calc_survey(stk = stk,
idx = observations$idx[use_age_idcs],
use_wt = use_wt)
### bug in FLIndices, need to iterate through them when replacing...
for (i in use_age_idcs) {
observations$idx[[i]] <- idcs_new[[i]]
}
### observed survey
idx0 <- observations$idx
### add uncertainty to observed age indices
if (isTRUE(use_idx_residuals)) {
for (idx_i in use_age_idcs) {
index(idx0[[idx_i]]) <- index(idx0[[idx_i]]) * deviances$idx[[idx_i]]
}
}
### cut off trailing years? used when running stock assessment
if (isTRUE(cut_idx)) {
### cut off years
### workaround for NS cod: survey until intermediate year, but catch stops
### 1 year earlier
### slots such as natural mortality, maturity need to be kept, otherwise
### SAM will fall over
if (any(idx_timing > catch_timing)) {
### keep stock until last survey data year
stk0 <- window(stk0, end = ay + max(idx_timing))
### find years to remove
yrs_remove <- (ay + catch_timing + 1):ay
### remove catch data
catch(stk0)[, ac(yrs_remove)] <- NA
catch.n(stk0)[, ac(yrs_remove)] <- NA
catch.wt(stk0)[, ac(yrs_remove)] <- NA
landings(stk0)[, ac(yrs_remove)] <- NA
landings.n(stk0)[, ac(yrs_remove)] <- NA
landings.wt(stk0)[, ac(yrs_remove)] <- NA
discards(stk0)[, ac(yrs_remove)] <- NA
discards.n(stk0)[, ac(yrs_remove)] <- NA
discards.wt(stk0)[, ac(yrs_remove)] <- NA
} else {
stk0 <- window(stk0, end = ay + catch_timing)
}
### timing of survey
### 0: intermediate/assessment year
### <0: fewer years available & vice versa
if (length(idx_timing) < length(idx0)) {
idx_timing <- rep(idx_timing, length(idx0))
}
### restrict years for indices based on timing
idx0 <- lapply(seq_along(idx0), function(x) {
window(idx0[[x]], end = ay + idx_timing[x])
})
idx0 <- FLIndices(idx0) ### restore class
names(idx0) <- names(observations$idx)
}
### create biomass index
if (!is.null(biomass_index)) {
idxB_yrs <- intersect(dimnames(index(observations$idx$idxB))$year,
dimnames(index(observations$idx[[biomass_index]]))$year)
index(observations$idx$idxB)[, idxB_yrs] <- quantSums(index(observations$idx[[biomass_index]]))[, idxB_yrs]
index(idx0$idxB)[, idxB_yrs] <- quantSums(index(idx0[[biomass_index]]))[, idxB_yrs]
}
### catch length index
if (isTRUE(length_idx)) {
### find years which need updating
### do not update beyond year ay
### always include/overwrite ay (in case MSE start earlier)
yrs_update <- which(is.na(iterMeans(window(index(idx0$idxL), end = ay))))
yrs_update <- unique(c(dimnames(idx0$idxL)$year[yrs_update], ay))
### match ALK year with data year
lmean <- full_join(
### use observed catch
x = as.data.frame(catch.n(stk0)[, ac(yrs_update)]) %>%
select(year, age, iter, data) %>%
mutate(data = ifelse(data < 0, 0, data)) %>% ### shouldn't happen...
rename("caa" = "data") %>%
mutate(iter = as.numeric(as.character(iter))) %>%
mutate(caa = caa + .Machine$double.eps), ### avoid 0s
y = as.data.frame(deviances$idx$alk_yrs[, ac(yrs_update)]) %>%
select(year, iter, data) %>%
rename("alk_year" = "data") %>%
mutate(iter = as.numeric(as.character(iter))),
by = c("year", "iter")) %>%
### merge with ALKs
left_join(alks %>% rename("alk_year" = "year"),
by = c("age", "alk_year")) %>%
### calculate numbers at length
mutate(cal = caa * freq) %>%
### keep only numbers where length >= Lc
filter(length >= Lc) %>%
### mean catch length above Lc
group_by(year, iter) %>%
summarise(data = mean(sample(x = length, prob = cal,
size = lngth_samples, replace = TRUE)),
.groups = "keep") %>%
arrange(as.numeric(as.character(iter)))
### save
index(observations$idx$idxL[, ac(yrs_update)]) <- as.FLQuant(lmean)
index(idx0$idxL) <- index(observations$idx$idxL)
}
### use SSB as index?
if (isTRUE(ssb_idx)) {
index(observations$idx$idxB) <- ssb(observations$stk)
### TSB?
} else if (isTRUE(tsb_idx)) {
index(observations$idx$idxB) <- tsb(observations$stk)
### otherwise calculate biomass index
} else if (isTRUE(simple_idx)) {
index(observations$idx$idxB) <- quantSums(stk@stock.n * stk@stock.wt *
index(observations$idx$sel))
}
### simpler length index (for compatibility)
if (isTRUE(lngth)) {
index(idx0$idxL) <- index(observations$idx$idxL) <-
lmean(stk = stk, params = lngth_par)
}
### stock status for PA buffer?
if (isTRUE(PA_status)) {
index(observations$idx$PA_status)[] <- ssb(stk) > 0.5*PA_Bmsy &
fbar(stk) < PA_Fmsy
}
### add more deviances to index?
if (isTRUE(idx_dev)) {
if (isTRUE(ssb_idx) | isTRUE(tsb_idx)) {
index(idx0$idxB) <- index(observations$idx$idxB) * deviances$idx$idxB
} else {
index(idx0$idxB) <- quantSums(stk@stock.n * stk@stock.wt *
observations$idx$sel * deviances$idx$sel)
if (isTRUE("idxB" %in% names(deviances$idx)) &
all.equal(dim(deviances$idx$idxB), dim(index(idx0$idxB))))
index(idx0$idxB) <- index(idx0$idxB) * deviances$idx$idxB
}
}
### uncertainty for catch length
if (isTRUE(lngth) & isTRUE(lngth_dev)) {
index(idx0$idxL) <- index(observations$idx$idxL) * deviances$idx$idxL
}
### uncertainty for stock status for PA buffer
if (isTRUE(PA_status) & isTRUE(PA_status_dev)) {
index(idx0$PA_status) <- ifelse(index(observations$idx$PA_status) == TRUE,
deviances$idx$PA_status["positive", ],
deviances$idx$PA_status["negative", ])
}
return(list(stk = stk0, idx = idx0, observations = observations,
tracking = tracking))
}
### dummmy oem without returning data
### used for constant F projections
oem_dummy <- function(stk, deviances, observations, args, tracking, ...) {
return(list(stk = stk, idx = FLIndices(FLIndex()),
observations = observations, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### estimator ####
### ------------------------------------------------------------------------ ###
est_comps <- function(stk, idx, tracking, args,
comp_r = FALSE, comp_f = FALSE, comp_b = FALSE,
comp_i = FALSE, comp_c = TRUE, comp_m = FALSE,
comp_hr = FALSE,
idxB_lag = 1, idxB_range_1 = 2, idxB_range_2 = 3,
idxB_range_3 = 1,
catch_lag = 1, catch_range = 1,
Lref, I_trigger,
idxL_lag = 1, idxL_range = 1,
pa_buffer = FALSE, pa_size = 0.8, pa_duration = 3,
Bmsy = NA,
FLXSA = FALSE, ### run FLXSA?
FLXSA_control = NULL,
FLXSA_landings = FALSE, ### landings only?
FLXSA_idcs = NULL, ### indices to use for XSA
FLXSA_stf = FALSE, ### add short-term forecast?
FLXSA_Btrigger = 0, ### SSB trigger for PA buffer
FLXSA_Ftrigger = Inf, ### F trigger for PA buffer
...) {
ay <- args$ay
### prepare biomass index
if (isTRUE(is(idx$idxB, "FLIndex"))) {
idxB <- index(idx$idxB)
} else {
idxB <- idx$idxB
}
### prepare length index
if (isTRUE(is(idx$idxL, "FLIndex"))) {
idxL <- index(idx$idxL)
} else {
idxL <- idx$idxL
}
if (isTRUE(FLXSA)) {
### prepare stock
stk_FLXSA <- stk
### use only landings in assessment?
if (isTRUE(FLXSA_landings)) {
discards.n(stk_FLXSA) <- 0
discards(stk_FLXSA) <- computeDiscards(stk_FLXSA)
catch.n(stk_FLXSA) <- landings.n(stk_FLXSA)
catch(stk_FLXSA) <- landings(stk_FLXSA)
}
### prepare indices
if (is.null(FLXSA_idcs)) FLXSA_idcs <- names(idx)
idx_FLXSA <- idx[FLXSA_idcs]
### crop years
stk_FLXSA <- window(stk_FLXSA, end = ay - 1)
idx_FLXSA <- window(idx_FLXSA, end = ay - 1)
### run FLXSA
xsa_res <- FLXSA(stock = stk_FLXSA, indices = idx_FLXSA,
control = FLXSA_control)
stk_FLXSA@stock.n[] <- xsa_res@stock.n
stock(stk_FLXSA) <- computeStock(stk_FLXSA)
stk_FLXSA@harvest[] <- xsa_res@harvest
### add short-term forecast to get SSB in following year?
if (isTRUE(FLXSA_stf)) {
### set up control object
stf_ctrl <- fwdControl(data.frame(year = ay,
value = 0, ### dummy value
quant = "f"))
### define stf recruitment
### constant value recruitment model, level defined by deviances
stf_rec_level <- apply(rec(stk_FLXSA), 6, function(x) exp(mean(log(x))))
stf_rec_level <- FLQuant(c(stf_rec_level),
dimnames = list(year = ay, iter = dimnames(stk_FLXSA)$iter))
stf_sr <- FLSR(model = "geomean", params = FLPar(1))
### extend stock by 1 year
stk_FLXSA <- stf(stk_FLXSA, 1)
### forecast
stk_FLXSA <- fwd(object = stk_FLXSA, control = stf_ctrl,
sr = stf_sr, deviances = stf_rec_level)
dimnames(stk_FLXSA) <- list(iter = dimnames(stk)$iter) ### fix iter names
}
### use SSB as stock index
idxB <- ssb(stk_FLXSA)
### overwrite PA buffer indicator, base on FLXSA results
### compare SSB to MSYBtrigger and Fbar to Fmsy
index(idx$PA_status)[] <- 0
### SSB status
PA_status_SSB <- ssb(stk_FLXSA) >= FLXSA_Btrigger
### Fbar status
PA_status_F <- fbar(stk_FLXSA) <= FLXSA_Ftrigger
### shift by 1 year because of stf
if (isTRUE(FLXSA_stf)) {
### remove first year because of shift
PA_status_SSB <- PA_status_SSB[, -1]
dimnames(PA_status_SSB)$year <- ac(an(dimnames(PA_status_SSB)$year) - 1)
### remove last year for F
### (no data, and to adapt dimension to PA_status_SSB)
PA_status_F <- PA_status_F[, -dims(PA_status_F)$year]
}
### combine SSB and F evaluation
index(idx$PA_status)[, dimnames(PA_status_F)$year] <-
(PA_status_F & PA_status_SSB)
}
### component r: index trend
if (isTRUE(comp_r)) {
r_res <- est_r(idx = idxB, ay = ay,
idxB_lag = idxB_lag, idxB_range_1 = idxB_range_1,
idxB_range_2 = idxB_range_2)
} else {
r_res <- 1
}
tracking[[1]]["comp_r", ac(ay)] <- r_res
### component f: length data
if (isTRUE(comp_f)) {
f_res <- est_f(idx = idxL, ay = ay,
Lref = Lref, idxL_range = idxL_range, idxL_lag = idxL_lag)
} else {
f_res <- 1
}
tracking[[1]]["comp_f", ac(ay)] <- f_res
### component b: biomass safeguard
if (isTRUE(comp_b)) {
b_res <- est_b(idx = idxB, ay = ay,
I_trigger = I_trigger, idxB_lag = idxB_lag,
idxB_range_3 = idxB_range_3)
} else {
b_res <- 1
}
### PA buffer
if (isTRUE(pa_buffer)) {
b_res <- est_pa(idx = index(idx$PA_status), ay = ay,
tracking = tracking, idxB_lag = idxB_lag,
pa_size = pa_size, pa_duration = pa_duration)
}
tracking[[1]]["comp_b", ac(ay)] <- b_res
### component i: index value
if (isTRUE(comp_i)) {
i_res <- est_i(idx = idxB, ay = ay,
idxB_lag = idxB_lag, idxB_range_3 = idxB_range_3)
} else {
i_res <- 1
}
tracking[[1]]["comp_i", ac(ay)] <- i_res
### current catch
if (isTRUE(comp_c)) {
c_res <- est_c(ay = ay, catch = catch(stk), catch_lag = catch_lag,
catch_range = catch_range)
} else {
c_res <- 1
}
tracking[[1]]["comp_c", ac(ay)] <- c_res
### component m: multiplier
if (!isFALSE(comp_m)) {
m_res <- comp_m
### subset to iteration when simultion is split into blocks
if (isTRUE(length(comp_m) > dims(stk)$iter)) {
m_res <- comp_m[as.numeric(dimnames(stk)$iter)]
}
} else {
m_res <- 1
}
tracking[[1]]["multiplier", ac(ay)] <- m_res
### component hr: harvest rate (catch/idx)
if (!isFALSE(comp_hr)) {
hr_res <- comp_hr
### subset to iteration when simultion is split into blocks
if (isTRUE(length(comp_hr) > dims(stk)$iter)) {
hr_res <- comp_hr[as.numeric(dimnames(stk)$iter)]
}
} else {
hr_res <- 1
}
tracking[[1]]["comp_hr", ac(ay)] <- hr_res
return(list(stk = stk, tracking = tracking))
}
### biomass index trend
est_r <- function(idx, ay,
idxB_lag, idxB_range_1, idxB_range_2,
...) {
### index ratio
yrs_a <- seq(to = c(ay - idxB_lag), length.out = idxB_range_1)
yrs_b <- seq(to = min(yrs_a) - 1, length.out = idxB_range_2)
idx_a <- yearMeans(idx[, ac(yrs_a)])
idx_b <- yearMeans(idx[, ac(yrs_b)])
idx_ratio <- c(idx_a / idx_b)
return(idx_ratio)
}
### length data
est_f <- function(idx, ay,
Lref, idxL_range, idxL_lag,
...) {
### if fewer iterations provided expand
if (isTRUE(length(Lref) < dims(idx)$iter)) {
Lref <- rep(Lref, dims(idx)$iter)
### if more iterations provided, subset
} else if (isTRUE(length(Lref) > dims(idx)$iter)) {
Lref <- Lref[an(dimnames(idx)$iter)]
}
### get mean length in catch
idx_yrs <- seq(to = ay - idxL_range, length.out = idxL_lag)
idx_mean <- yearMeans(idx[, ac(idx_yrs)])
### length relative to reference
idx_ratio <- c(idx_mean / Lref)
### avoid negative values
idx_ratio <- ifelse(idx_ratio > 0, idx_ratio, 0)
### avoid NAs, happens if catch = 0
idx_ratio <- ifelse(is.na(idx_ratio), 1, idx_ratio)
return(idx_ratio)
}
### biomass index trend
est_b <- function(idx, ay,
I_trigger, idxB_lag, idxB_range_3,
...) {
### if fewer iterations provided expand
if (isTRUE(length(I_trigger) < dims(idx)$iter)) {
I_trigger <- rep(I_trigger, dims(idx)$iter)
### if more iterations provided, subset
} else if (isTRUE(length(I_trigger) > dims(idx)$iter)) {
I_trigger <- I_trigger[an(dimnames(idx)$iter)]
}
### calculate index mean
idx_yrs <- seq(to = ay - idxB_lag, length.out = idxB_range_3)
idx_mean <- yearMeans(idx[, ac(idx_yrs)])
### ratio
idx_ratio <- c(idx_mean / I_trigger)
### b is 1 or smaller
idx_ratio <- ifelse(idx_ratio < 1, idx_ratio, 1)
return(idx_ratio)
}
### biomass index trend
est_pa <- function(idx, ay, tracking, pa_size, pa_duration, idxB_lag,
...) {
### find last year in which buffer was applied
last <- apply(tracking[[1]]["comp_b",,, drop = FALSE], 6,
FUN = function(x) {#browser()
### positions (years) where buffer was applied
yr <- dimnames(x)$year[which(x < 1)]
### return -Inf if buffer was never applied
ifelse(length(yr) > 0, as.numeric(yr), -Inf)
})
### find iterations to check
pos_check <- which(last <= (ay - pa_duration))
### find negative stock status (SSB<0.5Bmsy or F>Fmsy)
pos_negative <- which(idx[, ac(ay - idxB_lag)] == 0)
### apply only if buffer applications need to be checked and status is negative
pos_apply <- intersect(pos_check, pos_negative)
return(ifelse(seq(dims(last)$iter) %in% pos_apply, pa_size, 1))
}
### index value
est_i <- function(idx, ay,
idxB_lag, idxB_range_3,
...) {
### index ratio
yrs_r <- seq(to = c(ay - idxB_lag), length.out = idxB_range_3)
idx_i <- yearMeans(idx[, ac(yrs_r)])
return(idx_i)
}
### recent catch
est_c <- function(catch, ay,
catch_lag, catch_range,
...) {
catch_yrs <- seq(to = ay - catch_lag, length.out = catch_range)
catch_current <- yearMeans(catch[, ac(catch_yrs)])
return(catch_current)
}
### ------------------------------------------------------------------------ ###
### phcr ####
### ------------------------------------------------------------------------ ###
### parametrization of HCR
phcr_comps <- function(tracking, args,
exp_r = 1, exp_f = 1, exp_b = 1,
...){
ay <- args$ay
hcrpars <- tracking[[1]][c("comp_r", "comp_f", "comp_b", "comp_i",
"comp_hr", "comp_c", "multiplier",
"exp_r", "exp_f", "exp_b"), ac(ay)]
hcrpars <- FLPar(hcrpars)
hcrpars["exp_r", ] <- exp_r
hcrpars["exp_f", ] <- exp_f
hcrpars["exp_b", ] <- exp_b
if (exp_r != 1) tracking[[1]]["exp_r", ] <- exp_r
if (exp_f != 1) tracking[[1]]["exp_f", ] <- exp_f
if (exp_b != 1) tracking[[1]]["exp_b", ] <- exp_b
### return results
return(list(tracking = tracking, hcrpars = hcrpars))
}
### ------------------------------------------------------------------------ ###
### hcr ####
### ------------------------------------------------------------------------ ###
### apply catch rule
hcr_comps <- function(hcrpars, args, tracking, interval = 2,
...) {
ay <- args$ay ### current year
iy <- args$iy ### first simulation year
### check if new advice requested
if ((ay - iy) %% interval == 0) {
### calculate advice
advice <- hcrpars["comp_c", ] *
(hcrpars["comp_r", ]^hcrpars["exp_r", ]) *
(hcrpars["comp_f", ]^hcrpars["exp_f", ]) *
(hcrpars["comp_b", ]^hcrpars["exp_b", ]) *
hcrpars["comp_i"] *
hcrpars["comp_hr"] *
hcrpars["multiplier", ]
#advice <- apply(X = hcrpars, MARGIN = 6, prod, na.rm = TRUE)
} else {
### use last year's advice
advice <- tracking[[1]]["hcr", ac(ay - 1)]
}
advice <- FLQuant(c(advice),
dimnames = list(year = ay + 1,
iter = seq(dim(advice)[6])))
ctrl <- fwdControl(target = advice, quant = "catch")
return(list(ctrl = ctrl, tracking = tracking))
}
### constant F
fixedF_hcr <- function(stk, ftrg, args, tracking){
ay <- args$ay
ctrl <- fwdControl(year = ay + 1, quant = "fbar", value = c(ftrg))
list(ctrl = ctrl, tracking = tracking)
}
### ------------------------------------------------------------------------ ###
### implementation ####
### ------------------------------------------------------------------------ ###
### no need to convert, already catch in tonnes
### apply TAC constraint, if required
is_comps <- function(ctrl, args, tracking, interval = 2,
upper_constraint = Inf, lower_constraint = 0,
cap_below_b = TRUE, ...) {
ay <- args$ay ### current year
iy <- args$iy ### first simulation year
advice <- ctrl@iters[which(ctrl@target$year == (ay + 1)),
"value", ]
### check if new advice requested
if ((ay - iy) %% interval == 0) {
### apply TAC constraint, if requested
if (!is.infinite(upper_constraint) | lower_constraint != 0) {
### get last advice
if (isTRUE(ay == iy)) {
### use OM value in first year of projection
adv_last <- tracking[[1]]["C.om", ac(iy)]
} else {
adv_last <- tracking[[1]]["isys", ac(ay - 1)]
}
### ratio of new advice/last advice
adv_ratio <- advice/adv_last
### upper constraint
if (!is.infinite(upper_constraint)) {
### find positions
pos_upper <- which(adv_ratio > upper_constraint)
### turn of constraint when index below Itrigger?
if (isFALSE(cap_below_b)) {
pos_upper <- setdiff(pos_upper,
which(c(tracking[[1]][, ac(ay)]["comp_b", ]) < 1))
}
### limit advice
if (length(pos_upper) > 0) {
advice[pos_upper] <- adv_last[,,,,, pos_upper] * upper_constraint
}
### lower constraint
}
if (lower_constraint != 0) {
### find positions
pos_lower <- which(adv_ratio < lower_constraint)
### turn of constraint when index below Itrigger?
if (isFALSE(cap_below_b)) {
pos_lower <- setdiff(pos_lower,
which(c(tracking[[1]][, ac(ay)]["comp_b", ]) < 1))
}
### limit advice
if (length(pos_lower) > 0) {
advice[pos_lower] <- adv_last[,,,,, pos_lower] * lower_constraint
}
}
}
### otherwise do nothing here and recycle last year's advice
} else {
advice <- tracking[[1]]["isys", ac(ay - 1)]
}
ctrl@iters[which(ctrl@target$year == (ay + 1)),
"value", ] <- advice
return(list(ctrl = ctrl, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### implementation error ####
### ------------------------------------------------------------------------ ###
iem_comps <- function(ctrl, args, tracking,
iem_dev = FALSE, use_dev, ...) {
ay <- args$ay
### only do something if requested
if (isTRUE(use_dev)) {
### get advice
advice <- ctrl@iters[which(ctrl@target$year == (ay + 1)),
"value", ]
### get deviation
dev <- c(iem_dev[, ac(ay)])
### implement deviation
advice <- advice * dev
### insert into ctrl object
ctrl@iters[which(ctrl@target$year == (ay + 1)),
"value", ] <- advice
}
return(list(ctrl = ctrl, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### projection ####
### ------------------------------------------------------------------------ ###
fwd_attr <- function(om, ctrl,
#stk = stock(om),
#sr = sr(om), ### stock recruitment model
#sr.residuals = residuals(sr(om)), ### recruitment residuals
sr.residuals.mult = TRUE, ### are res multiplicative?
maxF = 5, ### maximum allowed Fbar
dupl_trgt = FALSE,
proc_res = NULL, ### process error noise,
migration = NULL, ### FLQuant with migration factor(s)
disc_survival = 0, ### discard survival proportion
dd_M = FALSE, ### density-dependent M
dd_M_relation = NULL, ### density-dependent M
dd_M_fun = calculate_ddM,
...) {
stk <- stock(om)
sr <- sr(om)
sr.residuals <- residuals(sr(om))
### avoid the issue that the catch is higher than the targeted catch
### can happen due to bug in FLash if >1 iteration provided
### sometimes, FLash struggles to get estimates and then uses F estimate from
### previous iteration
### workaround: target same value several times and force FLash to try again
if (isTRUE(dupl_trgt)) {
### duplicate target
ctrl@target <- rbind(ctrl@target, ctrl@target, ctrl@target)
### replace catch in second row with landings
ctrl@target$quantity[1] <- "landings"
ctrl@target$quantity[3] <- "catch"
### extract target values
val_temp <- ctrl@trgtArray[, "val", ]
### extend trgtArray
### extract dim and dimnames
dim_temp <- dim(ctrl@trgtArray)
dimnames_temp <- dimnames(ctrl@trgtArray)
### duplicate years
dim_temp[["year"]] <- dim_temp[["year"]] * 3
dimnames_temp$year <- rep(dimnames_temp$year, 3)
### create new empty array
trgtArray <- array(data = NA, dim = dim_temp, dimnames = dimnames_temp)
### fill with values
### first as target
trgtArray[1, "val", ] <- val_temp
### then again, but as max
trgtArray[2, "max", ] <- val_temp
### min F
trgtArray[3, "max", ] <- val_temp
### insert into ctrl object
ctrl@trgtArray <- trgtArray
}
### calculate density-dependent natural mortality if required
if (isTRUE(dd_M)) {
### overwrite M in the target year before projecting forward
m(stk)[, ac(ctrl@target$year)] <-
dd_M_fun(stk, ctrl@target$year - 1, relation = dd_M_relation)
}
### migration
if (!is.null(migration)) {
### find year to adapt for migration
yr_target <- ctrl@target$year
yr_migr <- yr_target - 1 ### adapt year before target
migr_factor <- FLQuant(NA, dimnames = list(age = dimnames(stk)$age,
year = yr_migr,
iter = dimnames(stk)$iter))
migr_factor[] <- sr@ssb[, ac(yr_target)]
### update stock numbers
stock.n_bckp <- stock.n(stk)[, ac(yr_migr)]
stock.n(stk)[, ac(yr_migr)] <- stock.n(stk)[, ac(yr_migr)] * migr_factor
}
### project forward with FLash::fwd
if (!isTRUE(disc_survival > 0)) {
stk[] <- fwd(object = stk, control = ctrl, sr = sr,
deviances = sr.residuals,
maxF = maxF)
} else {
### account for discard survival
stk_bckp <- stk
stk_fc1 <- stk ### backup stock
### first projection with all discards
stk_fc1[] <- fwd(object = stk_fc1, control = ctrl, sr = sr,
deviances = sr.residuals,
maxF = maxF)