-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflr_mse_WKNSMSE_funs.R
1301 lines (1058 loc) · 46.1 KB
/
flr_mse_WKNSMSE_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
### ------------------------------------------------------------------------ ###
### functions for running FLR WKNSMSE ####
### ------------------------------------------------------------------------ ###
### ------------------------------------------------------------------------ ###
### 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) {
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) {
### apply function to every element of FLIndices
lapply(X = idx, FUN = calc_survey_ind, stk = stk, use_q = use_q,
use_time = use_time)
})
#' @rdname calc_survey
setMethod(f = "calc_survey",
signature = signature(stk = "FLStock", idx = "FLIndices"),
definition = function(stk, idx, use_q = TRUE, use_time = TRUE) {
### apply function to every element of FLIndices
lapply(X = idx, FUN = calc_survey_ind, stk = stk, use_q = use_q,
use_time = use_time)
})
### 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) {
calc_survey_ind(stk = stk, idx = idx, use_q = use_q, use_time = use_time)
})
#' @rdname calc_survey
setMethod(f = "calc_survey",
signature = signature(stk = "FLStock", idx = "FLIndex"),
definition = function(stk, idx, use_q = TRUE, use_time = TRUE) {
calc_survey_ind(stk = stk, idx = idx, use_q = use_q, use_time = use_time)
})
calc_survey_ind <- function(stk, idx,
use_q = TRUE, ### catchability
use_time = TRUE ### timing of survey
) {
### 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
}
### If no age structure, use exploitable biomass index
if (isTRUE(length(ages) == 0) & isTRUE(time == 0.5)) {
### set age to 1, i.e. use first/only age
ages <- 1
### get index slot
index <- index(idx)
### get required quantities
Z_i <- m(stk)[, years,,,, iter] +
harvest(stk)[, years,,,, iter]
F_i <- harvest(stk)[, years,,,, iter]
CW_i <- catch.wt(stk)[, years,,,, iter]
N_i <- stock.n(stk)[, years,,,, iter]
### standardization by oldest age
### Fay / sumFy
W_i <- F_i %=% 0
W_i[] <- quantSums(F_i)
W_i[] <- F_i / W_i
### calculate FSB index
index <- quantSums(CW_i * W_i * N_i * exp(-time * Z_i))
### multiply by the number of age class to get index calculated by SAM
index <- index * dim(stk)[[1]]
### add catchability, if requested
if (isTRUE(use_q)) {
index <- index * index.q(idx)[, ac(years),,,, ac(iter)]
}
### insert values into index
index(idx)[, ac(years),,,, ac(iter)] <- index
} else {
### 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)
### add catchability, if requested
if (isTRUE(use_q)) {
index.n <- index.n * index.q(idx)[ac(ages), ac(years),,,, ac(iter)]
}
### insert values into index
index(idx)[ac(ages), ac(years),,,, ac(iter)] <- index.n
}
return(idx)
}
### ------------------------------------------------------------------------ ###
### observations wrapper ####
### ------------------------------------------------------------------------ ###
oem_WKNSMSE <- function(stk,
deviances,
observations,
genArgs,
tracking,
catch_timing = -1, ### catch timing relative to ay
idx_timing = -1, ### index timing relative to ay
use_catch_residuals = FALSE, ### use residuals for
use_idx_residuals = FALSE, ### observations
use_stk_oem = FALSE, ### biological parameters, wts etc
...) {
### current (assessment) year
ay <- genArgs$ay
### 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)
}
### 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)
}
### calculate index values
observations$idx <- calc_survey(stk = stk, idx = observations$idx)
### observed survey
idx0 <- observations$idx
### add uncertainty to observed indices
if (isTRUE(use_idx_residuals)) {
idx0 <- lapply(seq_along(idx0), function(idx_i) {
idx_tmp <- idx0[[idx_i]]
index(idx_tmp) <- index(idx_tmp) * deviances$idx[[idx_i]]
return(idx_tmp)
})
}
### 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)
### return observations
return(list(stk = stk0, idx = idx0, observations = observations,
tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### stock assessment: SAM wrapper ####
### ------------------------------------------------------------------------ ###
### wrapper for calling SAM
### this makes used of the SAM wrapper in FLfse,
### which in turn calls SAM from the package stockassessment
SAM_wrapper <- function(stk, idx, tracking,
prop_biasN = 1.0,## bias in N-at-age in SAM fit
prop_biasF = 1.0,## bias in F-at-age in SAM fit
genArgs, ### contains ay (assessment year)
forecast = FALSE,
fwd_trgt = "fsq", ### what to target in forecast
fwd_yrs = 1, ### number of years to add
fwd_yrs_average = -3:0, ### years used for averages
fwd_yrs_rec_start = NULL, ### recruitment
fwd_yrs_sel = -3:-1, ### selectivity
fwd_yrs_lf_remove = -2:-1,
fwd_splitLD = TRUE,
parallel = FALSE,
conf = NULL, ### SAM configuration
par_ini = NULL, ### initial parameters
track_ini = FALSE, ### store ini for next year
...){
### get additional arguments
args <- list(...)
### get current (assessment) year
ay <- genArgs$ay
### check if initial parameter values for SAM exist from last year's fit
### and reuse if possible
### this overrides generic initial parameters, if they exist in par_ini
### (they are only used in first simulation year)
if (isTRUE(track_ini) & !is.null(attr(tracking@units, "par_ini"))) {
par_ini <- attr(tracking@units, "par_ini")
}
### fit SAM to provided data
fit <- FLR_SAM(stk = stk, idx = idx, conf = conf, par_ini = par_ini,
conf_full = TRUE, DoParallel = parallel, ...)
### add a bias to stock N-at-age and F-at-age from sam fits
ssb_sam <- array(NA, dim=c(length(fit), 9))
for (i in 1:length(fit)) {
if (!is.null(fit[i]) & length(fit[i][[1]]$sdrep$estY)>0) {
fit[i][[1]]$sdrep$estY[1:8] <- fit[i][[1]]$sdrep$estY[1:8] + log(prop_biasN)
fit[i][[1]]$sdrep$estY[9:15] <- fit[i][[1]]$sdrep$estY[9:15] + log(prop_biasF)
### save ssbs from ay+previous years for retrospective analysis
ssb_sam[i,] <- ssbtable(fit[i][[1]])[(length(ssbtable(fit[i][[1]])[,1])-8):length(ssbtable(fit[i][[1]])[,1]),1]
}
}
colnames(ssb_sam) <- fit[1][[1]]$data$years[(length(fit[1][[1]]$data$years)-8):length(fit[1][[1]]$data$years)]
### store parameter values and provide them as initial values next year
### store in tracking object, this is the only object which does not get
### overriden next year
### weird workaround: save as attribute of "unit" slot of tracking,
### otherwise the attributes will be lost later...
if (isTRUE(track_ini)) {
attr(tracking@units, "par_ini") <- sam_getpar(fit)
}
### convert into FLStock
stk0 <- SAM2FLStock(object = fit, stk = stk)
### perform forecast to get SSB ay+1
if (isTRUE(forecast)) {
### check how to do forecast
### currently, can only do F status quo
if (fwd_trgt != "fsq") stop("only fsq supported in forecast")
### years for average values
ave.years <- range(stk0)[["maxyear"]] + fwd_yrs_average
### years for sampling of recruitment years
rec.years <- seq(from = fwd_yrs_rec_start, to = range(stk0)[["maxyear"]] - 1)
### years where selectivity is not used for mean in forecast
overwriteSelYears <- range(stk0)[["maxyear"]] + fwd_yrs_sel
lst_yr <- range(stk0)[["maxyear"]]
### extend stk0
stk0 <- window(stk0, end = range(stk0)[["maxyear"]] + fwd_yrs)
### modify fwd_yrs in case last data year does not include catch
if (all(is.na(catch(stk0)[, ac(lst_yr)]))) fwd_yrs <- fwd_yrs + 1
### forecast years
yrs <- seq(to = dims(stk0)$maxyear, length.out = fwd_yrs)
### coerce fit into list if only 1 iteration
if (is(fit, "sam")) {
fit <- list(fit)
class(fit) <- "sam_list"
}
### do forecast for all iterations
fc <- foreach(fit_i = fit, .errorhandling = "pass") %do% {
### overwrite landing fraction with last year, if requested
if (!is.null(fwd_yrs_lf_remove)) {
### index for years to remove/overwrite
idx_remove <- nrow(fit_i$data$landFrac) + args$fwd_yrs_lf_remove
### overwrite
fit_i$data$landFrac[idx_remove, ] <- fit_i$data$landFrac[rep(nrow(fit_i$data$landFrac), length(idx_remove)), ]
}
### run forecast
fc_i <- stockassessment::forecast(fit = fit_i, fscale = rep(1, fwd_yrs),
ave.years = ave.years,
rec.years = rec.years,
overwriteSelYears = overwriteSelYears,
splitLD = fwd_splitLD)
### get numbers at age for all forecast years
numbers <- lapply(seq(fwd_yrs), function(x) {
### index for numbers at age
idx <- seq(length(fit_i$conf$keyLogFsta[1, ]))
### get simulated numbers
n <- exp(fc_i[[x]]$sim[, idx])
### median
apply(n, 2, median)
})
numbers <- do.call(cbind, numbers)
return(list(stock.n = numbers))
}
### if forecast failed for a iteration, the list element will for this
### iteration will be an error message
### get numbers
fc_stock.n <- lapply(fc, "[[", "stock.n")
### insert stock numbers
for (iter_i in seq(dim(stk0)[6])) {
### do not insert numbers if forecast failed
if (!is.numeric(fc_stock.n[[iter_i]])) next()
stock.n(stk0)[, ac(yrs),,,, iter_i] <- fc_stock.n[[iter_i]]
}
### extend stock characteristics required for calculation of SSB, weights, etc.
### find years to fill (do not fill years, if there is already data inside)
yrs_fill <- setdiff(yrs, lst_yr)
stock.wt(stk0)[, ac(yrs_fill)] <- yearMeans(stock.wt(stk0)[, ac(ave.years)])
m(stk0)[, ac(yrs_fill)] <- yearMeans(m(stk0)[, ac(ave.years)])
mat(stk0)[, ac(yrs_fill)] <- yearMeans(mat(stk0)[, ac(ave.years)])
harvest.spwn(stk0)[, ac(yrs_fill)] <- yearMeans(harvest.spwn(stk0)[, ac(ave.years)])
m.spwn(stk0)[, ac(yrs_fill)] <- yearMeans(m.spwn(stk0)[, ac(ave.years)])
harvest(stk0)[, ac(yrs_fill)] <- yearMeans(harvest(stk0)[, ac(ave.years)])
### PLEASE NOTE:
### SSB value slightly different from SSB value generated from SAM:
### SAM calculates SSB per simulation and gives median
### here: calculate median of numbers at age and calculate SSB from median numbers
### ISSUES: forecast fails if SAM did not converge, creates error and stops
}
### save convergence for all iterations
tracking["conv.est", ac(ay)] <- sapply(fit, function(x) x$opt$convergence)
### add perceived F and SSB
### done in mp()
### save model fit (list) as attribute in stk0
attr(stk0, "fit") <- fit
### return assessed stock, tracking & model fit
### (model fit required later for TAC calculation)
return(list(stk = stk0, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### phcr: parameterize HCR ####
### ------------------------------------------------------------------------ ###
phcr_WKNSMSE <- function(Btrigger = NULL, Ftrgt = NULL, Bpa = NULL, Fpa = NULL,
Blim = NULL, tracking, ...) {
### coerce existing values into FLPar
hcrpars <- list("Btrigger" = Btrigger, "Ftrgt" = Ftrgt, "Bpa" = Bpa,
"Fpa" = Fpa, "Blim" = Blim)
hcrpars <- hcrpars[!sapply(hcrpars, is.null)]
hcrpars <- do.call(FLPar, hcrpars)
### if more iterations provided than neccessary, subset
if (dims(hcrpars)$iter > dims(tracking)$iter) {
hcrpars <- hcrpars[, dimnames(tracking)$iter]
}
### return as list
### keep tracking unchanged
return(list(hcrpars = hcrpars, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### hcr: ICES advice rule and modificatinons ####
### ------------------------------------------------------------------------ ###
### target Ftrgt
### A: if SSB < Btrigger, target reduced: Ftrgt * SSB/Btrigger
### B & C: different behaviour if SSB < Blim
hcr_WKNSME <- function(stk, genArgs, hcrpars, tracking,
option = "A", ### WKNSMSE options
...) {
### target F
### reduce F if SSB below Btrigger
### get current (assessment) year
ay <- genArgs$ay
### number of iterations
it <- dim(stk)[6]
### get reference values and extend iteration dimension
Ftrgt <- propagate(FLPar(hcrpars["Ftrgt"]), it)
Btrigger <- propagate(FLPar(hcrpars["Btrigger"]), it)
### get Blim
### if Blim not specified, set to zero
if ("Blim" %in% dimnames(hcrpars)$params) {
Blim <- propagate(FLPar(hcrpars["Blim"]), it)
} else {
Blim <- propagate(FLPar(0), it)
}
### SSB status (last year only) relative to Btrigger
status_Btrigger <- tail(ssb(stk), 1) / Btrigger
### SSB status (last year only) relative to Blim
status_Blim <- tail(ssb(stk), 1) / Blim
### positions (iterations) where SSB is below Btrigger
pos_Btrigger <- which(status_Btrigger < 1)
### below Blim
pos_Blim <- which(status_Blim < 1)
### default ICES HCR (option A):
### if SSB<Btrigger => F = Ftrgt * SSB/Btrigger
if (option == "A") {
mult <- ifelse(status_Btrigger < 1, status_Btrigger, 1)
} else if (option == "B") {
### option B:
### if SSB<Btrigger => F = Ftrgt * SSB/Btrigger
### if SSB<Blim => F = Ftrgt * 0.25
### SSB < Btrigger
mult <- ifelse(status_Btrigger < 1, status_Btrigger, 1)
### set to 0.25 if SSB < Blim
if (length(pos_Blim) > 0) {
mult[,,,,, pos_Blim] <- 0.25
}
} else if (option == "C") {
### option C:
### if SSB<Btrigger => F = Ftrgt * SSB/Btrigger
### if SSB<Blim => F = max(Ftrgt * 0.25, Ftrgt * SSB/Btrigger)
### SSB < Btrigger
mult <- ifelse(status_Btrigger < 1, status_Btrigger, 1)
### if SSB < Blim, use maximum of SSB/Btrigger or 0.25
### i.e. limit ratio to 0.25
if (length(pos_Blim) > 0) {
mult[,,,,, pos_Blim] <- c(ifelse(mult[,,,,, pos_Blim] < 0.25, 0.25,
mult[,,,,, pos_Blim]))
}
} else {
### unknown options
mult <- 1
}
### new target
Ftrgt <- Ftrgt * mult
### create ctrl object
ctrl <- getCtrl(values = Ftrgt, quantity = "f", years = ay + 1, it = it)
### save in tracking
### done later
return(list(ctrl = ctrl, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### management implementation: short term forecast with SAM ####
### ------------------------------------------------------------------------ ###
### short term forecast with SAM
### including TAC constraint
is_WKNSMSE <- function(stk, tracking, ctrl,
genArgs, ### contains ay (assessment year)
TAC_constraint = c(FALSE, TRUE),
upper = Inf, lower = -Inf, Btrigger_cond = FALSE,
### short term forecast
fwd_trgt = c("fsq", "hcr"), ### target in forecast
fwd_yrs = 2, ### number of years to add
fwd_yrs_average = -3:0, ### years used for averages
fwd_yrs_rec_start = NULL, ### recruitment
fwd_yrs_sel = -3:-1, ### selectivity
fwd_yrs_lf_remove = -2:-1,
fwd_splitLD = TRUE,
### banking and borrowing
BB = FALSE, ### banking and borrowing
### check stock status before applying BB
BB_check_hcr = FALSE, ### check status before forecast
BB_check_fc = FALSE, ### check status after forecast
BB_rho, ### definition of BB
### reference points
hcrpars = list(),
...) {
### get current (assessment) year
ay <- genArgs$ay
### number of iterations
it <- dim(stk)[6]
### retrieve SAM model fit (list)
fit <- attr(stk, "fit")
### check class of model fit(s)
if (!class(fit) %in% c("sam", "sam_list"))
stop("attr(stk0, \"fit\") has to be class sam or sam_list")
### if single fit, turn into list
if (is(fit, "sam")) fit <- list(fit)
### if conditional banking & borrowing applied, extend forecast for one more
### year to check SSB in year after advice year
### for this additional forecast assume Fsq as target
### i.e. target F from HCR twice, in analogy to intermediate year assumption
if (isTRUE(BB) & isTRUE(BB_check_fc)) {
### duplicate last target value
fwd_trgt <- c(fwd_trgt, tail(fwd_trgt, 1))
}
### go through all model fits
fc <- foreach(fit_i = fit, iter_i = seq_along(fit), .errorhandling = "pass") %do% {
### overwrite landing fraction with last year, if requested
if (!is.null(fwd_yrs_lf_remove)) {
### index for years to remove/overwrite
idx_remove <- nrow(fit_i$data$landFrac) + fwd_yrs_lf_remove
### overwrite
fit_i$data$landFrac[idx_remove, ] <- fit_i$data$landFrac[rep(nrow(fit_i$data$landFrac), length(idx_remove)), ]
}
### check how to do forecast
### currently, can only do F status quo and F target from ctrl object
fscale <- ifelse(fwd_trgt == "fsq", 1, NA) ### scaled Fsq
### target F values
fval <- ifelse(fwd_trgt == "hcr", ctrl@trgtArray[, "val", iter_i], NA)
### years for average values
ave.years <- max(fit_i$data$years) + fwd_yrs_average
### years for sampling of recruitment years
if (is.null(fwd_yrs_rec_start)) {
rec.years <- fit_i$data$years ### use all years, if not defined
} else {
rec.years <- seq(from = fwd_yrs_rec_start, max(fit_i$data$years))
}
### years where selectivity is not used for mean in forecast
overwriteSelYears <- max(fit_i$data$years) + fwd_yrs_sel
### forecast
fc_i <- stockassessment::forecast(fit = fit_i, fscale = fscale, fval = fval,
ave.years = ave.years,
rec.years = rec.years,
overwriteSelYears = overwriteSelYears,
splitLD = fwd_splitLD)
### return forecast table
return(attr(fc_i, "tab"))
}
### if forecast fails, error message returned
### replace error message with NA
### extract catch target
catch_target <- sapply(fc, function(x) {
if (is(x, "error")) {
return(NA)
} else {
return(x[ac(ay + 1), "catch:median"])
}
})
### get reference points
hcrpars <- hcrpars[!sapply(hcrpars, is.null)]
hcrpars <- do.call(FLPar, hcrpars)
### if more iterations provided than neccessary, subset
if (dims(hcrpars)$iter > dims(tracking)$iter) {
hcrpars <- hcrpars[, dimnames(tracking)$iter]
} else if (isTRUE(dim(hcrpars)[2] < it)) {
hcrpars <- propagate(hcrpars, it)
}
### ---------------------------------------------------------------------- ###
### TAC constraint ####
if (isTRUE(TAC_constraint)) {
### target year
yr_target <- ctrl@target$year
### get previous target from tracking
catch_prev <- tracking["metric.is", ac(yr_target - 1), drop = TRUE]
### change in advice, % of last advice
change <- (catch_target / catch_prev) * 100
### limit changes
changes_new <- change
### upper limit
changes_new <- ifelse(changes_new > upper, upper, changes_new)
### lower limit
changes_new <- ifelse(changes_new < lower, lower, changes_new)
### find positions which exceed limits
pos <- which(changes_new != change)
### conditional constraint based on SSB>=Btrigger?
if (isTRUE(Btrigger_cond)) {
### get SSB in TAC year from forecast
SSB_TACyr <- sapply(fc, function(x) {
if (is(x, "error")) {
return(NA)
} else {
return(x[ac(ay + 1), "ssb:median"])
}
})
### iterations where SSB is at or above Btrigger at start of TAC year
pos_Btrigger <- which(SSB_TACyr >= c(hcrpars["Btrigger"]))
### only apply TAC constraint if both
### - TAC change exceeds limit
### - stock at or above Btrigger
pos <- intersect(pos, pos_Btrigger)
}
### modify advice
catch_target[pos] <- catch_prev[pos] * changes_new[pos]/100
}
### ---------------------------------------------------------------------- ###
### banking and borrowing ####
if (isTRUE(BB)) {
### get current rho
BB_rho_i <- tail(rep(BB_rho, length.out = (ay - genArgs$y0)), 1)
### get catch borrowed last year
BB_return <- tracking["BB_borrow", ac(ay - 1)]
### assume nothing borrowed if NA
BB_return <- ifelse(!is.na(BB_return), BB_return, 0)
### get catch banked last year
BB_bank_use <- tracking["BB_bank", ac(ay - 1)]
BB_bank_use <- ifelse(!is.na(BB_bank_use), BB_bank_use, 0)
### bank for next year
if (BB_rho_i < 0) {
BB_bank <- catch_target * abs(BB_rho_i)
} else {
BB_bank <- rep(0, it)
}
### borrow from next year
if (BB_rho_i > 0) {
BB_borrow <- catch_target * abs(BB_rho_i)
} else {
BB_borrow <- rep(0, it)
}
### conditional banking and borrowing?
### first condition: for HCR option A (stability option D2)
### apply BB only if HCR option A1 (not A2) is applied
### i.e. stop BB if SSB is below Btrigger in TAC year
### find iterations where SSB is below Btriggerin TAC year
if (isTRUE(BB_check_hcr)) {
pos_hcr <- which(c(tail(ssb(stk), 1)) < c(hcrpars["Btrigger"]))
} else {
pos_hcr <- integer(0)
}
### second condition: for HCR options B & C (stability option E2)
### stop BB if either
### - if SSB is below Bpa AND F above Fpa in TAC year
### - if SSB is below Bpa in TAC year and year after
### if TAC restricted by TAC constraint, additional forecast required
### to estimate stock status when only TAC is fished
pos_fc <- integer(0)
if (any(c(BB_bank, BB_borrow) > 0) & isTRUE(BB_check_fc)) {
### if TAC constraint activated, do a forecast
### check if TAC constraint used
if (isTRUE(TAC_constraint)) {
### check where TAC constraint is implemented
pos_constr <- which(changes_new != change)
if (isTRUE(length(pos_constr) > 0)) {
### go through model fits of requested iterations
fc_new <- foreach(fit_i = fit[pos_constr],
iter_i = seq_along(fit)[pos_constr],
.errorhandling = "pass") %do% {
### overwrite landing fraction with last year, if requested
if (!is.null(fwd_yrs_lf_remove)) {
### index for years to remove/overwrite
idx_remove <- nrow(fit_i$data$landFrac) + fwd_yrs_lf_remove
### overwrite
fit_i$data$landFrac[idx_remove, ] <- fit_i$data$landFrac[rep(nrow(fit_i$data$landFrac), length(idx_remove)), ]
}
### create vector with targets
### target Fsq in intermediate year, and year after TAC year
fscale <- c(1, 1, NA, 1)
### target TAC in TAC year
cval <- c(NA,NA, catch_target[iter_i], NA)
### years for average values
ave.years <- max(fit_i$data$years) + fwd_yrs_average
### years for sampling of recruitment years
if (is.null(fwd_yrs_rec_start)) {
rec.years <- fit_i$data$years ### use all years, if not defined
} else {
rec.years <- seq(from = fwd_yrs_rec_start, max(fit_i$data$years))
}
### years where selectivity is not used for mean in forecast
overwriteSelYears <- max(fit_i$data$years) + fwd_yrs_sel
### forecast
fc_i <- stockassessment::forecast(fit = fit_i,
fscale = fscale,
catchval = cval,
ave.years = ave.years,
rec.years = rec.years,
overwriteSelYears = overwriteSelYears,
splitLD = fwd_splitLD)
### return forecast table
return(attr(fc_i, "tab"))
}
### overwrite updated forecasts
fc[pos_constr] <- fc_new
### get SSB in TAC year
SSB_TACyr <- sapply(fc, function(x) {
if (is(x, "error")) {
return(NA)
} else {
return(x[ac(ay + 1), "ssb:median"])
}
})
### get SSB in year after TAC year
SSB_TACyr1 <- sapply(fc, function(x) {
if (is(x, "error")) {
return(NA)
} else {
return(x[ac(ay + 2), "ssb:median"])
}
})
### get F in TAC year
F_TACyr <- sapply(fc, function(x) {
if (is(x, "error")) {
return(NA)
} else {
return(x[ac(ay + 1), "fbar:median"])
}
})
### check if SSB in TAC year below Bpa AND F above Fpa
pos_fc1 <- which(SSB_TACyr < c(hcrpars["Bpa"]) &
F_TACyr > c(hcrpars["Fpa"]))
### check if SSB below Bpa in TAC year and year after
pos_fc2 <- which(SSB_TACyr < c(hcrpars["Bpa"]) &
SSB_TACyr1 < c(hcrpars["Bpa"]))
### combine both conditions
pos_fc <- union(pos_fc1, pos_fc2)
}
}
}
### get position where BB is stopped
pos_stop <- union(pos_hcr, pos_fc)
### if status evaluated as not precautionary
### stop banking and borrowing
### (but still pay back/use from last year)
BB_bank[pos_stop] <- 0
BB_borrow[pos_stop] <- 0
### correct target catch later in iem module
# catch_target <- catch_target - c(BB_return) +
# c(BB_bank_use) - c(BB_bank) + c(BB_borrow)
} else {
### if B&B not applied, store 0
BB_return <- BB_bank_use <- BB_bank <- BB_borrow <- 0
}
### save B&B transfers in tracking
tracking["BB_return", ac(ay)] <- BB_return
tracking["BB_bank_use", ac(ay)] <- BB_bank_use
tracking["BB_bank", ac(ay)] <- BB_bank
tracking["BB_borrow", ac(ay)] <- BB_borrow
### create ctrl object
ctrl <- getCtrl(values = catch_target, quantity = "catch",
years = ctrl@target$year, it = it)
### return catch target and tracking
return(list(ctrl = ctrl, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### implementation error: banking and borrowing ####
### ------------------------------------------------------------------------ ###
### so far only banking and borrowing (B&B) implemented
iem_WKNSMSE <- function(tracking, ctrl,
genArgs, ### contains ay (assessment year)
BB = FALSE, ### apply banking and borrowing
...) {
if (isTRUE(BB)) {
### get current (assessment) year
ay <- genArgs$ay
### retrieve banking and borrowing values
BB_return <- tracking["BB_return", ac(ay)]
BB_bank_use <- tracking["BB_bank_use", ac(ay)]
BB_bank <- tracking["BB_bank", ac(ay)]
BB_borrow <- tracking["BB_borrow", ac(ay)]
### replace NAs with 0
BB_return <- ifelse(!is.na(BB_return), BB_return, 0)
BB_bank_use <- ifelse(!is.na(BB_bank_use), BB_bank_use, 0)
BB_bank <- ifelse(!is.na(BB_bank), BB_bank, 0)
BB_borrow <- ifelse(!is.na(BB_borrow), BB_borrow, 0)
### get catch target(s)
catch_target <- ctrl@trgtArray[, "val", ]
### implement B&B
catch_target <- catch_target - c(BB_return) + c(BB_bank_use) -
c(BB_bank) + c(BB_borrow)
### save in ctrl object
ctrl@trgtArray[, "val", ] <- catch_target
}
### return catch target and tracking
return(list(ctrl = ctrl, tracking = tracking))
}
### ------------------------------------------------------------------------ ###
### forward projection ####
### ------------------------------------------------------------------------ ###
### including process error on stock.n
### implemented by simply multiplying numbers at age from fwd with noise factor
fwd_WKNSMSE <- function(stk, ctrl,
sr, ### stock recruitment model
sr.residuals, ### recruitment residuals
sr.residuals.mult = TRUE, ### are res multiplicative?
maxF = 2, ### maximum allowed Fbar
proc_res = NULL, ### process error noise
...) {
### project forward with FLash::fwd
stk[] <- fwd(object = stk, control = ctrl, sr = sr,
sr.residuals = sr.residuals,
sr.residuals.mult = sr.residuals.mult,
maxF = maxF)
### add process error noise, if supplied
if (!is.null(proc_res)) {
### projected years
yrs_new <- seq(from = ctrl@target[, "year"], to = range(stk)[["maxyear"]])
### workaround to get residuals
### they are saved in the "fitted" slot of sr...
if (!isTRUE(proc_res == "fitted")) {
stop("survival process error inacessible")
} else {
### implement process error
stock.n(stk)[, ac(yrs_new)] <- stock.n(stk)[, ac(yrs_new)] *
fitted(sr)[, ac(yrs_new)]
### update stock biomass
stock(stk) <- computeStock(stk)
}
}
### return stock
return(list(object = stk))
}
### ------------------------------------------------------------------------ ###
### extract uncertainty from SAM object ####
### ------------------------------------------------------------------------ ###
### function for creating iterations based on estimation of uncertainty in SAM
### note: SAM works on a log scale and all reported parameters are also on a
### scale, even standard deviations.
### Therefore, the values returned from this function are exponentiated
SAM_uncertainty <- function(fit, n = 1000, print_screen = FALSE) {
if (!is(fit, "sam")) stop("fit has to be class \"sam\"")
### index for fishing mortality ages
idxF <- fit$conf$keyLogFsta[1, ] + 1# +dim(stk)[1]
idxF <- idxF[idxF != 0] ### remove 0s
### index for F variances (usually some ages are bound)
#idxNVar <- fit$conf$keyVarLogN
### get ages used for calculating fbar
#bAges <- fit$conf$fbarRange
#bAges <- do.call(':', as.list(bAges))
### index for stock numbers ages
#idxN <- 1:ncol(natural.mortality)
#idxN <- seq(min(fit$data$minAgePerFleet), max(fit$data$maxAgePerFleet))
### index for observation variances
idxObs <- fit$conf$keyVarObs # starts at 0