-
Notifications
You must be signed in to change notification settings - Fork 1
/
funs_OM.R
1230 lines (1151 loc) · 52.6 KB
/
funs_OM.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 creating operating models (OMs) ####
### ------------------------------------------------------------------------ ###
### ------------------------------------------------------------------------ ###
### convert Beverton-Holt stock-recruitment model ####
### ------------------------------------------------------------------------ ###
### formulation with steepness, virgin biomass -> a & b
bevholtSV_to_bevholt <- function(sr) {
sr_new <- sr ### duplicate model
model(sr_new) <- "bevholt" ### change model type to bevholt
### convert parameters into normal bevholt parameters
sr_pars <- abPars(model = "bevholt", spr0 = (params(sr)["spr0"]),
s = (params(sr)["s"]), v = (params(sr)["v"]))
dimnames(sr_pars$a)$params <- "a"
dimnames(sr_pars$b)$params <- "b"
### combine all parameters and insert them
sr_pars <- rbind(rbind(sr_pars$a, sr_pars$b), params(sr))
params(sr_new) <- sr_pars
### also insert some more slots
ssb(sr_new) <- ssb(sr)
rec(sr_new) <- rec(sr)
logLik(sr_new) <- logLik(sr)
details(sr_new) <- details(sr)
residuals(sr_new) <- residuals(sr)
fitted(sr_new) <- fitted(sr)
return(sr_new)
}
### ------------------------------------------------------------------------ ###
### create operating model ####
### ------------------------------------------------------------------------ ###
### this function creates the elements required for an OM to run an MSE,
### e.g. OM stock, stock-recruitment model, survey indices, etc.
create_OM <- function(stk_data, idx_data,
n = 1000, n_years = 100, yr_data = 2020, int_yr = FALSE,
SAM_conf, SAM_conf_full = FALSE, SAM_NA_rm = TRUE,
SAM_idx_weight = FALSE,
SAM_newtonsteps = 0, SAM_rel.tol = 0.001,
n_sample_yrs = 5, sel_legacy = FALSE,
sr_model = "bevholtSV", sr_start = NULL,
sr_fixed = list(),
sr_yrs_rm = NULL, ### remove recruitment years?
sr_parallel = 10, sr_ar_check = TRUE,
process_error = TRUE, catch_oem_error = TRUE,
idx_weights = c("none"), ### "none"/"catch.wt"/"stock.wt"
idxB = 1, ### FALSE, index name or numeric index
idxL = TRUE, ALKs, ALK_yrs = NULL,
length_samples = 2000,
PA_status = TRUE, ### status evaluation success rate
refpts = list(),
stock_id = "ple.27.7e",
OM = "baseline",
save = TRUE,
return = FALSE,
M_alternative = NULL, ### 1 value (const.) or M@age
M_alternative_mult = FALSE,
M_dd = FALSE, ### density dependent M
M_dd_relation = NULL,
M_dd_yr = NULL, ### last key run
M_dd_migration = NULL, ### "correct" ages 3+ for migration
disc_survival = 0, ### discard survival proportion
disc_survival_hidden = TRUE
) {
### ---------------------------------------------------------------------- ###
### preparation for alternative OMs ####
stk_data_input <- stk_data
### ---------------------------------------------------------------------- ###
### alternative M scenario? ####
if (!is.null(M_alternative)) {
message("using alternative M scenario")
if (isTRUE(M_alternative_mult)) {
m(stk_data) <- m(stk_data) * M_alternative
} else {
m(stk_data)[] <- M_alternative
}
}
### ---------------------------------------------------------------------- ###
### alternative discard survival scenario? ####
if (isTRUE(disc_survival > 0)) {
message("using alternative discard survival scenario")
discards.n(stk_data)[is.na(discards.n(stk_data))] <- 0
discards.wt(stk_data)[is.na(discards.wt(stk_data))] <- 0
discards.n(stk_data)[] <- discards.n(stk_data) * (1 - disc_survival)
discards(stk_data) <- computeDiscards(stk_data)
catch(stk_data) <- computeCatch(stk_data, slot = "all")
}
### ---------------------------------------------------------------------- ###
### fit SAM ####
message("fitting SAM")
fit <- FLR_SAM(stk_data, idx_data, conf = SAM_conf, conf_full = SAM_conf_full,
idx_weight = SAM_idx_weight, NA_rm = SAM_NA_rm)
### fit SAM with relaxed convergence
fit_mse <- FLR_SAM(stk_data, idx_data, conf = SAM_conf,
conf_full = SAM_conf_full, idx_weight = SAM_idx_weight,
NA_rm = SAM_NA_rm,
newtonsteps = SAM_newtonsteps, rel.tol = SAM_rel.tol)
### get initial parameters
pars_ini <- getpars(fit_mse)
### ---------------------------------------------------------------------- ###
### create FLStock ####
message("create OM FLStock")
stk <- SAM2FLStock(object = fit, stk = stk_data)
stk_orig <- stk
### ---------------------------------------------------------------------- ###
### projection years ####
yrs_hist <- as.numeric(dimnames(stk)$year)
yrs_proj <- seq(from = dims(stk)$maxyear + 1, length.out = n_years)
n_years_new <- n_years
if (isTRUE(int_yr)) {
yrs_hist <- yrs_hist[-length(yrs_hist)]
yrs_proj <- seq(from = dims(stk)$maxyear + 0, length.out = n_years)
n_years_new <- n_years - 1
}
yrs_mse <- sort(unique(c(yrs_hist, yrs_proj)))
### ---------------------------------------------------------------------- ###
### add uncertainty with variance-covariance matrix ####
message("add uncertainty to OM with variance-covariance matrix")
### add iteration dimension
stk <- FLCore::propagate(stk, n)
### add uncertainty estimated by SAM as iterations
set.seed(1)
uncertainty <- SAM_uncertainty(fit = fit, n = n)
### add noise to stock
stock.n(stk)[] <- uncertainty$stock.n
stock(stk)[] <- computeStock(stk)
### add noise to F
harvest(stk)[] <- uncertainty$harvest
### add noise to catch numbers
catch.n(stk)[, ac(yrs_hist)] <- uncertainty$catch.n[, ac(yrs_hist)]
catch(stk) <- computeCatch(stk)
### update landings/discards
landings.n(stk) <- catch.n(stk) *
landings.n(stk)/(landings.n(stk) + discards.n(stk))
landings(stk) <- computeLandings(stk)
discards.n(stk) <- catch.n(stk) *
discards.n(stk)/(landings.n(stk) + discards.n(stk))
discards(stk) <- computeDiscards(stk)
### ---------------------------------------------------------------------- ###
### discard rate ####
### if missing, add discard rate to avoid later issues when projecting
if (isTRUE(int_yr)) {
yr_max <- range(stk)[["maxyear"]]
if (all(is.na(catch(stk)[, ac(yr_max)]))) {
landings.n(stk)[, ac(yr_max)] <-
(landings.n(stk)/catch.n(stk))[, ac(yr_data)]
discards.n(stk)[, ac(yr_max)] <-
(discards.n(stk)/catch.n(stk))[, ac(yr_data)]
}
}
### ---------------------------------------------------------------------- ###
### alternative discard survival scenario ####
### by default, the surviving discards are included in discards.n slot
if (isTRUE(disc_survival > 0) & isTRUE(disc_survival_hidden)) {
### use catch.n uncertainty from SAM for landings and discards
### get catch.n uncertainty (SAM estimate/input data)
c_noise <- catch.n(stk)/catch.n(stk_data)
### discards ratio in data
dratio <- discards.n(stk_data_input)/catch.n(stk_data_input)
dratio[is.na(dratio)] <- 0
### landings ratio in data
lratio <- landings.n(stk_data_input)/catch.n(stk_data_input)
### total (after accounting for discard survival)
ctotal <- dratio*(1 - disc_survival) + lratio
### update landings
landings.n(stk)[] <- catch.n(stk) * lratio/ctotal ### catch.n includes noise
### discards relative to landings -> used to reproduce original discards
dlratio <- discards.n(stk_data_input)/landings.n(stk_data_input)
### update discards (including surviving discards)
discards.n(stk)[] <- landings.n(stk) * dlratio
### update total catch (including surviving discards)
catch.n(stk)[] <- catch.n(stk_data_input) * c_noise
### get original catch weights (these were changed for SAM)
catch.wt(stk)[] <- catch.wt(stk_data_input)
### update totals
catch(stk) <- computeCatch(stk)
landings(stk) <- computeLandings(stk)
discards(stk) <- computeDiscards(stk)
}
### ---------------------------------------------------------------------- ###
### extend stock for MSE simulation ####
message("extend OM stock for projection")
stk_stf <- stf(stk, n_years_new)
### ---------------------------------------------------------------------- ###
### biological data for OM ####
message("create biological and fishery data for projection")
### Resample weights, maturity and natural mortality from the last X years
### set up an array with one resampled year for each projection year
### (including intermediate year) and replicate
### use the same resampled year for all biological parameters
set.seed(2)
### use last X data years to sample biological parameters
sample_yrs <- seq(to = yr_data, length.out = n_sample_yrs)
### get year position of sample years
sample_yrs_pos <- which(dimnames(stk_stf)$year %in% sample_yrs)
### create samples for biological data (weights, etc.)
### the historical biological parameters are identical for all iterations
### and consequently do not need to be treated individually
### (but keep age structure)
### create vector with resampled years
bio_samples <- sample(x = sample_yrs_pos,
size = (n_years) * n, replace = TRUE)
### years to be populated
bio_yrs <- which(dimnames(stk_stf)$year %in%
(yr_data + 1):dims(stk_stf)$maxyear)
### insert values
catch.wt(stk_stf)[, bio_yrs] <- c(catch.wt(stk)[, bio_samples,,,, 1])
stock.wt(stk_stf)[, bio_yrs] <- c(stock.wt(stk)[, bio_samples,,,, 1])
landings.wt(stk_stf)[, bio_yrs] <- c(landings.wt(stk)[, bio_samples,,,, 1])
discards.wt(stk_stf)[, bio_yrs] <- c(discards.wt(stk)[, bio_samples,,,, 1])
m(stk_stf)[, bio_yrs] <- c(m(stk)[, bio_samples,,,, 1])
mat(stk_stf)[, bio_yrs] <- c(mat(stk)[, bio_samples,,,, 1])
### do the same for selectivity
if (isTRUE(sel_legacy)) {
### legacy approach for selectivity
sel_samples <- sample(x = sample_yrs_pos,
size = (n_years) * n, replace = TRUE)
harvest(stk_stf)[, bio_yrs] <- c(harvest(stk)[, sel_samples,,,, 1])
} else {
### better: use replicate specific selectivity and sample these
sel_samples <- sample(x = sample_yrs_pos,
size = n_years * n, replace = TRUE)
### selectivity differs by replicate -> keep replicate specific values
sel_samples_iter <- split(sel_samples,
f = rep(seq(n), each = n_years))
sel_vals <- as.numeric(sapply(seq(n), function(x) {
c(harvest(stk)[, sel_samples_iter[[x]],,,, x])
}))
### insert
harvest(stk_stf)[, bio_yrs] <- sel_vals
}
### density dependent M
if (isTRUE(M_dd) & isTRUE(int_yr)) {
### update M in intermediate year
m(stk_stf)[, ac(yr_data + 1)] <-
calculate_ddM(stk_stf, yr_data + 1, relation = M_dd_relation,
migration = M_dd_migration)
}
### ---------------------------------------------------------------------- ###
### stock recruitment ####
message("creating stock-recruitment model")
### fit stock-recruitment model and get residuals from smoothed residuals
### create FLSR object
sr <- as.FLSR(stk_stf, model = sr_model)
if (!is.null(sr_start)) sr <- window(sr, start = sr_start)
if (!is.null(sr_yrs_rm)) rec(sr)[, ac("sr_yrs_rm")] <- NA
### fit model individually to each iteration and suppress output to screen
if (isFALSE(sr_parallel) | isTRUE(sr_parallel == 0)) {
sr <- fmle(sr, method = 'L-BFGS-B', fixed = sr_fixed,
control = list(trace = 0))
} else {
### run in parallel
message("fitting stock-recruitment model in parallel")
cl_tmp <- makeCluster(as.numeric(sr_parallel))
registerDoParallel(cl_tmp)
sr <- fmle_parallel(sr, cl_tmp, method = 'L-BFGS-B', fixed = sr_fixed)
stopCluster(cl_tmp)
}
### run again for failed iterations - if needed
pos_error <- which(is.na(params(sr)[1]))
if (isTRUE(length(pos_error) > 0)) {
message("repeating failed iterations")
sr_corrected <- FLCore::iter(sr, pos_error)
sr_corrected <- fmle(sr_corrected, method = 'L-BFGS-B', fixed = sr_fixed,
control = list(trace = 0))
sr[,,,,, pos_error] <- sr_corrected[]
params(sr)[, pos_error] <- params(sr_corrected)
}
if (identical(sr_model, "bevholtSV")) {
sr <- bevholtSV_to_bevholt(sr)
}
### check autocorrelation of residuals for SAM median perception
sr_med <- as.FLSR(stk_orig, model = sr_model)
if (!is.null(sr_start)) sr_med <- window(sr_med, start = sr_start)
if (!is.null(sr_yrs_rm)) rec(sr_med)[, ac("sr_yrs_rm")] <- NA
sr_med <- fmle(sr_med, method = 'L-BFGS-B', fixed = sr_fixed,
control = list(trace = 0))
sr_acf <- acf(residuals(sr_med), plot = FALSE, na.action = na.exclude)
sr_rho <- sr_acf$acf[2]
### only include if lag-1 auto-correlation is above threshold
ci <- qnorm((1 + 0.95)/2)/sqrt(sr_acf$n.used)
if (isTRUE(sr_rho >= ci)) {
message(paste0("residual lag-1 autocorrelation ",
round(sr_rho, 2), " above threshold of ", round(ci, 2)))
} else {
message(paste0("residual lag-1 autocorrelation ",
round(sr_rho, 2), " below threshold of ", round(ci, 2)))
}
if (isFALSE(sr_ar_check) | sr_rho < ci) {
message("- NOT including auto-correlation")
} else {
message("- including auto-correlation")
}
### generate residuals for MSE
### years with missing residuals
yrs_res <- colnames(rec(sr))[which(is.na(iterMeans(rec(sr))))]
### go through iterations and create residuals
### use kernel density to create smooth distribution of residuals
### and sample from this distribution
res_new <- foreach(iter_i = seq(dim(sr)[6])) %do% {
set.seed(iter_i)
### get residuals for current iteration
res_i <- c(FLCore::iter(residuals(sr), iter_i))
res_i <- res_i[!is.na(res_i)]
### calculate kernel density of residuals
density <- density(x = res_i)
### sample residuals
mu <- sample(x = res_i, size = length(yrs_res), replace = TRUE)
### "smooth", i.e. sample from density distribution
res_new <- rnorm(n = length(yrs_res), mean = mu, sd = density$bw)
### "add" autocorrelation
if (isTRUE(sr_ar_check) & isTRUE(sr_rho >= ci)) {
sr_acf_i <- acf(res_i, lag.max = 1, plot = FALSE, na.action = na.exclude)
sr_rho_i <- sr_acf_i$acf[2]
res_ac <- rep(0, length(yrs_res))
res_ac[1] <- sr_rho * tail(res_i, 1) + sqrt(1 - sr_rho^2) * res_new[1]
for (r in 2:length(res_ac)) {
res_ac[r] <- sr_rho * res_ac[r - 1] + sqrt(1 - sr_rho^2) * res_new[r]
}
res_new <- res_ac
}
return(res_new)
}
### insert into model
residuals(sr)[, yrs_res] <- unlist(res_new)
### exponentiate residuals to get factor
residuals(sr) <- exp(residuals(sr))
sr_res <- residuals(sr)
### ---------------------------------------------------------------------- ###
### process noise ####
### create FLQuant with process noise
### this will be added to the values obtained from fwd() in the MSE
if (isTRUE(process_error)) {
message("including survival process error")
### create noise for process error
set.seed(3)
proc_res <- stock.n(stk_stf) %=% 0 ### template FLQuant
proc_res[] <- stats::rnorm(n = length(proc_res), mean = 0,
sd = uncertainty$proc_error)
### the proc_res values follow a normal distribution,
### exponentiate to get log-normal residuals
proc_res <- exp(proc_res)
### proc_res is a factor by which the numbers at age are multiplied
### for historical period, numbers already include process error from SAM
### -> remove deviation
proc_res[, dimnames(proc_res)$year <= yr_data] <- 1
### remove deviation for first age class (recruits)
proc_res[1, ] <- 1
} else {
message("NOT including survival process error")
proc_res <- 1
}
### try saving in stock recruitment model ...
### this gets passed on to the projection module
fitted(sr) <- proc_res
### ---------------------------------------------------------------------- ###
### stf ####
### for intermediate year
### not done for plaice
stk_fwd <- stk_stf
### ---------------------------------------------------------------------- ###
### biological data for OEM ####
message("OEM biological data")
### base on OM stock
stk_oem <- stk_fwd
### if alternative M scenario, MP does not know this
if (!is.null(M_alternative)) {
M_yrs <- dimnames(stk_data_input)$year
m(stk_oem)[, M_yrs] <- m(stk_data_input)
}
### projection years
proj_yrs <- (yr_data + 1):range(stk_oem)[["maxyear"]]
### use means of sampled values for projection period
catch.wt(stk_oem)[, ac(proj_yrs)] <-
yearMeans(catch.wt(stk_oem)[, ac(sample_yrs)])
landings.wt(stk_oem)[, ac(proj_yrs)] <-
yearMeans(landings.wt(stk_oem)[, ac(sample_yrs)])
discards.wt(stk_oem)[, ac(proj_yrs)] <-
yearMeans(discards.wt(stk_oem)[, ac(sample_yrs)])
stock.wt(stk_oem)[, ac(proj_yrs)] <-
yearMeans(stock.wt(stk_oem)[, ac(sample_yrs)])
m(stk_oem)[, ac(proj_yrs)] <- yearMeans(m(stk_oem)[, ac(sample_yrs)])
mat(stk_oem)[, ac(proj_yrs)] <- yearMeans(mat(stk_oem)[, ac(sample_yrs)])
### remove stock assessment results
stock.n(stk_oem)[] <- stock(stk_oem)[] <- harvest(stk_oem)[] <- NA
### density dependent M
if (isTRUE(M_dd)) {
### last key run was in M_dd_yr, with the last data year M_dd_yr - 1
### so M M_dd_yr:M_dd_yr+2 will be the mean of M_dd_yr-1:M_dd_yr-3 of OM
### and M M_dd_yr+3:M_dd_yr+5 mean of M_dd_yr:M_dd_yr+2
m(stk_oem)[, ac(M_dd_yr:(M_dd_yr + 2))] <-
yearMeans(m(stk_oem)[, ac((M_dd_yr - 1):(M_dd_yr - 3))])
}
### ---------------------------------------------------------------------- ###
### catch noise ####
### take estimates from sam: uncertainty$catch_sd is "logSdLogObs"
### assume catch observed by SAM in projection is log-normally distributed
### around operating model catch
if (isTRUE(catch_oem_error)) {
message("including catch observation error")
### create noise for catch
set.seed(5)
catch_res <- catch.n(stk_fwd) %=% 0 ### template FLQuant
catch_res[] <- stats::rnorm(n = length(catch_res), mean = 0,
sd = uncertainty$catch_sd)
### the catch_res values are on a normale scale,
### exponentiate to get log-normal
catch_res <- exp(catch_res)
### catch_res is a factor by which the numbers at age are multiplied
### for historical period, pass on real observed catch
### -> remove deviation
if (isFALSE(disc_survival > 0)) {
catch_res[, dimnames(catch_res)$year <= yr_data] <-
window(catch.n(stk_orig), end = yr_data) /
window(catch.n(stk_fwd), end = yr_data)
} else {
catch_res[, dimnames(catch_res)$year <= yr_data] <-
window(catch.n(stk_data_input), end = yr_data) /
window(catch.n(stk_fwd), end = yr_data)
}
} else {
message("NOT including catch observation error")
catch_res <- catch.n(stk_fwd) %=% 1
}
### ---------------------------------------------------------------------- ###
### indices ####
message("creating OM indices")
### use real FLIndices object as template (use all)
idx <- idx_data
### extend for simulation period
idx <- window(idx, end = yr_data + n_years)
### add iterations
idx <- lapply(idx, propagate, n)
### extract some dimension names
idx_yrs <- lapply(idx, function(x) as.numeric(dimnames(x)$year))
idx_yrs_hist <- lapply(idx_yrs, function(x) setdiff(x, yrs_proj))
idx_ages <- lapply(idx, function(x) dimnames(x)$age)
### set catchability for projection
for (idx_i in seq_along(idx)) {
index.q(idx[[idx_i]])[] <- uncertainty$survey_catchability[[idx_i]]
}
### index weights
if (isTRUE(length(idx_weights) < length(idx))) {
idx_weights <- rep(idx_weights, length(idx))[seq(length(idx))]
}
for (idx_i in seq_along(idx)) {
if (isTRUE(idx_weights[idx_i] == "none")) {
next
### take weights from OM stock
} else if (isTRUE(idx_weights[idx_i] %in% c("stock.wt", "catch.wt"))) {
get.weights_i <- get(x = idx_weights[idx_i])
catch.wt(idx[[idx_i]]) <-
get.weights_i(stk_oem)[ac(idx_ages[[idx_i]]), ac(idx_yrs[[idx_i]])]
### use weights from index - resample
} else if (isTRUE(idx_weights[idx_i] %in% c("index.wt"))) {
catch.wt(idx[[idx_i]])[, ac(proj_yrs)] <-
yearMeans(catch.wt(idx[[idx_i]])[, ac(sample_yrs)])
}
}
### create copy of index with original values
idx_raw <- lapply(idx ,index)
### calculate index numbers
idx <- calc_survey(stk = stk_fwd, idx = idx, use_q = TRUE, use_time = TRUE,
use_wt = FALSE)
### create deviances for indices
### first, get template
idx_dev <- lapply(idx, index)
### create random noise based on sd
set.seed(4)
for (idx_i in seq_along(idx_dev)) {
### insert sd
idx_dev[[idx_i]][] <- uncertainty$survey_sd[[idx_i]]
### noise
idx_dev[[idx_i]][] <- stats::rnorm(n = length(idx_dev[[idx_i]]),
mean = 0, sd = idx_dev[[idx_i]])
### exponentiate to get from normal to log-normal scale
idx_dev[[idx_i]] <- exp(idx_dev[[idx_i]])
}
### modify residuals for historical period so that index values passed to
### stock assessment are the ones observed in reality
for (idx_i in seq_along(idx_dev)) {
idx_dev[[idx_i]][, ac(idx_yrs_hist[[idx_i]])] <-
idx_raw[[idx_i]][, ac(idx_yrs_hist[[idx_i]])] /
index(idx[[idx_i]])[, ac(idx_yrs_hist[[idx_i]])]
}
### add template for biomass index
if (!isFALSE(idxB)) {
message("adding biomass index template")
if (is.numeric(idxB)) idxB <- names(idx)[idxB]
idxB_template <- quantSums(idx[[idxB]]@catch.wt *
idx[[idxB]]@index) %=% NA_real_
idx <- FLIndices(c(idx, idxB = FLIndex(index = idxB_template)))
idx_dev[["idxB"]] <- idxB_template
}
### ---------------------------------------------------------------------- ###
### length index ####
if (isTRUE(idxL)) {
message("adding length index")
if (!is.null(ALK_yrs))
ALKs <- ALKs %>%
filter(year %in% ALK_yrs)
ALKs <- ALKs %>%
arrange(year, age, length)
### randomly select ALK years
set.seed(89)
alk_samples <- catch(stk_fwd) %=% NA_real_
alk_samples[] <- sample(x = ALK_yrs, size = length(yrs_mse) * n,
replace = TRUE)
### use existing ALKs for historical years
alk_samples[, ac(ALK_yrs)] <- ALK_yrs
### pre-populate index
set.seed(91)
data_yrs <- range(stk_fwd)[["minyear"]]:yr_data
### match ALK year with data year
lmean <- full_join(
### use observed catch
x = as.data.frame(catch.n(stk_fwd)[, ac(data_yrs)]) %>%
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(alk_samples[, ac(data_yrs)]) %>%
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 >= refpts$Lc) %>%
### mean catch length above Lc
group_by(year, iter) %>%
summarise(data = mean(sample(x = length, prob = cal,
size = length_samples, replace = TRUE)),
.groups = "keep") %>%
arrange(as.numeric(as.character(iter)))
### convert into FLQuant
idxL <- window(as.FLQuant(lmean), end = dims(stk_fwd)$maxyear)
### add to index
idx <- FLIndices(c(idx, idxL = FLIndex(index = idxL)))
idx_dev[["idxL"]] <- idxL %=% 1
idx_dev[["alk_yrs"]] <- alk_samples
}
### ------------------------------------------------------------------------ ###
### PA buffer for 2 over 3 rule ####
### SPiCT performance based on
### Fischer et al. 2021 https://doi.org/10.1093/icesjms/fsab018
### index deviation
if (isTRUE(PA_status)) {
message("adding template for PA status")
PA_status_dev <- FLQuant(NA, dimnames = list(age = c("positive", "negative"),
year = dimnames(stk_fwd)$year,
iter = dimnames(stk_fwd)$iter))
set.seed(1)
PA_status_dev["positive"] <- rbinom(n = PA_status_dev["positive"],
size = 1, prob = 0.9886215)
set.seed(2)
PA_status_dev["negative"] <- rbinom(n = PA_status_dev["negative"],
size = 1, prob = 1 - 0.4216946)
### PA status index template
PA_status_template <- FLIndex(index = ssb(stk_fwd) %=% NA_integer_)
### add to index object
idx <- FLIndices(c(idx, PA_status = PA_status_template))
idx_dev[["PA_status"]] <- PA_status_dev
}
### ---------------------------------------------------------------------- ###
### format reference points ####
refpts <- FLPar(refpts, unit = "")
### ---------------------------------------------------------------------- ###
### save ####
if (isTRUE(save)) {
### path
input_path <- paste0("input/", stock_id, "/", OM, "/", n, "_", n_years, "/")
dir.create(input_path, recursive = TRUE)
### SAM model fit
saveRDS(fit, file = paste0(input_path, "SAM_fit.rds"))
### stock
saveRDS(stk_fwd, file = paste0(input_path, "stk.rds"))
### stock recruitment
saveRDS(sr, file = paste0(input_path, "sr.rds"))
### surveys
saveRDS(idx, file = paste0(input_path, "idx.rds"))
saveRDS(idx_dev, file = paste0(input_path, "idx_dev.rds"))
### catch noise
saveRDS(catch_res, file = paste0(input_path, "catch_res.rds"))
### process error
saveRDS(proc_res, file = paste0(input_path, "proc_res.rds"))
### observed stock
saveRDS(stk_oem, file = paste0(input_path, "stk_oem.rds"))
### sam initial parameters
saveRDS(pars_ini, file = paste0(input_path, "SAM_initial.rds"))
### sam configuration
saveRDS(SAM_conf, file = paste0(input_path, "SAM_conf.rds"))
### reference values
saveRDS(refpts, file = paste0(input_path, "refpts_mse.rds"))
### age-length keys
saveRDS(ALKs, file = paste0(input_path, "ALKs.rds"))
### density dependent M
if (isTRUE(M_dd)) {
saveRDS(list(dd_M_relation = dd_M_relation, M_dd_yr = M_dd_yr,
M_dd_migration = M_dd_migration),
file = paste0(input_path, "dd_M.rds"))
}
}
if (isTRUE(return)) {
return(list(stk_fwd = stk_fwd, sr = sr, idx = idx, idx_dev = idx_dev,
catch_res = catch_res, proc_res = proc_res, stk_oem = stk_oem,
pars_ini = pars_ini, SAM_conf = SAM_conf, refpts = refpts,
ALKs = ALKs))
}
}
### ------------------------------------------------------------------------ ###
### create input for mp() ####
### ------------------------------------------------------------------------ ###
### this function loads the elements required for running mse::mp(),
### adapts them if necessary (dimensions), sets the MP and
### creates the input object for mp()
input_mp <- function(stock_id = "ple.27.7e", OM = "baseline", n_iter = 1000,
n_yrs = 100, yr_start = 2021, iy = yr_start - 1,
n_blocks = 1, seed = 1, cut_hist = TRUE, MP = "rfb",
hr_years = NULL,
migration = NULL,
disc_survival = 0, rec_failure = FALSE,
use_age_idcs = NULL, biomass_index = NULL,
idx_timing = NULL, catch_timing = NULL,
fwd_yrs_rec_start = NULL,
fwd_splitLD = NULL,
fwd_yrs_average = NULL,
fwd_yrs_sel = NULL,
fwd_trgt = NULL, fwd_yrs = NULL
) {
### path to input objects
path_input <- paste0("input/", stock_id, "/", OM, "/", n_iter, "_100/")
### load objects
### use full dimensions (100 years, 1000 iterations) - reduced later
stk_fwd <- readRDS(paste0(path_input, "stk.rds"))
sr <- readRDS(paste0(path_input, "sr.rds"))
idx <- readRDS(paste0(path_input, "idx.rds"))
idx_dev <- readRDS(paste0(path_input, "idx_dev.rds"))
catch_res <- readRDS(paste0(path_input, "catch_res.rds"))
proc_res <- readRDS(paste0(path_input, "proc_res.rds"))
stk_oem <- readRDS(paste0(path_input, "stk_oem.rds"))
SAM_pars_ini <- readRDS(paste0(path_input, "SAM_initial.rds"))
SAM_conf <- readRDS(paste0(path_input, "SAM_conf.rds"))
ALKs <- readRDS(paste0(path_input, "ALKs.rds"))
refpts_mse <- readRDS(paste0(path_input, "refpts_mse.rds"))
if (identical(OM, "M_dd"))
dd_M <- readRDS(paste0(path_input, "dd_M.rds"))
### find last year
yr_end <- yr_start + n_yrs - 1
### reduce dimensions, if requested
if (isTRUE(n_yrs < 100)) {
### OM stock
stk_fwd <- window(stk_fwd, end = yr_end)
### OEM stock
stk_oem <- window(stk_oem, end = yr_end)
### stock-recruitment model - need to go through elements individually
rec(sr) <- window(rec(sr), end = yr_end)
ssb(sr) <- window(ssb(sr), end = yr_end)
residuals(sr) <- window(residuals(sr), end = yr_end)
fitted(sr) <- window(fitted(sr), end = yr_end)
range(sr)[["maxyear"]] <- yr_end
### index
idx <- window(idx, end = yr_end)
### residuals
idx_dev <- window(idx_dev, end = yr_end)
catch_res <- window(catch_res, end = yr_end)
proc_res <- window(proc_res, end = yr_end)
}
if (isTRUE(n_iter < 1000)) {
### OM stock
stk_fwd <- FLCore::iter(stk_fwd, seq(n_iter))
### OEM stock
stk_oem <- FLCore::iter(stk_oem, seq(n_iter))
### stock-recruitment model
sr <- FLCore::iter(sr, seq(n_iter))
### index
idx <- FLCore::iter(idx, seq(n_iter))
### residuals
idx_dev <- FLCore::iter(idx_dev, seq(n_iter))
catch_res <- FLCore::iter(catch_res, seq(n_iter))
proc_res <- FLCore::iter(proc_res, seq(n_iter))
### reference points
if (isTRUE(n_iter < dims(refpts_mse)$iter))
refpts_mse <- iter(refpts_mse, seq(n_iter))
}
### ---------------------------------------------------------------------- ###
### recruitment failure? ####
### ---------------------------------------------------------------------- ###
if (!isFALSE(rec_failure)) {
residuals(sr)[, ac(rec_failure)] <- residuals(sr)[, ac(rec_failure)] * 0.1
}
### ---------------------------------------------------------------------- ###
### generic arguments ####
args <- list(fy = yr_end, ### final simulation year
y0 = dims(stk_fwd)$minyear, ### first data year
data_lag = 1, ### make ay catch available
management_lag = 1, ### save advice in tracking's ay
iy = iy, ### first simulation (intermediate) year
nsqy = 3, ### not used, but has to provided
nblocks = n_blocks, ### block for parallel processing
seed = seed ### random number seed before starting MSE
)
### ---------------------------------------------------------------------- ###
### reference values ####
# refpts_mse <- FLPar(refpts_mse, iter = n_iter)
### ---------------------------------------------------------------------- ###
### Operating model (OM) ####
om <- FLom(stock = stk_fwd, ### stock
sr = sr, ### stock recruitment and precompiled residuals
projection = mseCtrl(method = fwd_attr,
args = list(maxF = 5,
### process noise on stock.n
proc_res = "fitted",
dupl_trgt = FALSE,
disc_survival = disc_survival
))
)
### ---------------------------------------------------------------------- ###
### migration ####
if (!is.null(migration)) {
if (isTRUE(length(migration) == 1)) {
migr_fct <- seq(from = 1, to = migration, length.out = n_yrs + 1)
migr_fct <- migr_fct[-1]/migr_fct[-length(migr_fct)]
#prod(migr_fct)
} else {
migr_fct <- migration
}
migr_qnt <- ssb(stk_fwd) %=% 1
migr_qnt[, ac(yr_start:dims(migr_qnt)$maxyear)] <- migr_fct
### store in sr ssb slot
om@sr@ssb <- migr_qnt
om@projection@args$migration = "ssb"
}
### ---------------------------------------------------------------------- ###
### Observation (error) model OEM ####
if (identical(stock_id, "ple.27.7e")) {
if (is.null(use_age_idcs)) use_age_idcs <- c("Q1SWBeam", "FSP-7e")
if (is.null(biomass_index)) biomass_index <- "FSP-7e"
if (is.null(idx_timing)) idx_timing <- c(-1, -1)
if (is.null(catch_timing)) catch_timing <- -1
} else if (identical(stock_id, "cod.27.47d20")) {
if (is.null(use_age_idcs))
use_age_idcs <- c("IBTS_Q1_gam", "IBTS_Q3_gam", "IBTS_Q3_gam_age0")
if (is.null(biomass_index)) biomass_index <- "IBTS_Q3_gam"
if (is.null(idx_timing)) idx_timing <- c(0, -1, 0)
if (is.null(catch_timing)) catch_timing <- -1
} else if (identical(stock_id, "her.27.3a47d")) {
if (is.null(use_age_idcs))
use_age_idcs <- c("HERAS", "IBTS-Q1", "IBTS0", "IBTS-Q3")
if (is.null(biomass_index)) biomass_index <- "HERAS"
if (is.null(idx_timing)) idx_timing <- c(-1, 0, 0, -1)
if (is.null(catch_timing)) catch_timing <- -1
}
### default oem for rfb rule
oem <- FLoem(method = obs_generic,
observations = list(
stk = stk_oem,
idx = idx),
deviances = list(
stk = FLQuants(catch.dev = catch_res),
idx = idx_dev),
args = list(use_catch_residuals = TRUE,
use_idx_residuals = TRUE,
use_stk_oem = TRUE,
use_wt = TRUE,
alks = as.data.frame(ALKs),
use_age_idcs = use_age_idcs,
biomass_index = biomass_index,
length_idx = TRUE,
Lc = unique(c(refpts_mse["Lc"])),
lngth_samples = 2000))
### 2 over 3 rule: biomass index and PA status relative to OM + error
if (isTRUE(MP == "2over3")) {
oem@args$length_idx <- FALSE ### no length index
oem@args$PA_status <- TRUE
oem@args$PA_status_dev <- TRUE
oem@args$PA_Bmsy <- unique(c(refpts_mse["Bmsy"])) ### real MSY from OM
oem@args$PA_Fmsy <- unique(c(refpts_mse["Fmsy"]))
### 2 over 3 based on XSA
} else if (isTRUE(MP == "2over3_XSA")) {
oem@args$length_idx <- FALSE ### no length index
oem@args$PA_status <- TRUE
oem@args$PA_status_dev <- TRUE
### ICES category 1 with SAM
} else if (isTRUE(MP == "ICES_SAM")) {
oem@observations$idx <- oem@observations$idx[use_age_idcs] ### age indices only
oem@deviances$idx <- oem@deviances$idx[use_age_idcs]
oem@args <- list(cut_idx = TRUE, idx_timing = idx_timing,
catch_timing = catch_timing, use_catch_residuals = TRUE,
use_idx_residuals = TRUE, use_stk_oem = TRUE)
### harvest rate based on biomass index
} else if (isTRUE(MP == "hr")) {
### remove redundant indices and arguments
oem@observations$idx <- oem@observations$idx[c(use_age_idcs, "idxB")]
oem@deviances$idx <- oem@deviances$idx[c(use_age_idcs, "idxB")]
oem@args$alks <- NULL
oem@args$length_idx <- FALSE
oem@args$Lc <- NULL
oem@args$lngth_samples <- NULL
} else if (isTRUE(MP == "constF")) {
oem <- FLoem(method = oem_dummy,
observations = list(stk = FLQuant(0),
idx = FLQuant()),
deviances = list(stk = FLQuant(0),
idx = FLQuant()))
}
### ---------------------------------------------------------------------- ###
### Management procedure MP ####
if (isTRUE(MP == "rfb")) {
### I_trigger = 1.4 * I_loss
I_trigger = apply(quantSums(index(idx[[biomass_index]]) *
idx_dev[[biomass_index]] *
catch.wt(idx[[biomass_index]])),
6, min, na.rm = TRUE) * 1.4
ctrl <- mpCtrl(list(
est = mseCtrl(method = est_comps,
args = list(comp_r = TRUE, comp_f = TRUE, comp_b = TRUE,
comp_c = TRUE, comp_m = 1,
idxB_lag = 1, idxB_range_1 = 2, idxB_range_2 = 3,
idxB_range_3 = 1,
catch_lag = 0, ### 0 to mimic advice
catch_range = 1,
Lref = unique(c(refpts_mse["Lref"])),
I_trigger = c(I_trigger),
idxL_lag = 1, idxL_range = 1)),
phcr = mseCtrl(method = phcr_comps,
args = list(exp_r = 1, exp_f = 1, exp_b = 1)),
hcr = mseCtrl(method = hcr_comps,
args = list(interval = 2)),
isys = mseCtrl(method = is_comps,
args = list(interval = 2,
upper_constraint = 1.2, lower_constraint = 0.7,
cap_below_b = FALSE))
))
} else if (isTRUE(MP == "hr")) {
idxB <- quantSums(index(idx[[biomass_index]]) *
idx_dev[[biomass_index]] *
catch.wt(idx[[biomass_index]]))
idxB_yrs <- dimnames(idxB)$year[which(!is.na(iterMeans(idxB)))]
idxC <- quantSums(catch.n(stk_fwd) * catch.wt(stk_fwd) *
oem@deviances$stk$catch.dev)
### I_trigger = 1.4 * I_loss
I_trigger = apply(idxB, 6, min, na.rm = TRUE) * 1.4
### define target harvest rate through reference years
hr_values <- idxC[, ac(idxB_yrs)]/idxB[, ac(idxB_yrs)]
if (is.null(hr_years)) {
if (identical(stock_id, "ple.27.7e")) {
hr_years <- 2014
} else if (identical(stock_id, "cod.27.47d20")) {
hr_years <- c(2008:2013, 2015:2019)
} else if (identical(stock_id, "her.27.3a47d")) {
hr_years <- c(1989:2020)
} else {
hr_years <- NULL
}
}
### target is mean harvest of target years * 0.5 (WKLIFE X)
hr_target <- yearMeans(hr_values[, ac(hr_years)]) * 0.5
ctrl <- mpCtrl(list(
est = mseCtrl(method = est_comps,
args = list(comp_r = FALSE, comp_f = FALSE, comp_b = TRUE,
comp_c = FALSE, comp_m = 1,
comp_i = TRUE, comp_hr = c(hr_target),
idxB_lag = 1, idxB_range_3 = 1,
I_trigger = c(I_trigger))),
phcr = mseCtrl(method = phcr_comps,
args = list(exp_r = 1, exp_f = 1, exp_b = 1)),
hcr = mseCtrl(method = hcr_comps,
args = list(interval = 1)),
isys = mseCtrl(method = is_comps,
args = list(interval = 1,
upper_constraint = 1.2, lower_constraint = 0.7,
cap_below_b = FALSE))
))
} else if (isTRUE(MP == "2over3")) {
ctrl <- mpCtrl(list(
est = mseCtrl(method = est_comps,
args = list(comp_r = TRUE, comp_f = FALSE, comp_b = FALSE,
comp_c = TRUE, comp_m = 1,
idxB_lag = 1, idxB_range_1 = 2, idxB_range_2 = 3,
catch_lag = 0, ### 0 to mimic advice
catch_range = 1,
pa_buffer = TRUE,
pa_size = 0.8, pa_duration = 3)),
phcr = mseCtrl(method = phcr_comps),
hcr = mseCtrl(method = hcr_comps,
args = list(interval = 2)),
isys = mseCtrl(method = is_comps,
args = list(interval = 2,
upper_constraint = 1.2, lower_constraint = 0.8,
cap_below_b = TRUE))))
} else if (isTRUE(MP == "2over3_XSA")) {
FLXSA_control <- FLXSA.control(fse = 1.0, rage = 0, qage = 6,
shk.n = FALSE,
shk.ages = 3, shk.yrs = 3,
min.nse = 0.3,
tspower = 0, tsrange = 100,
maxit = 100)
ctrl <- mpCtrl(list(
est = mseCtrl(method = est_comps,
args = list(comp_r = TRUE, comp_f = FALSE, comp_b = FALSE,
comp_c = TRUE, comp_m = 1, pa_buffer = TRUE,
idxB_lag = 1,
idxB_range_1 = 2, idxB_range_2 = 3,
catch_lag = 0, ### 0 to mimic advice
catch_range = 1,
FLXSA = TRUE,
FLXSA_control = FLXSA_control,
FLXSA.control = NULL,
FLXSA_landings = TRUE, ### landings only?
FLXSA_idcs = c("Q1SWBeam", "FSP-7e"),
FLXSA_stf = TRUE,
### from WGCSE
FLXSA_Btrigger = unique(c(refpts_mse["ICES_Btrigger"])),
FLXSA_Ftrigger = unique(c(refpts_mse["ICES_Fmsy"]))
)),
phcr = mseCtrl(method = phcr_comps),
hcr = mseCtrl(method = hcr_comps,
args = list(interval = 1)), ### annual
isys = mseCtrl(method = is_comps,
args = list(interval = 2, ### annual
upper_constraint = 1.2, lower_constraint = 0.8,
cap_below_b = TRUE))))
} else if (isTRUE(MP == "ICES_SAM")) {
if (stock_id %in% c("ple.27.7e", "cod.27.47d20")) {
### some specifications for short term forecast with SAM
if (identical(stock_id, "ple.27.7e")) {
if (is.null(fwd_yrs_rec_start)) fwd_yrs_rec_start <- 1980
if (is.null(fwd_splitLD)) fwd_splitLD <- TRUE
if (is.null(fwd_yrs_average)) fwd_yrs_average <- -4:0
if (is.null(fwd_yrs_sel)) fwd_yrs_sel <- -4:0
if (is.null(fwd_trgt)) fwd_trgt <- c("fsq", "fsq", "fsq")
if (is.null(fwd_yrs)) fwd_yrs <- 2
} else if (identical(stock_id, "cod.27.47d20")) {
if (is.null(fwd_yrs_rec_start)) fwd_yrs_rec_start <- 1998
if (is.null(fwd_splitLD)) fwd_splitLD <- TRUE
if (is.null(fwd_yrs_average)) fwd_yrs_average <- -2:0
if (is.null(fwd_yrs_sel)) fwd_yrs_sel <- NULL
if (is.null(fwd_trgt)) fwd_trgt <- c("fsq")
if (is.null(fwd_yrs)) fwd_yrs <- 1