-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWP_RecCovariates_Gaichas.Rmd
773 lines (503 loc) · 29.3 KB
/
WP_RecCovariates_Gaichas.Rmd
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
---
title: "Working Paper: Recruitment covariate testing in WHAM"
author: "Sarah Gaichas and Jon Deroba"
date: "`r Sys.Date()`"
output:
bookdown::pdf_document2:
includes:
in_header: latex/header1.tex
keep_tex: true
bookdown::html_document2:
toc: true
toc_float: true
code_fold: hide
bookdown::word_document2:
toc: true
link-citations: yes
csl: "canadian-journal-of-fisheries-and-aquatic-sciences.csl"
bibliography: zoopindex.bib
urlcolor: blue
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = FALSE,
warning = FALSE)
library(tidyverse)
library(here)
library(DT)
library(patchwork)
library(wham)
```
# Introduction
The Atlantic herring research track assessment working group (WG) prioritized investigation of recruitment drivers as potential stock assessment model covariates, because low recruitment in recent years is an important issue for the stock and for fishery management.
The WG used a boosted regression tree analysis (Molina 2024) to identify zooplankton indices that best explained patterns in herring recruitment. These indices included large copepods in spring (influencing growth of herring postlarvae and juveniles), small copeopods in fall (influencing survival of herring larvae over the winter), haddock egg predation (influencing egg mortality), and temperature (influencing larval and juvenile survival).
In this working paper, we evaluate each of these indices as potential recruitment covariates in the Atlantic herring assessment implemented during the research track in the Woods Hole Assessment Model (WHAM, @stock_woods_2021).
As a sensitivity test, we also evaluate the effects of potential recruitment covariates with NAA random effects turned off in the Atlantic herring assessment model.
# Methods
We are using the `devel` version of WHAM: https://github.com/timjmiller/wham/tree/devel
Model [mm192](https://drive.google.com/drive/folders/1sQdDsfdnVbiiY4X7Rgr-fvegwT7Fa1Az?usp=drive_link) is our starting point.
Haddock egg predation, Zooplankton, and temperature indices were explored as covariates on herring recruitment. Methods for developing each indicator are in separate working papers.
Recruitment is modeled as deviations from the "recruitment scaling parameter", leaving one option for modeling effects of covariates on recruitment: "controlling".
A "controlling" recruitment covariate results in a time-varying recruitment scaling parameter.
[merge with Jon's ecisting text on the haddock egg predation testing here... ]
We explored indices with different zooplankton groups, seasons, and regions according to herring life history and results from the boosted regression tree:
* Jan-Jun (Spring) large copepods in spring herring BTS strata with lag-0 to represent food for pre-recruit juveniles
* Jul-Dec (Fall) small copepods in fall herring BTS strata with lag-1 to represent food for larvae in general
* Sep-Feb small copepods in herring larval area with lag-1 to represent food for larvae more specifically
* Combinations of large and small copepod covariates above
In addition, we explored an index of optimal temperature duration (days) during the fall larval season, September-December
We evaluated
* Options for covariate input (millions of cells vs. log(cells), VAST estimated SE vs. WHAM estimated SE)
* Options for covariate observation model ("rw" vs. "ar1")
* Options for recruitment link ("none" vs. "controlling-linear" with lag-0 for large copepods, lag-1 for small copepods, and lag-1 for larval temperature duration)
* No attempts were made to fit polynomial effects although this is possible in WHAM
The main diagnostics we used to determine if the model was improved by covariates were:
* model converged and Hessian matrix invertable
* dAIC lowest
* recruitment sigma reduced (how much?)
* estimated covariate effect CI does not include 0
* direction of covariate effect is sensible
Sensitivity runs
Turn off NAA RE and then try fitting with the most robust recruitment covariates.
# Results
Short story:
* Models with covariates input on the log scale generally converged
* Models with WHAM estimated covariate SE ("est_1") generally converged
* Under the above conditions, most models with and without the recruitment link converged
* Models with the Jan-Jun (Spring) large copepods covariate also converged with input as millions of cells and VAST estimated SE
[Way too much detail including false starts](https://noaa-edab.github.io/zooplanktonindex/WHAMcovariate_tests.html)
Summary table with each model, recruitment sigma compared with base, covariate beta with CI
We show diagnostics and results from the best fit models for each covariate in sections below.
Diagnostics include:
* WHAM's fit to the covariate time series
* One step ahead residuals for WHAM's fit
* Estimated time varying recruitment scaling parameter with N age 1
## Spring large copepods
*Models with no covariates had slightly better AIC than comparable models with recruitment links*
```{r, results='hide'}
config <- "lgcopeSpring2"
# name the model directory
name <- paste0("mm192_", config)
write.dir <- here::here(sprintf("WHAMfits/%s/", name))
# larger set of ecov setups to compare
df.mods <- data.frame(Recruitment = c(rep(2, 16)),
ecov_process = c(rep("rw",8),rep("ar1",8)),
ecov_how = c(rep("none",4),
rep("controlling-lag-0-linear",4)),
ecovdat = rep(c("logmean-logsig",
"logmean-est_1",
"meanmil-logsigmil",
"meanmil-est_1"),4),
stringsAsFactors=FALSE)
n.mods <- dim(df.mods)[1]
df.mods$Model <- paste0("m",1:n.mods)
df.mods <- dplyr::select(df.mods, Model, tidyselect::everything()) # moves Model to first col
mod.list <- paste0(write.dir, df.mods$Model,".rds")
mods <- lapply(mod.list, readRDS)
n.mods <- length(mod.list)
vign2_conv <- lapply(mods, function(x) capture.output(check_convergence(x)))
for(m in 1:n.mods) cat(paste0("Model ",m,":"), vign2_conv[[m]], "", sep='\n')
opt_conv = 1-sapply(mods, function(x) x$opt$convergence)
ok_sdrep = sapply(mods, function(x) if(x$na_sdrep==FALSE & !is.na(x$na_sdrep)) 1 else 0)
df.mods$conv <- as.logical(opt_conv)
df.mods$pdHess <- as.logical(ok_sdrep)
df.mods$NLL <- sapply(mods, function(x) round(x$opt$objective,3))
not_conv <- !df.mods$conv | !df.mods$pdHess
mods2 <- mods
mods2[not_conv] <- NULL
df.aic.tmp <- as.data.frame(compare_wham_models(mods2, table.opts=list(sort=FALSE, calc.rho=T))$tab)
df.aic <- df.aic.tmp[FALSE,]
ct = 1
for(i in 1:n.mods){
if(not_conv[i]){
df.aic[i,] <- rep(NA,5)
} else {
df.aic[i,] <- df.aic.tmp[ct,]
ct <- ct + 1
}
}
df.mods <- cbind(df.mods, df.aic)
df.mods <- df.mods[order(df.mods$dAIC, na.last=TRUE),]
df.mods[is.na(df.mods$AIC), c('dAIC','AIC','rho_R','rho_SSB','rho_Fbar')] <- "---"
rownames(df.mods) <- NULL
top4 <- df.mods |>
dplyr::filter(ecovdat %in% ("logmean-est_1")) |>
dplyr::select(Model, ecov_process, ecov_how, NLL, conv, pdHess, dAIC, AIC)
```
```{r}
flextable::flextable(top4) |>
flextable::set_header_labels(ecov_process = "Process",
ecov_how = "Rec. link") |>
flextable::set_table_properties(layout = "autofit")
```
### Spring large copepods covariate: logscale ar1 diagnostics
The WHAM ar1 fit looks strange


### Spring large copepods covariate: logscale ar1 recruitment


```{r}
m10par <- readRDS(here::here("WHAMfits/mm192_lgcopeSpring2/m10/res_tables/parameter_estimates_table.RDS"))
m14par <- readRDS(here::here("WHAMfits/mm192_lgcopeSpring2/m14/res_tables/parameter_estimates_table.RDS"))
```
Without covariate, recruitment variance is `r round(m10par["stock 1 NAA $\\sigma$ (age 1)",1],3)`, and with is `r round(m14par["stock 1 NAA $\\sigma$ (age 1)",1],3)`; lgCopeSpring2 beta_1 is `r round(m14par["stock 1 Recruitment Ecov: lgCopeSpring2 $\\beta_1$",1],3)`, CI `r round(m14par["stock 1 Recruitment Ecov: lgCopeSpring2 $\\beta_1$",3],3)`, `r round(m14par["stock 1 Recruitment Ecov: lgCopeSpring2 $\\beta_1$",4],3)`
### Spring large copepods covariate: logscale rw diagnostics


### Spring large copepods covariate: logscale rw recruitment


```{r}
m2par <- readRDS(here::here("WHAMfits/mm192_lgcopeSpring2/m2/res_tables/parameter_estimates_table.RDS"))
m6par <- readRDS(here::here("WHAMfits/mm192_lgcopeSpring2/m6/res_tables/parameter_estimates_table.RDS"))
```
Without covariate, recruitment variance is `r round(m2par["stock 1 NAA $\\sigma$ (age 1)",1],3)`, and with is `r round(m6par["stock 1 NAA $\\sigma$ (age 1)",1],3)`; lgCopeSpring2 beta_1 is `r round(m6par["stock 1 Recruitment Ecov: lgCopeSpring2 $\\beta_1$",1],3)`, CI `r round(m6par["stock 1 Recruitment Ecov: lgCopeSpring2 $\\beta_1$",3],3)`, `r round(m6par["stock 1 Recruitment Ecov: lgCopeSpring2 $\\beta_1$",4],3)`
## Fall small copepods
*Models with no covariates had slightly better AIC than the ar1 model with recruitment links*
```{r, results='hide'}
config <- "smcopeFall2"
# name the model directory
name <- paste0("mm192_", config)
write.dir <- here::here(sprintf("WHAMfits/%s/", name))
# larger set of ecov setups to compare
df.mods <- data.frame(Recruitment = c(rep(2, 16)),
ecov_process = c(rep("rw",8),rep("ar1",8)),
ecov_how = c(rep("none",4),
rep("controlling-lag-0-linear",4)),
ecovdat = rep(c("logmean-logsig",
"logmean-est_1",
"meanmil-logsigmil",
"meanmil-est_1"),4),
stringsAsFactors=FALSE)
n.mods <- dim(df.mods)[1]
df.mods$Model <- paste0("m",1:n.mods)
df.mods <- dplyr::select(df.mods, Model, tidyselect::everything()) # moves Model to first col
mod.list <- paste0(write.dir, df.mods$Model,".rds")
mods <- lapply(mod.list, readRDS)
n.mods <- length(mod.list)
vign2_conv <- lapply(mods, function(x) capture.output(check_convergence(x)))
for(m in 1:n.mods) cat(paste0("Model ",m,":"), vign2_conv[[m]], "", sep='\n')
opt_conv = 1-sapply(mods, function(x) x$opt$convergence)
ok_sdrep = sapply(mods, function(x) if(x$na_sdrep==FALSE & !is.na(x$na_sdrep)) 1 else 0)
df.mods$conv <- as.logical(opt_conv)
df.mods$pdHess <- as.logical(ok_sdrep)
df.mods$NLL <- sapply(mods, function(x) round(x$opt$objective,3))
not_conv <- !df.mods$conv | !df.mods$pdHess
mods2 <- mods
mods2[not_conv] <- NULL
df.aic.tmp <- as.data.frame(compare_wham_models(mods2, table.opts=list(sort=FALSE, calc.rho=T))$tab)
df.aic <- df.aic.tmp[FALSE,]
ct = 1
for(i in 1:n.mods){
if(not_conv[i]){
df.aic[i,] <- rep(NA,5)
} else {
df.aic[i,] <- df.aic.tmp[ct,]
ct <- ct + 1
}
}
df.mods <- cbind(df.mods, df.aic)
df.mods <- df.mods[order(df.mods$dAIC, na.last=TRUE),]
df.mods[is.na(df.mods$AIC), c('dAIC','AIC','rho_R','rho_SSB','rho_Fbar')] <- "---"
rownames(df.mods) <- NULL
top4 <- df.mods |>
dplyr::filter(ecovdat %in% ("logmean-est_1")) |>
dplyr::select(Model, ecov_process, ecov_how, NLL, conv, pdHess, dAIC, AIC)
```
```{r}
flextable::flextable(top4) |>
flextable::set_header_labels(ecov_process = "Process",
ecov_how = "Rec. link") |>
flextable::set_table_properties(layout = "autofit")
```
### Fall small copepods covariate: logscale ar1 diagnostics


### Fall small copepods covariate: logscale ar1 recruitment


```{r}
m10par <- readRDS(here::here("WHAMfits/mm192_smcopeFall2/m10/res_tables/parameter_estimates_table.RDS"))
m14par <- readRDS(here::here("WHAMfits/mm192_smcopeFall2/m14/res_tables/parameter_estimates_table.RDS"))
```
Without covariate, recruitment variance is `r round(m10par["stock 1 NAA $\\sigma$ (age 1)",1],3)`, and with is `r round(m14par["stock 1 NAA $\\sigma$ (age 1)",1],3)`; smcopeFall2 beta_1 is `r round(m14par["stock 1 Recruitment Ecov: smCopeFall2 $\\beta_1$",1],3)`, CI `r round(m14par["stock 1 Recruitment Ecov: smCopeFall2 $\\beta_1$",3],3)`, `r round(m14par["stock 1 Recruitment Ecov: smCopeFall2 $\\beta_1$",4],3)`
## September - February small copepods in the larval herring area
*Model with ar1 covariate had slightly better AIC than models without recruitment links*
```{r, results='hide'}
config <- "smcopeSepFeb2"
# name the model directory
name <- paste0("mm192_", config)
write.dir <- here::here(sprintf("WHAMfits/%s/", name))
# larger set of ecov setups to compare
df.mods <- data.frame(Recruitment = c(rep(2, 16)),
ecov_process = c(rep("rw",8),rep("ar1",8)),
ecov_how = c(rep("none",4),
rep("controlling-lag-1-linear",4)),
ecovdat = rep(c("logmean-logsig",
"logmean-est_1",
"meanmil-logsigmil",
"meanmil-est_1"),4),
stringsAsFactors=FALSE)
n.mods <- dim(df.mods)[1]
df.mods$Model <- paste0("m",1:n.mods)
df.mods <- dplyr::select(df.mods, Model, tidyselect::everything()) # moves Model to first col
mod.list <- paste0(write.dir, df.mods$Model,".rds")
mod.list <- mod.list[-16] # mod 16 didnt run
mods <- lapply(mod.list, readRDS)
df.mods <- df.mods[-16,] # mod 16 didnt run
n.mods <- length(mod.list)
vign2_conv <- lapply(mods, function(x) capture.output(check_convergence(x)))
for(m in 1:n.mods) cat(paste0("Model ",m,":"), vign2_conv[[m]], "", sep='\n')
opt_conv = 1-sapply(mods, function(x) x$opt$convergence)
ok_sdrep = sapply(mods, function(x) if(x$na_sdrep==FALSE & !is.na(x$na_sdrep)) 1 else 0)
df.mods$conv <- as.logical(opt_conv)
df.mods$pdHess <- as.logical(ok_sdrep)
df.mods$NLL <- sapply(mods, function(x) round(x$opt$objective,3))
not_conv <- !df.mods$conv | !df.mods$pdHess
mods2 <- mods
mods2[not_conv] <- NULL
df.aic.tmp <- as.data.frame(compare_wham_models(mods2, table.opts=list(sort=FALSE, calc.rho=T))$tab)
df.aic <- df.aic.tmp[FALSE,]
ct = 1
for(i in 1:n.mods){
if(not_conv[i]){
df.aic[i,] <- rep(NA,5)
} else {
df.aic[i,] <- df.aic.tmp[ct,]
ct <- ct + 1
}
}
df.mods <- cbind(df.mods, df.aic)
df.mods <- df.mods[order(df.mods$dAIC, na.last=TRUE),]
df.mods[is.na(df.mods$AIC), c('dAIC','AIC','rho_R','rho_SSB','rho_Fbar')] <- "---"
rownames(df.mods) <- NULL
top4 <- df.mods |>
dplyr::filter(ecovdat %in% ("logmean-est_1")) |>
dplyr::select(Model, ecov_process, ecov_how, NLL, conv, pdHess, dAIC, AIC)
```
```{r}
flextable::flextable(top4) |>
flextable::set_header_labels(ecov_process = "Process",
ecov_how = "Rec. link") |>
flextable::set_table_properties(layout = "autofit")
```
### Sep-Feb small copepods in herring larval area covariate: logscale ar1 diagnostics


### Sep-Feb small copepods in herring larval area covariate: logscale ar1 recruitment


```{r}
m10par <- readRDS(here::here("WHAMfits/mm192_smcopeSepFeb2/m10/res_tables/parameter_estimates_table.RDS"))
m14par <- readRDS(here::here("WHAMfits/mm192_smcopeSepFeb2/m14/res_tables/parameter_estimates_table.RDS"))
```
Without covariate, recruitment variance is `r round(m10par["stock 1 NAA $\\sigma$ (age 1)",1],3)`, and with is `r round(m14par["stock 1 NAA $\\sigma$ (age 1)",1],3)`; smcopeSepFeb2 beta_1 is `r round(m14par["stock 1 Recruitment Ecov: smCopeSepFeb2 $\\beta_1$",1],3)`, CI `r round(m14par["stock 1 Recruitment Ecov: smCopeSepFeb2 $\\beta_1$",3],3)`, `r round(m14par["stock 1 Recruitment Ecov: smCopeSepFeb2 $\\beta_1$",4],3)`
## Alternate: Sep-Feb small copepods in fall herring survey strata covariate
*Both rw and ar1 models worked with covariates, rw better fit*
```{r, results='hide'}
config <- "smcopeSepFeb2_fallstrata"
# name the model directory
name <- paste0("mm192_", config)
write.dir <- here::here(sprintf("WHAMfits/%s/", name))
# larger set of ecov setups to compare
df.mods <- data.frame(Recruitment = c(rep(2, 16)),
ecov_process = c(rep("rw",8),rep("ar1",8)),
ecov_how = c(rep("none",4),
rep("controlling-lag-1-linear",4)),
ecovdat = rep(c("logmean-logsig",
"logmean-est_1",
"meanmil-logsigmil",
"meanmil-est_1"),4),
stringsAsFactors=FALSE)
n.mods <- dim(df.mods)[1]
df.mods$Model <- paste0("m",1:n.mods)
df.mods <- dplyr::select(df.mods, Model, tidyselect::everything()) # moves Model to first col
mod.list <- paste0(write.dir, df.mods$Model,".rds")
mod.list <- mod.list[-16] # mod 16 didnt run
mods <- lapply(mod.list, readRDS)
df.mods <- df.mods[-16,] # mod 16 didnt run
n.mods <- length(mod.list)
vign2_conv <- lapply(mods, function(x) capture.output(check_convergence(x)))
for(m in 1:n.mods) cat(paste0("Model ",m,":"), vign2_conv[[m]], "", sep='\n')
opt_conv = 1-sapply(mods, function(x) x$opt$convergence)
ok_sdrep = sapply(mods, function(x) if(x$na_sdrep==FALSE & !is.na(x$na_sdrep)) 1 else 0)
df.mods$conv <- as.logical(opt_conv)
df.mods$pdHess <- as.logical(ok_sdrep)
df.mods$NLL <- sapply(mods, function(x) round(x$opt$objective,3))
not_conv <- !df.mods$conv | !df.mods$pdHess
mods2 <- mods
mods2[not_conv] <- NULL
df.aic.tmp <- as.data.frame(compare_wham_models(mods2, table.opts=list(sort=FALSE, calc.rho=T))$tab)
df.aic <- df.aic.tmp[FALSE,]
ct = 1
for(i in 1:n.mods){
if(not_conv[i]){
df.aic[i,] <- rep(NA,5)
} else {
df.aic[i,] <- df.aic.tmp[ct,]
ct <- ct + 1
}
}
df.mods <- cbind(df.mods, df.aic)
df.mods <- df.mods[order(df.mods$dAIC, na.last=TRUE),]
df.mods[is.na(df.mods$AIC), c('dAIC','AIC','rho_R','rho_SSB','rho_Fbar')] <- "---"
rownames(df.mods) <- NULL
top4 <- df.mods |>
dplyr::filter(ecovdat %in% ("logmean-est_1")) |>
dplyr::select(Model, ecov_process, ecov_how, NLL, conv, pdHess, dAIC, AIC)
```
```{r}
flextable::flextable(top4) |>
flextable::set_header_labels(ecov_process = "Process",
ecov_how = "Rec. link") |>
flextable::set_table_properties(layout = "autofit")
```
### Sep-Feb small copepods in herring fall survey area covariate: logscale rw diagnostics


### Sep-Feb small copepods in herring fall survey area covariate: logscale rw recruitment


```{r}
m2par <- readRDS(here::here("WHAMfits/mm192_smcopeSepFeb2_fallstrata/m2/res_tables/parameter_estimates_table.RDS"))
m6par <- readRDS(here::here("WHAMfits/mm192_smcopeSepFeb2_fallstrata/m6/res_tables/parameter_estimates_table.RDS"))
```
Without covariate, recruitment variance is `r round(m2par["stock 1 NAA $\\sigma$ (age 1)",1],3)`, and with is `r round(m6par["stock 1 NAA $\\sigma$ (age 1)",1],3)`; smcopeSepFeb2 beta_1 is `r round(m6par["stock 1 Recruitment Ecov: smCopeSepFeb2 $\\beta_1$",1],3)`, CI `r round(m6par["stock 1 Recruitment Ecov: smCopeSepFeb2 $\\beta_1$",3],3)`, `r round(m6par["stock 1 Recruitment Ecov: smCopeSepFeb2 $\\beta_1$",4],3)`
## September - December duration of larval optimal temperature
*Both rw and ar1 models worked with covariates, rw better fit*
```{r, results='hide'}
config <- "LarvalTempDuration"
# name the model directory
name <- paste0("mm192_", config)
write.dir <- here::here(sprintf("WHAMfits/%s/", name))
# larger set of ecov setups to compare
# larger set of ecov setups to compare
df.mods <- data.frame(Recruitment = c(rep(2, 8)),
ecov_process = c(rep("rw",4),rep("ar1",4)),
ecov_how = rep(c("none","controlling-lag-1-linear"), 4),
ecovdat = c(rep("mean-est_1", 2),rep("logmean-est_1",2)),
stringsAsFactors=FALSE)
n.mods <- dim(df.mods)[1]
df.mods$Model <- paste0("m",1:n.mods)
df.mods <- dplyr::select(df.mods, Model, tidyselect::everything()) # moves Model to first col
mod.list <- paste0(write.dir, df.mods$Model,".rds")
mods <- lapply(mod.list, readRDS)
n.mods <- length(mod.list)
vign2_conv <- lapply(mods, function(x) capture.output(check_convergence(x)))
for(m in 1:n.mods) cat(paste0("Model ",m,":"), vign2_conv[[m]], "", sep='\n')
opt_conv = 1-sapply(mods, function(x) x$opt$convergence)
ok_sdrep = sapply(mods, function(x) if(x$na_sdrep==FALSE & !is.na(x$na_sdrep)) 1 else 0)
df.mods$conv <- as.logical(opt_conv)
df.mods$pdHess <- as.logical(ok_sdrep)
df.mods$NLL <- sapply(mods, function(x) round(x$opt$objective,3))
not_conv <- !df.mods$conv | !df.mods$pdHess
mods2 <- mods
mods2[not_conv] <- NULL
df.aic.tmp <- as.data.frame(compare_wham_models(mods2, table.opts=list(sort=FALSE, calc.rho=T))$tab)
df.aic <- df.aic.tmp[FALSE,]
ct = 1
for(i in 1:n.mods){
if(not_conv[i]){
df.aic[i,] <- rep(NA,5)
} else {
df.aic[i,] <- df.aic.tmp[ct,]
ct <- ct + 1
}
}
df.mods <- cbind(df.mods, df.aic)
df.mods <- df.mods[order(df.mods$dAIC, na.last=TRUE),]
df.mods[is.na(df.mods$AIC), c('dAIC','AIC','rho_R','rho_SSB','rho_Fbar')] <- "---"
rownames(df.mods) <- NULL
top4 <- df.mods |>
dplyr::filter(ecovdat %in% ("logmean-est_1")) |>
dplyr::select(Model, ecov_process, ecov_how, NLL, conv, pdHess, dAIC, AIC)
```
```{r}
flextable::flextable(top4) |>
flextable::set_header_labels(ecov_process = "Process",
ecov_how = "Rec. link") |>
flextable::set_table_properties(layout = "autofit")
```
### Duration of optimal larval temperature, Sept-Dec: logscale rw diagnostics


### Duration of optimal larval temperature, Sept-Dec: logscale rw recruitment


```{r}
m3par <- readRDS(here::here("WHAMfits/mm192_LarvalTempDuration/m3/res_tables/parameter_estimates_table.RDS"))
m4par <- readRDS(here::here("WHAMfits/mm192_LarvalTempDuration/m4/res_tables/parameter_estimates_table.RDS"))
```
Without covariate, recruitment variance is `r round(m3par["stock 1 NAA $\\sigma$ (age 1)",1],3)`, and with is `r round(m4par["stock 1 NAA $\\sigma$ (age 1)",1],3)`; LarvalTempDuration beta_1 is `r round(m4par["stock 1 Recruitment Ecov: LarvalTempDuration $\\beta_1$",1],3)`, CI `r round(m4par["stock 1 Recruitment Ecov: LarvalTempDuration $\\beta_1$",3],3)`, `r round(m4par["stock 1 Recruitment Ecov: LarvalTempDuration $\\beta_1$",4],3)`
## Sensitivity: Remove NAA RE, add September - December duration of larval optimal temperature
*Both rw and ar1 models worked with covariates, rw better fit*
```{r, results='hide'}
config <- "LarvalTempDuration_NAA_REoff"
# name the model directory
name <- paste0("mm192_", config)
write.dir <- here::here(sprintf("WHAMfits/%s/", name))
# larger set of ecov setups to compare
# larger set of ecov setups to compare
df.mods <- data.frame(Recruitment = c(rep(2, 8)),
ecov_process = c(rep("rw",4),rep("ar1",4)),
ecov_how = rep(c("none","controlling-lag-1-linear"), 4),
ecovdat = c(rep("mean-est_1", 2),rep("logmean-est_1",2)),
stringsAsFactors=FALSE)
n.mods <- dim(df.mods)[1]
df.mods$Model <- paste0("m",1:n.mods)
df.mods <- dplyr::select(df.mods, Model, tidyselect::everything()) # moves Model to first col
mod.list <- paste0(write.dir, df.mods$Model,".rds")
mods <- lapply(mod.list, readRDS)
n.mods <- length(mod.list)
vign2_conv <- lapply(mods, function(x) capture.output(check_convergence(x)))
for(m in 1:n.mods) cat(paste0("Model ",m,":"), vign2_conv[[m]], "", sep='\n')
opt_conv = 1-sapply(mods, function(x) x$opt$convergence)
ok_sdrep = sapply(mods, function(x) if(x$na_sdrep==FALSE & !is.na(x$na_sdrep)) 1 else 0)
df.mods$conv <- as.logical(opt_conv)
df.mods$pdHess <- as.logical(ok_sdrep)
df.mods$NLL <- sapply(mods, function(x) round(x$opt$objective,3))
not_conv <- !df.mods$conv | !df.mods$pdHess
mods2 <- mods
mods2[not_conv] <- NULL
df.aic.tmp <- as.data.frame(compare_wham_models(mods2, table.opts=list(sort=FALSE, calc.rho=T))$tab)
df.aic <- df.aic.tmp[FALSE,]
ct = 1
for(i in 1:n.mods){
if(not_conv[i]){
df.aic[i,] <- rep(NA,5)
} else {
df.aic[i,] <- df.aic.tmp[ct,]
ct <- ct + 1
}
}
df.mods <- cbind(df.mods, df.aic)
df.mods <- df.mods[order(df.mods$dAIC, na.last=TRUE),]
df.mods[is.na(df.mods$AIC), c('dAIC','AIC','rho_R','rho_SSB','rho_Fbar')] <- "---"
rownames(df.mods) <- NULL
top4 <- df.mods |>
dplyr::filter(ecovdat %in% ("logmean-est_1")) |>
dplyr::select(Model, ecov_process, ecov_how, NLL, conv, pdHess, dAIC, AIC)
```
```{r}
flextable::flextable(top4) |>
flextable::set_header_labels(ecov_process = "Process",
ecov_how = "Rec. link") |>
flextable::set_table_properties(layout = "autofit")
```
### Duration of optimal larval temperature, Sept-Dec: logscale rw diagnostics


### Duration of optimal larval temperature, Sept-Dec: logscale rw recruitment


```{r}
m3par <- readRDS(here::here("WHAMfits/mm192_LarvalTempDuration_NAA_REoff/m3/res_tables/parameter_estimates_table.RDS"))
m4par <- readRDS(here::here("WHAMfits/mm192_LarvalTempDuration_NAA_REoff/m4/res_tables/parameter_estimates_table.RDS"))
```
Without covariate, recruitment variance is `r round(m3par["stock 1 NAA $\\sigma$ (age 1)",1],3)`, and with is `r round(m4par["stock 1 NAA $\\sigma$ (age 1)",1],3)`; LarvalTempDuration beta_1 is `r round(m4par["stock 1 Recruitment Ecov: LarvalTempDuration $\\beta_1$",1],3)`, CI `r round(m4par["stock 1 Recruitment Ecov: LarvalTempDuration $\\beta_1$",3],3)`, `r round(m4par["stock 1 Recruitment Ecov: LarvalTempDuration $\\beta_1$",4],3)`
# Discussion
The duration of optimal larval temperature covariate resulted in slightly better model fits and reduced recruitment variability relative to the model without covariates. The effect of optimal larval temperature on recruitment was postive, as hypothesized, with fewer days of optimal temperature resulting in a lower recruitment scaling parameter. However, the confidence interval of the effect included 0.
While some of the zooplankton time series also resulted in slightly better model fits and reduced recruitment variability relative to the model without covariates, the effects of zooplankton were negative on recruitment. This relationship is opposite the hypothesized relationship between herring and food, which was expected to be positive.
The sensitivity run removing NAA RE resulted in a much stronger impact of the optimal larval temperature on recruitment.
# References