
---
title: "salmonMSE conditioning model"
date: "`r Sys.Date()`"
output:
  html_document:
    highlight: rstudio
    df_print: paged
---

<style type="text/css">
h1 { /* Header 1 */
  font-size: 24px;
}
</style>

```{r setup, include = FALSE, echo = FALSE}
library(ggplot2)
library(salmonMSE)
knitr::opts_chunk$set(
  collapse = TRUE, echo = FALSE, message = FALSE,
  fig.width = 6, fig.height = 4.5, out.width = "650px", comment = "#>"
)
theme_set(theme_bw())
```

# NAME {.tabset}

## Summary

```{r}
data.frame(
  Parameter = c("Historical years", "Age classes", "MCMC iterations"),
  Value = c(d$Ldyr, d$Nages, length(report))
)
```

```{r}
df_pars <- rstan::summary(stanfit)$summary %>%
  round(3) %>%
  as.data.frame()
df_pars[, c("mean", "sd", "2.5%", "50%", "97.5%", "n_eff", "Rhat")]
```

### Legend

```{r par-key, results="asis"}
knitr::kable(make_CM_table(fit), row.names = FALSE)
```

## MCMC

```{r trace-cr, fig.cap="Wormplots of productivity parameters, scaling parameters and the log-posterior from MCMC iterations after thinning and warmup."}
pars_core <- c("cr", "log_so", "moadd", "log_FbasePT", "log_FbaseT", "lp")
stan_trace(stanfit, pars_core)
```

```{r trace-cov, fig.cap="Wormplots of coefficients for mortality covariates from MCMC iterations after thinning and warmup."}
pars_cov <- c("^b")
stan_trace(stanfit, pars_cov)
```

```{r trace-vul, fig.cap="Wormplots of fishery vulnerability at age parameters from MCMC iterations after thinning and warmup."}
pars_vul <- c("^logit_vul")
stan_trace(stanfit, pars_cov)
```

```{r trace-var, fig.cap="Wormplots of variance parameters from MCMC iterations after thinning and warmup."}
pars_var <- c("lnE_sd", "wt_sd", "wto_sd", "fanomalyPT_sd", "fanomalyT_sd", "sd_matt")
stan_trace(stanfit, pars_var)
```

```{r pairs-cr, fig.cap="Marginal posterior distributions and correlations of productivity parameters, scaling parameters and the log-posterior (from MCMC iterations after thinning and warmup)."}
pairs_panel(stanfit, pars_core)
```

```{r pairs-cov, fig.cap="Marginal posterior distributions and correlations of coefficients for mortality covariates."}
pairs_panel(stanfit, pars_cov)
```

```{r pairs-vul, fig.cap="Marginal posterior distributions and correlations for fishery vulnerability at age parameters."}
pairs_panel(stanfit, pars_vul)
```

```{r pairs-var, fig.cap="Marginal posterior distributions and correlations of variance parameters."}
pairs_panel(stanfit, pars_var)
```

```{r wt, fig.cap="Posterior estimates of egg mortality deviates from the stock-recruit relationship (medians and 95% prediction interval)."}
CM_wt(stanfit, year1)
```

```{r wto, fig.cap="Posterior estimates of age 1 mortality deviates (medians and 95% prediction interval)."}
CM_wto(stanfit, year1)
```

## Data and fits {.tabset}

### Hatchery and escapement

```{r hatch-rel, fig.cap="Total hatchery releases."}
CM_data(d$hatchrelease, c(year, max(year) + 1), ylab = "Hatchery release")
```

```{r fit-esc, fig.cap="Observed total escapement (points with dotted lines) and posterior predicted values (medians and 95% prediction interval)."}
CM_fit_esc(report, d, year)
```

```{r cov1, fig.cap="Linear covariates to natural mortality in age 1."}
CM_covariate(d[["covariate1"]], cov1_names, year1, ylab = "M covariates (age 1)")
```

```{r cov, fig.cap="Linear covariates to natural mortality in ages 2+."}
CM_covariate(d[["covariate"]], cov_names, year1, ylab = "M covariates (age 2+)")
```

### CWT

```{r cwt-rel, fig.cap="Total CWT releases."}
CM_CWTrel(d$cwtrelease, year1, rs_names)
```

```{r fit-cwt-esc, fig.cap="Observed coded wire tag escapement at age (points with dotted lines) and posterior predicted values (medians and 95% prediction interval)."}
CM_fit_CWTesc(report, d, year1, rs_names)
```

```{r fit-cwt-catpt, fig.cap="Observed coded wire tag preterminal catch at age (points with dotted lines) and posterior predicted values (medians and 95% prediction interval)."}
CM_fit_CWTcatch(report, d, PT = TRUE, year1, rs_names)
```

```{r fit-cwt-catt, fig.cap="Observed coded wire tag terminal catch at age (points with dotted lines) and posterior predicted values (medians and 95% prediction interval)."}
CM_fit_CWTcatch(report, d, PT = FALSE, year1, rs_names)
```

```{r mat-cwt-rs, fig.cap="Brood year maturity by release strategy."}
if (d$n_r > 1) {
  CM_maturity(report, d, year1, rs_names = rs_names, annual = TRUE)
}
```

## State variables {.tabset}

### Spawners/Broodtake

```{r Spawners, fig.cap="Number of spawners by origin (medians and 95% prediction interval)."}
CM_ts_origin(report, year1, var = "Spawners")
```

