-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.3_caa_region.Rmd
175 lines (147 loc) · 6.33 KB
/
3.3_caa_region.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
---
title: "Catch-at-age: by region"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_depth: 3
toc_float:
collapsed: FALSE
code_folding: show
number_sections: TRUE
---
# Catch-at-age: by region
# SET-UP
```{r markdown, echo=F}
library(knitr)
opts_chunk$set(echo = T, collapse = T, fig.align = "center", fig.width = 9, fig.height = 6)
options(width = 140)
```
## settings
```{r settings, message = F}
source('0.0_settings.R')
```
## load all data
```{r data, message = F}
load(paste0(dir.rdat, "catch.caa.Rdata"))
load(paste0(dir.rdat, "lf.caa.Rdata"))
load(paste0(dir.rdat, "bio.caa.Rdata"))
load(paste0(dir.rdat, "lw.mod.Rdata"))
lf.caa[,which(names(lf.caa) %in% names(lw.mod)[-c(1:2)])] <- NULL # remove model details
catch.caa <- catch.caa[catch.caa$year %in% unique(lf.caa$year),] # years for which bio/lf
```
# CATCH-AT-AGE BY REGION
From the tables of length-frequencies/bio samples by region it is clear that insufficient samples are available to cover all regions.
Only keep eNL, wNL, sGSL and SS. Only the sGSL has samples each year.
```{r caa regional, message = F, fig.height = 6,fig.width = 8}
catch.diffused.regR <- paste0(dir.rdat, "catch.diffused.reg.Rdata")
caa.regR <- paste0(dir.rdat, "caa.reg.Rdata")
these.regions <- c('SS','sGSL','wNL','eNL','sNL','nGSL')
if(!all(file.exists(c(catch.diffused.regR,caa.regR)))){
# step 1) get samples
catch.diffused.reg <- ldply(these.regions,function(x){
years <- my.year[my.year>=1976 & my.year %in% unique(lf.caa[lf.caa$region==x,'year'])]
lf.reg <- lf.caa[lf.caa$region==x & !is.na(lf.caa$region) & lf.caa$year %in% years,]
al.reg <- bio.caa[bio.caa$region==x & !is.na(bio.caa$region) & bio.caa$year %in% years,]
ca.reg <- catch.caa[catch.caa$region==x & !is.na(catch.caa$region) & catch.caa$year %in% years,]
catch.diffused <- get.samples(catch=ca.reg,
lf=lf.reg,
al=al.reg,
tresh.lf=2, # min samples for length-frequency
tresh.al=2, # min samples for age-length key
period.unit='quarter') # quarterly grouping instead of monthly
catch.diffused$age.0 <- NULL # remove age 0 ?? (total landings somewhat smaller)
catch.diffused <- catch.diffused[catch.diffused$length!=-1,] # remove instances where threshold not reached (CAA will only represent part of catch; see checks later)
catch.diffused <- catch.diffused[catch.diffused$option.lengthfreq<7,] # don't allow to take info neighboring years for length frequencies (but yes for age length)
return(catch.diffused)
})
save(catch.diffused.reg, file = catch.diffused.regR)
caa.reg <- ddply(catch.diffused.reg,c('region'),function(x){get.caa(x,plus=10)})
save(caa.reg, file = caa.regR)
}else{
load(caa.regR)
load(catch.diffused.regR)
}
```
## CHECK {.tabset}
### samples
```{r caa check samples, fig.height = 6,fig.width = 12}
check <- ddply(catch.diffused.reg,c('id','region'),summarise,
option.lengthfreq=unique(option.lengthfreq),
nsample.lengthfreq=unique(nsample.lengthfreq),
nfish.lengthfreq=sum(n.lf),
option.agelength=unique(option.agelength),
nsample.agelength=unique(nsample.agelength),
nfish.agekey=sum(n.agekey))
check <- reshape2::melt(check,id=c('id','region'))
ggplot(check,aes(x=value))+geom_histogram(bins=30)+facet_grid(region~variable,scale='free') # region and gear can only rarely be matched
```
### total
```{r caa check total, fig.height = 6,fig.width = 10}
tot <- ddply(catch.caa[catch.caa$region %in% these.regions,],c('year','region'),summarise,catch=sum(catch))
ggplot(caa.reg,aes(x=year))+
geom_bar(stat='identity',aes(y=caaw,fill=age))+
geom_line(data=tot,aes(y=catch),size=1)+
facet_wrap(~region,scale='free_y')+
scale_fill_viridis()
```
# PLOTS {.tabset}
## CAAN abs
```{r caan plot, message = F, fig.height = 6,fig.width = 12}
ggplot(caa.reg,aes(x=year,y=age,size=caan))+
geom_point(alpha=0.8)+ scale_size(range = c(1,10))+
facet_wrap(~region,ncol=2)+
scale_y_continuous(breaks=min(caa.reg$age):max(caa.reg$age))
```
## CAAN rel
```{r caan rel, message = F, fig.height = 6,fig.width = 12}
df <- caa.reg[!is.na(caa.reg$caan),]
df <- ddply(df,c('year','region'),mutate,caan.rel=caan/max(caan))
ggplot(df,aes(x=year,y=age,size=caan.rel))+
geom_point(alpha=0.8)+
scale_size(range = c(1,6))+
facet_wrap(~region,ncol=2)+
scale_y_continuous(breaks=min(df$age):max(df$age))
```
## CAAN arrow
```{r caan arrow, message = F, fig.height = 6,fig.width = 12}
df <- caa.reg[!is.na(caa.reg$caan),]
df <- ddply(df,c('year','region'),mutate,caan.rel=caan/max(caan))
# autodetermine cohorts
m <- acast(df[df$region=='sGSL',],age~year,value.var = 'caan.rel')
a <- cohort(m)
a <- a[a$strength>=quantile(a$strength, 0.85, na.rm=TRUE),] # 15% strongest years in the southern gulf
a$year <- a$year + min(df$age)
a$age <- min(df$age)
a$agemax <- max(df$age)
a$yearend <- a$year+max(df$age)-min(df$age)
a$label <- a$year-1
# just select some myself
a <- data.frame(year = c(1979, 1983, 1989, 2000), # four largest peaks according to assessment
age = c(5, 1, 1, 1),
agemax = c(10, 10, 10, 10),
yearend = c(1984,1992, 1998, 2009),
label = c(1978, 1982, 1988, 1999))
ggplot(df,aes(x=year,y=age))+
geom_point(alpha=0.8,aes(size=caan.rel))+
scale_size(range = c(1,7))+
facet_wrap(~region)+
geom_segment(data=a,aes(xend=yearend,yend=agemax),col='red',size=1)+
geom_text(data=a,aes(x=yearend,y=agemax,label=label),col='red',size=3,hjust=0,vjust=-1)+
coord_cartesian(xlim=range(df$year),clip='off')+
scale_y_continuous(breaks=min(caa.reg$age):max(caa.reg$age))
```
## CAAW
```{r caaw plot, message = F, fig.height = 6,fig.width = 16}
ggplot(caa.reg,aes(x=year,y=age,size=caaw))+
geom_point(alpha=0.8)+ scale_size(range = c(1,10))+
facet_wrap(~region)+
scale_y_continuous(breaks=min(caa.reg$age):max(caa.reg$age))
```
## WAA
```{r waa plot, message = F, fig.height = 6,fig.width = 16}
ggplot(caa.reg,aes(x=year,y=waa,group=age))+
geom_line(aes(color=age),size=1)+
facet_wrap(~region)+
scale_color_viridis()
```