-
Notifications
You must be signed in to change notification settings - Fork 10
/
length_based_methods_classification_skill.Rmd
364 lines (263 loc) · 10.3 KB
/
length_based_methods_classification_skill.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
---
title: "Classification Skill of Length Based Indicators"
output:
github_document:
mathjax: TRUE
html_document:
fig_width: 6
fig_height: 4
toc: yes
bibliography: bibliography.bib
---
```{r knitr_init, echo=FALSE, results="hide"}
library(knitr)
## Global options
opts_chunk$set(echo =FALSE,
eval =TRUE,
prompt =FALSE,
comment =NA,
message =FALSE,
warning =FALSE,
tidy =TRUE,
fig.height=6,
fig.width =8,
cache =TRUE,
cache.path="../cache/class/")
options(digits=3)
iFig=0
```
[Packages](#Packages)
[Operating Model Conditioning](#OM%5D)
[Indicators](#Indicators)
[Classification Skill](#Skill)
[More Information](#More)
[References](#References)
# Packages {#Packages}
As well as 'FLR' a variety of other packages are required
```{r, pkgs3, echo=TRUE}
library(FLCore)
library(ggplotFL)
library(FLBRP)
library(FLasher)
library(mydas)
library(FLRef)
```
for plotting
```{r, pkgs1, echo=TRUE}
library(ggplot2)
library(GGally)
library(ggpubr)
```
and data manipulation
```{r, pkgs2, echo=TRUE}
library(reshape)
library(plyr)
library(dplyr)
library(reshape)
```
## Quick Start {#QuickStart}
The packges **FLife** and **mydas** are required for modelling life-histories and estimation
The simplest way to obtain these to install them from the `FLR` repository
```{r install,echo=TRUE,eval=FALSE}
install.packages("FLife", repos = "http://flr-project.org/R")
```
See `help(install.packages)` for more details.
After installation
```{r lib,echo=TRUE}
library(FLife)
library(mydas)
```
The `popbio` package is also required for analysing age and stage based population models.
```{r, pkgs4, echo=TRUE}
library(popbio)
library(spatstat)
```
[Back to Top](#top)
# Operating Model Conditioning
```{r, param}
par=lhPar(FLPar(c(linf= 59.1, k=0.28, t0=-0.4,
a=0.01111,b=3.15,a50=4.0, l50=43.25,
s=0.7),units="NA"))
```
```{r, om}
eq=lhEql(par)
```
```{r, fig-eq}
plot(eq,refpt="msy")
```
```{fbar(eq)=refpts(eq)["msy","harvest"]%*%FLQuant(c(rep(0.1,60),}
seq(0.1,2,length.out=40)[-40],
seq(2,0.7,length.out=11),rep(0.7,20)))
set.seed(234)
srDev =rlnoise(100,fbar(eq)%=%0,0.3,0)
om=as(eq,"FLStock")
om=propagate(om,dim(srDev)[6])
f =propagate(fbar(eq),100)[,-1]
om=ffwd(om,fbar=f,sr=eq,deviances=srDev)
om=window(om,start=41)
```
```{r, fig-om, fig.width=10,fig.height=6}
mets <- list(RMSY =function(x, y) rec( x)/refpts(y)["msy","rec"],
YMSY =function(x, y) catch(x)/refpts(y)["msy","yield"],
SBMSY=function(x, y) ssb( x)/ sbmsy(y),
FMSY =function(x, y) fbar( x)/ fmsy(y))
fqs <- FLQuants(lapply(mets, function(m) m(om, eq)))
plot(fqs,iter=1)+
ylim(c(0, NA))+
geom_hline(yintercept=1, linetype=2)+
scale_x_continuous(limits=c(50,120))+
theme_bw(16)+
theme(legend.position="none")
```
**Figure `r iFig=iFig+1; iFig`** Operating Models.
```{r, lfd}
set.seed(6789)
ak =invAlk(iter(par,.id),cv=0.1)
lfd=lenSample(catch.n(om),ak,nsample=500)
```
```{r, fig-lfd, fig.width=10,fig.height=10}
dat=subset(transform(as.data.frame(lfd,drop=T),lustrum=5*(year%/%5)),year>=60)
dat=subset(dat,lustrum%in%seq(60,120,10))
lmodeFn<-function(len,n,bin=25) {
dat=data.frame(bin=cut(len,breaks=bin),n=n)
res=ddply(dat,.(bin), with, data.frame(freq=sum(n)))
res=as.character(subset(res,freq==max(freq))[1,"bin"])
mydas:::unbin(res)$mid}
dt2=ddply(dat,.(lustrum), with, {
lc=lmodeFn(len,data)*0.5
data.frame(lc =lc,
lmean=weighted.mean(len,data*(len>=lc)))})
gghistogram(dat,x="len",weight="data", bins=25)+
coord_flip()+
scale_x_reverse()+
facet_grid(.~lustrum,scale="free")+xlab("Length (cm)")+
geom_vline(aes(xintercept=lc), data=dt2,col="red")+
geom_vline(aes(xintercept=lmean), data=dt2,col="blue")+
theme_bw(16)+
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
```
**Figure `r iFig=iFig+1; iFig`** Length data
[Back to Top](#top)
# Indicators {#indicators}
To provide advice on the status of data poor stocks ICES uses $MSY$ proxy reference points as part of a Precautionary Approach.
Data poor stocks include those for which only trends such as lpue, cpue, and mean length in the catch are available (**Category 3**), and stocks for which only reliable catch data are available (**Category 4**).
Methods currently approved by ICES for calculation of $MSY$ reference points for these stocks are
- Length based indicators
- Z derived from mean length
- Length based spawner per recruit; and
- Surplus Production models
Many approaches have emerged over the last few decades, for example Where length data are available methods include Length Based Spawning Potential Ratio (LBSPR), Length-Based Integrated Mixed Effects (LIME), and Length-Based Bayesian (LBB). While where only catch data are available methods include Catch-Maximum Sustainable Yield (Catch-MSY), State-Space Catch-Only Model (SSCOM), Depletion Based Stock Reduction Analysis (DBSRA), and Simple Stock Synthesis (SSS) an extension of Catch-MSY (CMSY).
Empirical indicators and reference points can also be used to monitor stocks and these include
- $L_{max5\%}$ mean length of largest 5%
- $L_{95\%}$ $95^{th}$ percentile
- $P_{mega}$ Proportion of individuals above $L_{opt} + 10\%$
- $L_{25\%}$ $25^{th}$ percentile of length distribution
- $L_{c}$ Length at $50\%$ of modal abundance
- $L_{mean}$ Mean length of individuals $> L_c$
- $L_{max_{y}}$ Length class with maximum biomass in catch
- $L_{mean}$ Meanlength of individuals $> L$
where potential **reference points** include
- $L_{opt} = L_{\infty}\frac{3}{3+\frac{M}{K}}$, assuming $M/K = 1.5$ gives $\frac{2}{3}L_{\infty}$
- $L_{F=M} = 0,75l_c+0.25l_{\infty}$
```{r, lbi, eval=FALSE}
lbi =transform(subset(as.data.frame(lfd,drop=TRUE),data>0),
wt =c(par["a"])*len^c(par["b"]),
lopt=c(2/3*par["linf"]))
lbi =ddply(lbi, .(year,iter), with, mydas:::lenInd(len,data,wt,lopt))
lbi=cbind(lbi,linf=c(par["linf"]),k=c(par["k"]),l50=c(par["l50"]))
```
```{r, indicator}
source("~/pCloudDrive/flr/FLCandy/R/length.R")
state =fbar(om)%/%refpts(eq)["msy","harvest"]
indicator=lmean(lfd)
```
```{r, acc}
x =catch.n(om)[ac(par["sel1"]+0:7)]
smpl=as.FLQuant(adply(x, c(2,6), function(x) data.frame(age=names(x),data=apply(rmultinom(100,1,x),1,sum))))
z =ddply(subset(as.data.frame(smpl),data>0&!is.na(data)&is.finite(data)), .(year,iter), with,
data.frame(data=-coefficients(lm(log(data)~age,na.rm=T))[2]))
zhat=as.FLQuant(z)
```
```{r}
plot(mcf(FLQuants(state=state,indicator=indicator,Z=zhat)))
```
[Back to Top](#top)
# Classification Skill {#Skill}
```{r, tss, fig.height=8, fig.width=8}
dat=subset(model.frame(mcf(FLQuants(state=state,indicator=1/(indicator/45)))),year%in%100:120)
with(dat, {
PN=c(
TP=sum(indicator>=1&state>=1),
TN=sum(indicator< 1&state< 1),
FN=sum(indicator< 1&state>=1),
FP=sum(indicator>=1&state< 1))
ggplot(data.frame(state=state,indicator=indicator))+
geom_point(aes(state,indicator))+
geom_vline(aes(xintercept=1),col="red")+
geom_hline(aes(yintercept=1),col="red")+
xlab("F/FMSY")+ylab("LMean / ref year")+
geom_label(aes(x=1.8,y=1.2,label=paste("TP=",PN["TP"])))+
geom_label(aes(x=1.8,y=0.9,label=paste("FP=",PN["FN"])))+
geom_label(aes(x=0.8,y=1.2,label=paste("FN=",PN["FP"])))+
geom_label(aes(x=0.8,y=0.9,label=paste("TN=",PN["TN"])))
tss=PN["TP"]/(PN["TP"]+PN["FN"])-PN["FP"]/(PN["FP"]+PN["TN"])
names(tss)="TSS"
tss})
```
**Figure `r iFig=iFig+1; iFig`**
```{r, roc}
source("~/pCloudDrive/flr/FLCandy/R/roc.R")
dat=with(subset(model.frame(mcf(FLQuants(state=state,indicator=1/(indicator/45)))),year%in%100:120),roc(state,indicator))
dt2=with(subset(model.frame(mcf(FLQuants(state=state,indicator=(zhat-0.25)/0.25))),year%in%100:120),roc(state,indicator))
ggplot(dat)+
geom_line(aes(FPR,TPR))+
geom_point(aes(FPR,TPR),data=subset(dat,abs(indicator-1)==min(abs(indicator-1)))[1,],size=3)+
geom_abline(aes(intercept=0,slope=1),linetype=2)+
theme_bw(20)+
theme(legend.position="bottom")+
scale_x_continuous(breaks=c(0,0.5,1))+
scale_y_continuous(breaks=c(0,0.5,1))+
geom_line(aes(FPR,TPR),data=dt2,col="blue")+
geom_point(aes(FPR,TPR),data=subset(dt2,abs(indicator-1)==min(abs(indicator-1)))[1,],col="blue",size=3)
```
**Figure `r iFig=iFig+1; iFig`.** ROC for Catch Curve analysis with reference level indicated.
```{r}
ggplot(dat)+
geom_line(aes(indicator,TSS))+
geom_vline(aes(xintercept=1),col="red")
```
**Figure `r iFig=iFig+1; iFig`.** TSS by reference level for lmean.
```{r}
ggplot(dt2)+
geom_line(aes(indicator,TSS))+
geom_vline(aes(xintercept=1),col="red")
```
**Figure `r iFig=iFig+1; iFig`.** TSS by reference level for catch curve analysis.
[Back to Top](#top)
# More Information {#More}
- You can submit bug reports, questions or suggestions on `FLife` at the `FLife` issue page [^1], or on the *FLR* mailing list.
- Or send a pull request to <https://github.com/lauriekell/FLife/>
- For more information on the FLR Project for Quantitative Fisheries Science in R, visit the FLR webpage [^2].
- The latest version of `FLife` can always be installed using the `devtools` package, by calling
[^1]: <https://github.com/lauriekell/FLife/issues>
[^2]: <http://flr-project.org>
```{r, devtools, echo=TRUE, eval=FALSE}
library(devtools)
install_github("flr/FLife")
```
\`
## Software Versions
- `r version$version.string`
- FLCore: `r packageVersion('FLCore')`
- FLPKG: `r # packageVersion('FLPKG')`
- **Compiled**: `r date()`
- **Git Hash**: `r system("git log --pretty=format:'%h' -n 1", intern=TRUE)`
## Author information
**Laurence KELL**. [laurie\@seaplusplus.co.uk](mailto:[email protected]){.email}
## Acknowledgements
This vignette and the methods documented in it were developed under the MyDas project funded by the Irish exchequer and EMFF 2014-2020. The overall aim of MyDas is to develop and test a range of assessment models and methods to establish Maximum Sustainable Yield (MSY) reference points (or proxy MSY reference points) across the spectrum of data-limited stocks.
# References {#References}
[Back to Top](#top)