```{r pHOS, fig.cap="Proportion hatchery origin spawners (medians and 95% prediction interval)."}
.CM_ts(report, year1, var = "pHOScensus", ci = TRUE, ylab = "pHOScensus")
```

```{r pNOB, fig.cap="Proportion natural origin broodtake, assuming non-selective broodtake (medians and 95% prediction interval)."}
.CM_ts(report, year1, var = "pNOB", ci = TRUE, ylab = "pNOB")
```

```{r PNI, fig.cap="Proportionate natural influence."}
report <- lapply(report, function(i) {
  i$PNI <- i$pNOB/(i$pNOB + i$pHOSeff)
  return(i)
})
.CM_ts(report, year1, var = "PNI", ci = TRUE, ylab = "PNI")
```

### Egg production

```{r SRR, fig.cap="Estimated stock-recruit relationship (medians and 95% prediction interval). Points show annual smolt production (medians)."}
CM_SRR(report, year1)
```

```{r egg, fig.cap="Egg production (medians and 95% prediction interval)."}
.CM_ts(report, year1, var = "egg", ci = TRUE, ylab = "Egg production")
```

```{r smolt, fig.cap="Natural smolt production (medians and 95% prediction interval)."}
report <- lapply(report, function(i) {
  i$Smolt <- rowSums(i$N[, 1, ])
  return(i)
})
.CM_ts(report, year1, var = "Smolt", ci = TRUE, ylab = "Smolt production")
```

```{r Megg, fig.cap="Estimated egg-smolt instantaneous mortality rate (medians and 95% prediction interval)."}
CM_Megg(report, year1, ci = TRUE, surv = FALSE)
```

```{r Meggsurv, fig.cap="Estimated egg-smolt survival (medians and 95% prediction interval)."}
CM_Megg(report, year1, ci = TRUE, surv = TRUE) +
  coord_cartesian(ylim = c(0, 1))
```

### Juvenile abundance

```{r Njuv, fig.cap="Juvenile abundance at age (medians and 95% prediction interval). Natural origin age 1 fish corresponds to smolt production."}
CM_Njuv(report, year1, ci = FALSE)
```

```{r M, fig.cap="Juvenile instantaneous natural mortality at age (medians and 95% prediction interval)."}
CM_M(report, year1)
```

```{r Msurv, fig.cap="Juvenile natural survival at age (medians and 95% prediction interval)."}
CM_surv(report, year1) +
  coord_cartesian(ylim = c(0, 1))
```

```{r Mcov1, fig.cap="Contribution to natural mortality in age 1 from covariates."}
CM_covariate(d[["covariate1"]], cov1_names, year1, b = rstan::extract(stanfit, "b1")[["b1"]], ylab = "Natural mortality (age 1)")
```

```{r Mcov, fig.cap="Contribution to natural mortality in age 1 from covariates."}
CM_covariate(d[["covariate"]], cov_names, year1, b = rstan::extract(stanfit, "b")[["b"]], ylab = "Natural mortality (age 2+)")
```

### Preterminal fishery

```{r vulPT, fig.cap="Posterior estimates of preterminal vulnerability at age (medians and 95% prediction interval)."}
CM_vul(report, type = "vulPT")
```

```{r FPT, fig.cap="Preterminal instantaneous fishing mortality (medians and 95% prediction interval)."}
CM_F(report, PT = TRUE, year1)
```

```{r CYER-PT, fig.cap="Return year preterminal exploitation rate at age (medians and 95% prediction interval)."}
CM_ER(report, brood = FALSE, type = "PT", year1, r = d$r_matt)
```

```{r BYER-PT, fig.cap="Brood year preterminal exploitation rate at age (medians and 95% prediction interval)."}
CM_ER(report, brood = TRUE, type = "PT", year1, r = d$r_matt)
```

### Return

```{r matt-RY, fig.cap="Posterior estimates of maturity by age and return year (medians and 95% prediction interval). Dashed, horizontal lines represent prior means."}
CM_maturity(report, d, year1, r = d$r_matt, brood = FALSE, rs_names = rs_names)
```

```{r matt-BY, fig.cap="Posterior estimates of maturity by age and brood year (medians and 95% prediction interval). Dashed, horizontal lines represent prior means."}
CM_maturity(report, d, year1, r = d$r_matt, brood = TRUE, rs_names = rs_names)
```

```{r recr, fig.cap="Recruitment at age (medians and 95% prediction interval)."}
CM_recr(report, year1, ci = TRUE)
```

### Terminal fishery

```{r vulT, fig.cap="Posterior estimates of terminal vulnerability at age (medians and 95% prediction interval)."}
CM_vul(report, type = "vulT")
```

```{r FT, fig.cap="Terminal instantaneous fishing mortality (medians and 95% prediction interval)."}
CM_F(report, PT = FALSE, year1)
```

```{r CYER-T, fig.cap="Return year terminal exploitation rate at age (medians and 95% prediction interval)."}
CM_ER(report, brood = FALSE, type = "T", year1, r = d$r_matt)
```

```{r BYER-T, fig.cap="Brood year terminal exploitation rate at age (medians and 95% prediction interval)."}
CM_ER(report, brood = TRUE, type = "T", year1, r = d$r_matt)
```

### Escapement

```{r esc, fig.cap="Escapement at age (medians and 95% prediction interval)."}
CM_esc(report, year1)
```

## About

This report was generated on: `r Sys.time()`<br />
salmonMSE version `r packageVersion("salmonMSE")`<br />
`r R.version.string`
