---
title: "Model Diagnostics for FLa4a"
author:
  - "Iago MOSQUEIRA (WMR)"
  - "Henning WINKER (EC JRC)"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Model Diagnostics for FLa4a}
  %\VignetteEncoding{UTF-8}
bibliography: references.bib
tags: [FLa4a FLR diagnostics]
license: Creative Commons Attribution-ShareAlike 4.0 International
---

```{r, pkgs, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(collapse = T, comment = "#>")
```

**a4adiags** is an open-source R package that provides a number of extra diagnostics for a4a stock assessment models, complementary of those already availabel in *FLa4a*.

[@Carvalho_2020]

It makes use of the S4 classes and methods developed by the **FLR** project (<http://flr-project.org/R>, [@Kell_2007]), which simplify the development, testing and use of statistical and simulation models for fisheries management advice.

Diagnostics in FLa4a


# Example model fit

```{r fit}
library(a4adiags)
library(FLa4a)
data("sol274")

# fmodel, mimics AAP

fmod <- ~te(replace(age, age > 8, 8), year, k = c(4, 22)) +
  s(replace(age, age > 8, 8), k=4) +
  s(year, k=22, by=as.numeric(age==1))

# qmodel (GAM, SNS)

qmod <- list(~s(age, k=3), ~s(age, k=3))

# vmodel (catch, GAM, SNS)

vmod <- list(
  ~s(age, k=3),
  ~s(age, k=3),
  ~s(age, k=3))

# srmodel

srmod <- ~factor(year)

# fit

fit <- sca(stock, indices[c("BTS", "SNS")],
  srmodel=srmod, fmodel=fmod, qmodel=qmod, vmodel=vmod)
```

<!-- # Retrospective analysis -->

# Prediction skill by hindcasting cross-validation

```{r parallel, eval=FALSE}
library(doParallel)
ncores <- floor(detectCores() * .75)

if(Sys.info()["sysname"] %in% c("Darwin", "Linux")) {
  registerDoParallel(ncores)
} else if(Sys.info()["sysname"] %in% c("Windows")) {
  cl <- makeCluster(ncores)
  clusterEvalQ(cl = cl, expr = { library(FLa4a); library(FLasher) })
  registerDoParallel(cl)
}
```

```{r xval}
xval <- a4ahcxval(stock, indices, nyears=5, nsq=3, srmodel=srmod, fmodel=fmod,
  qmodel=qmod, vmodel=vmod)
```

```{r mase}
mase(indices, xval$indices[-1])
```

```{r plotxval}
plotXval(xval$indices)
plotXval(indices, xval$indices)
plotXval(indices, xval$indices[-1])
plotXval(xval$indices[["data"]], xval$indices[-1])
plotXval(xval$indices[["data"]], xval$indices)
```

```{r retro}
plot(xval$stocks)
mohnMatrix(xval$stocks, ssb)
icesAdvice::mohn(mohnMatrix(xval$stocks, ssb))
icesAdvice::mohn(mohnMatrix(xval$stocks, fbar))
```


[@Kell_2016]

[@Hyndman_2006]

# Runs test

```{r runstest}
plotRunstest(fit, indices)
```

```{r runstest_age}
plotRunstest(fit, indices, combine=FALSE)
```


# More information

* You can submit bug reports, questions or suggestions on `FLPKG` at the `FLPKG` issue page ^[<https://github.com/flr/a4adiagfs/issues>].
* Or send a pull request to <https://github.com/flr/a4adiags/>
* For more information on the FLR Project for Quantitative Fisheries Science in R, visit the FLR webpage ^[<http://flr-project.org>].
* The latest version of `a4diags` can always be installed using the `devtools` package, by calling
```{r, devtools, echo=TRUE, eval=FALSE}
	library(devtools)
	install_github('flr/a4adiags')
```

## Software Versions

* `r version$version.string`
* FLCore: `r packageVersion('FLCore')`
* FLa4a: `r packageVersion('FLa4a')`
* ggplotFL: `r packageVersion('ggplotFL')`
* a4adiags: `r packageVersion('a4adiags')`
* **Compiled**: `r date()`
* **Git Hash**: `r system("git log --pretty=format:'%h' -n 1", intern=TRUE)`

## Author information

**Iago MOSQUEIRA**. Wageningen Marine Research (WMR). Haringkade 1, Postbus 68. 1976CP, IJmuiden. The Netherlands

**Henning WINKER**. European Commission Joint Research Centre (EC JRC). Ispra, Italy.

# References
