forked from sgaichas/poseidon-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestWeightAtStage.Rmd
More file actions
417 lines (304 loc) · 14.8 KB
/
Copy pathTestWeightAtStage.Rmd
File metadata and controls
417 lines (304 loc) · 14.8 KB
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
---
title: "Testing atlantisom: visualize weight at (st)age output. How should we interplolate this for true ages?"
author: "Sarah Gaichas and Christine Stawitz"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: html_document
bibliography: "packages.bib"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
# automatically create a bib database for R packages
knitr::write_bib(c(
.packages(), 'knitr', 'rmarkdown', 'tidyr', 'dplyr', 'ggplot2',
'data.table', 'here', 'ggforce', 'ggthemes'
), 'packages.bib')
```
## Introduction
Now we delve into the calculation of true age classes and weight-at-age from standard Atlantis output age class, which is generally in 10 stages of maxage/10 true age classes for all but the newest Atlantis models. Note that modern Atlantis models can output true age classes in two output files (from the wiki):
>**ANNAGEBIO.nc**: This output provides numbers in each annual age class (so mapped from Atlantis "age class" which can contain multiple years to true annual age classes). Set flag_age_output to 1 to get this output. Tracers provided are:
>
> + Numbers at age per species
>
>**ANNAGECATCH.nc**: This output provides numbers at annual age class (so mapped from Atlantis "age class" which can contain multiple years to true annual age classes) in the catch and discards (summed over all fleets). Set flag_age_output to 1 to get this output. Tracers provided are:
>
> + Numbers at age per species in the catch
> + Numbers at age per species in the discards
Future `atlantisom` users should take advantage of this direct output, and some `atlantisom` functions will have to be rewritten to use it.
However, The CCA model does not produce this output, and other legacy models may not, so we still need the `atlantisom` functions `calc_stage2age` and the associated `calc_Z`. This function will be tested. However, it produces numbers at age but not weight. Weight-at-true-age is needed as an input to stock assessment models using empirical weight at age. See discussion [here](https://sgaichas.github.io/poseidon-dev/RethinkSamplingFunctions.html).
Therefore, our first step is understanding what the 10 class weight at age (output as `mulength` from `calc_age2length`) looks like for a variety of species.
For all setup, etc, please see previous files. Full methods are explained [here](https://sgaichas.github.io/poseidon-dev/TrueBioTest.html) and [here](https://sgaichas.github.io/poseidon-dev/TrueLengthCompTest.html).
This page has visualizations for the NOBA model example, CERES Global Sustainability. At the end of the file we also review saved outputs from the CCA model example, Atlantis Summit Common Scenario 1.
```{r message=FALSE, warning=FALSE}
library(tidyr)
require(dplyr)
library(ggplot2)
library(data.table)
library(here)
library(ggforce)
library(ggthemes)
library(atlantisom)
```
```{r initialize}
initCCA <- FALSE
initNEUS <- FALSE
initNOBA <- TRUE
if(initCCA) source(here("config/CCConfig.R"))
if(initNEUS) source(here("config/NEUSConfig.R"))
if(initNOBA) source(here("config/NOBAConfig.R"))
```
```{r get_names, message=FALSE, warning=FALSE}
#Load functional groups
funct.groups <- load_fgs(dir=d.name,
file_fgs = functional.groups.file)
#Get just the names of active functional groups
funct.group.names <- funct.groups %>%
filter(IsTurnedOn == 1) %>%
select(Name) %>%
.$Name
```
```{r load_Rdata, message=FALSE, warning=FALSE}
if(initCCA) {
truth.file <- "outputCCV3run_truth.RData"
load(file.path(d.name, truth.file))
truth <- result
}
if(initNEUS) {
truth.file <- "outputneusDynEffort_Test1_run_truth.RData"
load(file.path(d.name, truth.file))
truth <- result
}
if(initNOBA){
truth.file <- "outputnordic_runresults_01run_truth.RData"
load(file.path(d.name, truth.file))
truth <- result
}
```
## Simulate a survey part 5: weight at st(age) census
Full methods are explained [here](https://sgaichas.github.io/poseidon-dev/StdSurvLengthCompTest.html).
We will apply examples here to just a few NOBA species to examine within-year variability:
- Cod "North_atl_cod", likely a test assessment species
- Herring "Norwegian_ssh", likely a test assessment species
- Greenland halibut "Green_halibut", which grows to a large size.
To create a census, the user specifies the timing of the survey, which species are captured, the spatial coverage of the survey, the species-specific survey efficiency ("q"), and the selectivity at age for each species. The following settings should achieve a survey that samples all Atlantis model output timesteps, all fish and shark species, and all model polygons, with perfect efficiency and full selectivity for all ages.
```{r census-spec, message=FALSE, warning=FALSE}
# should return a perfectly scaled survey
effic1 <- data.frame(species=funct.group.names,
efficiency=rep(1.0,length(funct.group.names)))
# should return all lengths fully sampled (Atlantis output is 10 age groups per spp)
selex1 <- data.frame(species=rep(funct.group.names, each=10),
agecl=rep(c(1:10),length(funct.group.names)),
selex=rep(1.0,length(funct.group.names)*10))
# should return all model areas
boxpars <- load_box(d.name, box.file)
boxall <- c(0:(boxpars$nbox - 1))
# generalized timesteps all models
runpar <- load_runprm(d.name, run.prm.file)
noutsteps <- runpar$tstop/runpar$outputstep
stepperyr <- if(runpar$outputstepunit=="days") 365/runpar$toutinc
timeall <- c(0:noutsteps)
# define set of species we expect surveys to sample (e.g. fish only? vertebrates?)
# for ecosystem indicator work test all species, e.g.
survspp <- funct.group.names
# for length and age groups lets just do fish and sharks
# NOBA model has InvertType, changed to GroupType in file, but check Atlantis default
if(initNOBA) funct.groups <- rename(funct.groups, GroupType = InvertType)
survspp <- funct.groups$Name[funct.groups$IsTurnedOn==1 &
funct.groups$GroupType %in% c("FISH", "SHARK")]
```
Here we use `create_survey` on the numbers output of `run_truth` to create the survey census of age composition (for just one species in this case). The `sample_fish` applies the median for aggregation and does not apply multinomial sampling if `sample=FALSE` in the function call.
Because we don't want to wait 24 hours for this, we will look at only the first 112 time steps.
```{r stdsurveyNbased-3spp, echo=TRUE}
spp.name <- funct.group.names[funct.group.names %in% c("North_atl_cod",
"Norwegian_ssh",
"Green_halibut")]
# get survey nums with full (no) selectivity
survey_testNall <- create_survey(dat = truth$nums,
time = c(0:111),
species = spp.name,
boxes = boxall,
effic = effic1,
selex = selex1)
# this one is high but not equal to total for numerous groups
effNhigh <- data.frame(species=survspp, effN=rep(1e+8, length(survspp)))
# apply default sample fish as before to get numbers
numsallhigh <- sample_fish(survey_testNall, effNhigh)
# aggregate true resn per survey design
aggresnall <- aggregateDensityData(dat = truth$resn,
time = c(0:111),
species = spp.name,
boxes = boxall)
# aggregate true structn per survey design
aggstructnall <- aggregateDensityData(dat = truth$structn,
time = c(0:111),
species = spp.name,
boxes = boxall)
#dont sample these, just aggregate them using median
structnall <- sample_fish(aggstructnall, effNhigh, sample = FALSE)
resnall <- sample_fish(aggresnall, effNhigh, sample = FALSE)
```
Length sample with user specified max length bin (200 cm):
```{r userset-maxlen, echo=TRUE}
length_census <- calc_age2length(structn = structnall,
resn = resnall,
nums = numsallhigh,
biolprm = truth$biolprm, fgs = truth$fgs,
maxbin = 200,
CVlenage = 0.1, remove.zeroes=TRUE)
```
We should get the upper end of Greenland halibut with a 200cm max length bin. That shouldnt matter for weight at age. This is all timesteps for each species by agecl.
```{r vis-wtageclass-test}
wageplot <- ggplot(length_census$muweight, aes(agecl, atoutput)) +
geom_point(aes(colour = time)) +
theme_tufte() +
theme(legend.position = "bottom") +
scale_x_discrete(limits=c(1:10)) +
xlab("age class") +
ylab("average individual weight (g)") +
labs(subtitle = paste(scenario.name))
wageplot + facet_wrap(c("species"), scales="free_y")
```
How much does weight at age class vary over time? Here are a couple of annual cycles:
```{r vis-wtageclass-test2}
wtageyr1 <- length_census$muweight %>%
filter(time<5)
wageplot <- ggplot(wtageyr1, aes(agecl, atoutput)) +
geom_line(aes(colour = factor(time))) +
theme_tufte() +
theme(legend.position = "bottom") +
scale_x_discrete(limits=c(1:10)) +
xlab("age class") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " initial year"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
```{r vis-wtageclass-test3}
wtageyrmid <- length_census$muweight %>%
filter(between(time, 50,54))
wageplot <- ggplot(wtageyrmid, aes(agecl, atoutput)) +
geom_line(aes(colour = factor(time))) +
theme_tufte() +
theme(legend.position = "bottom") +
scale_x_discrete(limits=c(1:10)) +
xlab("age class") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " year 10"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
```{r vis-wtageclass-test4}
wtageyr20 <- length_census$muweight %>%
filter(between(time, 100,104))
wageplot <- ggplot(wtageyr20, aes(agecl, atoutput)) +
geom_line(aes(colour = factor(time))) +
theme_tufte() +
theme(legend.position = "bottom") +
scale_x_discrete(limits=c(1:10)) +
xlab("age class") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " year 20"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
There are some interesting jumps between age classes. Most pronounced for herring. All of these species would be split into two true ages per age class. Interpolation could be interesting.
Change in wt at age over time for age classes using an annual mid-year snapshot (survey) (first 22+ years of NOBA model run):
```{r aggwtcomp}
# from std survey code and model step info read in above
midptyr <- round(median(seq(1,stepperyr)))
annualmidyear <- seq(midptyr, noutsteps, stepperyr)
wtage_annsurv <- length_census$muweight %>%
filter(time %in% annualmidyear)
# reverse to show agecl time series of wt
wageplot <- ggplot(wtage_annsurv, aes(time, atoutput)) +
geom_line(aes(colour = factor(agecl))) +
theme_tufte() +
theme(legend.position = "bottom") +
xlab("model timestep (5 per year)") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " annual mid year survey"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
## Quick test with saved CCA census output
Here we check the CCA model (Atlantis Summit Common Scenario 1) saved length and weight census output for small species at Isaac's suggestion (all three of the following are true age classes, NumAgeClassSize=1 per Atlantis agecl bin:
```{r loadCCAlengthcomp, echo=TRUE}
source(here("config/CCConfig.R"))
length_censussurvsamp <- readRDS(file.path(d.name, paste0(scenario.name, "length_censussurvsamp.rds")))
```
Sardine:
```{r plotCCAsardine}
censuswt <- length_censussurvsamp$muweight %>%
filter(species == "Pacific_sardine")
wageplot <- ggplot(censuswt, aes(time, atoutput)) +
geom_line(aes(colour = factor(agecl))) +
theme_tufte() +
theme(legend.position = "bottom") +
xlab("model timestep (1 per year)") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " annual mid year survey"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
Anchovy:
```{r plotCCAanchovy}
censuswt <- length_censussurvsamp$muweight %>%
filter(species == "Anchovy")
wageplot <- ggplot(censuswt, aes(time, atoutput)) +
geom_line(aes(colour = factor(agecl))) +
theme_tufte() +
theme(legend.position = "bottom") +
xlab("model timestep (1 per year)") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " annual mid year survey"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
Herring:
```{r plotCCAherring}
censuswt <- length_censussurvsamp$muweight %>%
filter(species == "Herring")
wageplot <- ggplot(censuswt, aes(time, atoutput)) +
geom_line(aes(colour = factor(agecl))) +
theme_tufte() +
theme(legend.position = "bottom") +
xlab("model timestep (1 per year)") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " annual mid year survey"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
How do these three look? Herring had a rough period from model years 15-30 or so.
Now for some CCA groups with multiple true ages per stage.
Pacific hake, 2 true ages per class:
```{r plotCCAhake}
censuswt <- length_censussurvsamp$muweight %>%
filter(species == "Mesopel_M_Fish")
wageplot <- ggplot(censuswt, aes(time, atoutput)) +
geom_line(aes(colour = factor(agecl))) +
theme_tufte() +
theme(legend.position = "bottom") +
xlab("model timestep (1 per year)") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " annual mid year survey"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
Bocaccio rockfish, 5 true ages per age class:
```{r plotCCAbocaccio}
censuswt <- length_censussurvsamp$muweight %>%
filter(species == "Bocaccio_rockfish")
wageplot <- ggplot(censuswt, aes(time, atoutput)) +
geom_line(aes(colour = factor(agecl))) +
theme_tufte() +
theme(legend.position = "bottom") +
xlab("model timestep (1 per year)") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " annual mid year survey"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
Yelloweye rockfish, most extreme at 12 true ages per age class:
```{r plotCCAYelloweye}
censuswt <- length_censussurvsamp$muweight %>%
filter(species == "Yelloweye_rockfish")
wageplot <- ggplot(censuswt, aes(time, atoutput)) +
geom_line(aes(colour = factor(agecl))) +
theme_tufte() +
theme(legend.position = "bottom") +
xlab("model timestep (1 per year)") +
ylab("average individual weight (g)") +
labs(subtitle = paste0(scenario.name, " annual mid year survey"))
wageplot + facet_wrap(c("species"), scales="free_y")
```
I'm not sure any of this helps us decide how to interpolate, but there is clearly contrast in weight at age (class) in both models and example runs.