-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.1_caa_comparison.Rmd
130 lines (112 loc) · 4.43 KB
/
3.1_caa_comparison.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
---
title: "Catch-at-age: comparison"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
toc_depth: 3
toc_float:
collapsed: FALSE
code_folding: show
number_sections: TRUE
---
# 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}
# New caa
load(paste0(dir.rdat, "caa.Rdata"))
# Old caa and waa
year <- 2018
repo <- "https://github.com/iml-assess/mackerel_assessment/blob/master/"
caa.old <- read.ices(url(paste0(repo,'data/',year,'/cn.dat',"?raw=true")))
waa.old <- read.ices(url(paste0(repo,'data/',year,'/cw.dat',"?raw=true")))
```
## Bindind and scaling
```{r wrangle, message = F}
caa.old <- reshape2::melt(as.matrix(caa.old),varnames=c('year','age'),value.name='caan.old')
waa.old <- reshape2::melt(as.matrix(waa.old),varnames=c('year','age'),value.name='waa.old')
caa.comp <- Reduce(function(x, y) merge(x, y, all.x=TRUE), list(caa, caa.old, waa.old))
caa.comp <- caa.comp[caa.comp$year %in% unique(caa.old$year),] # remove the recent years
caa.comp <- ddply(caa.comp,c('year'),mutate,caan.rel=caan/max(caan),caan.old.rel=caan.old/max(caan.old)) # compare proportions instead of absolutes
```
# CAA PLOTS {.tabset}
## Side-wise absolute
```{r plot 1}
grid.arrange(
ggplot(caa.comp,aes(x=year,y=age,size=caan))+geom_point(alpha=0.8)+ scale_size(range = c(1,8))+labs(title='NEW (absolute)'),
ggplot(caa.comp,aes(x=year,y=age,size=caan.old))+geom_point(alpha=0.8)+ scale_size(range = c(1,8))+labs(title='OLD (absolute)')
)
```
## Side-wise relative
```{r plot2}
grid.arrange(
ggplot(caa.comp,aes(x=year,y=age,size=caan.rel))+geom_point(alpha=0.8)+ scale_size(range = c(1,5))+labs(title='NEW (rel by year)'),
ggplot(caa.comp,aes(x=year,y=age,size=caan.old.rel))+geom_point(alpha=0.8)+ scale_size(range = c(1,5))+labs(title='OLD (rel by year)')
)
```
## Residual absolute
If the difference is more than 100% the dots/triangles are black
```{r plot 3}
caa.comp$res <- with(caa.comp,(caan-caan.old)/caan.old)
caa.comp$big <- ifelse(abs(caa.comp$res)>100,TRUE,FALSE)
caa.comp$dir <- ifelse(caa.comp$res<0,'-',ifelse(caa.comp$res==0,'0','+'))
ggplot()+
geom_point(data=caa.comp[caa.comp$big==FALSE,],aes(x=year,y=age,col=abs(res),shape=dir),size=4)+
geom_point(data=caa.comp[caa.comp$big==TRUE,],aes(x=year,y=age,shape=dir),col='black',size=4)+
labs(title='(NEW-OLD)/OLD')+
scale_color_viridis_c(direction = -1)
```
## Residual relative
```{r plot 4}
caa.comp$res <- with(caa.comp,caan.rel-caan.old.rel)
caa.comp$col <- ifelse(caa.comp$res<0,'-',ifelse(caa.comp$res==0,'0','+'))
ggplot(caa.comp,aes(x=year,y=age,size=abs(res),col=col))+
geom_point(alpha=0.8)+
scale_color_manual(values=c('darkred','darkgreen','black'))+
labs(title='NEW (rel) - OLD (rel)')
```
## Correlation
```{r plot 5}
corr <- ddply(caa.comp[!is.na(caa.comp$caan.rel) & !is.na(caa.comp$caan.old.rel),],c('age'),summarise,corr=cor(caan.rel,caan.old.rel))
ggplot(caa.comp,aes(x=caan.rel,y=caan.old.rel))+
geom_point()+
geom_text(aes(label=year),size=2,hjust=0,vjust=0)+
geom_text(data=corr,aes(x=-Inf,y=Inf,label=round(corr,2)),vjust=1,hjust=0,col='blue')+
geom_abline(slope=1,intercept=0)+
facet_wrap(~age,scale='free')
```
# WAA PLOTS{.tabset}
## side-wise
```{r plot 1b}
grid.arrange(
ggplot(caa.comp,aes(x=year,y=waa,group=age))+geom_line(aes(color=age),size=1)+scale_color_viridis()+labs(title='NEW'),
ggplot(caa.comp,aes(x=year,y=waa.old,group=age))+geom_line(aes(color=age),size=1)+scale_color_viridis()+labs(title='OLD')
)
```
## side-wise by age
```{r plot 2b}
df <- reshape2::melt(caa.comp[,c('year','age','waa','waa.old')],id=c('year','age'))
ggplot(df,aes(x=year,y=value,group=variable,col=variable))+
geom_line(size=1,alpha=0.6)+
facet_wrap(~age,scale='free')+
scale_color_manual(values=c('red','black'))
```
## Correlation
```{r plot 3b}
corr <- ddply(caa.comp[!is.na(caa.comp$waa.old) & !is.na(caa.comp$waa),],c('age'),summarise,corr=cor(waa,waa.old))
ggplot(caa.comp,aes(x=waa,y=waa.old))+
geom_point()+
geom_text(aes(label=year),size=2,hjust=0,vjust=0)+
geom_text(data=corr,aes(x=-Inf,y=Inf,label=round(corr,2)),vjust=1,hjust=0,col='blue')+
geom_abline(slope=1,intercept=0)+
facet_wrap(~age,scale='free')
```