---
title: "Diagnosing errors"
author: "Eric J. Ward, Jason E. Jannot"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Diagnosing errors}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, results="hide", message=FALSE, warning=FALSE}
library(bycatch)
library(loo)
set.seed(123)
```

In some cases, models might produce warnings about high Pareto K statistics (> 0.7). These may reflect a model that is too flexible for the data being used, in which case the model might need to be changed. In some cases, 'moment matching' may be used to improve diagnostics; an example of this is below.

### Load data
```{r data}
# replace this with your own data frame
d = data.frame("Year"= 2002:2014, 
  "Takes" = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 0, 0),
  "expansionRate" = c(24, 22, 14, 32, 28, 25, 30,  7, 26, 21, 22, 23, 27),
  "Sets" = c(391, 340, 330, 660, 470, 500, 330, 287, 756, 673, 532, 351, 486))
```

## Simple model with constant bycatch, no covariates

This model has a constant bycatch rate and Poisson distribution,  

```{r, results="hide", message=FALSE, warning=FALSE}
fit = fit_bycatch(Takes ~ 1, data=d, time="Year", effort="Sets", family="poisson",
  time_varying = FALSE)
```

If the 'fit' object produced errors about the Pareto K statistics being high, we could use moment matching in the `loo` package,  

```{r}
loo_stats <- loo(fit$fitted_model, moment_match = TRUE)

print(loo_stats)
```





