---
title: "Case Study 14.2: Snapper counts in and around marine reserves"
output:
  rmarkdown::html_document:
    self_contained: true
---

```{r, include=FALSE}
## Do not delete this!
## It loads the s20x library for you. If you delete it 
## your document may not compile it.
require(s20x)

knitr::opts_chunk$set(
  dev = "png",
  fig.ext = "png",
  dpi = 96
)

```

# Problem
Baited underwater video (BUV) is an established tool for counting fish such as snapper.

BUV was used at two locations, Leigh and Hahei. Each location has a marine reserve. The BUV was deployed at sites inside the marine reserve, and at sites just outside the reserve. BUV was used at a total of 18 sites.

The variables of interest were:

* `Locn`: A two-level factor which describes the BUV's location.
    + It has the levels "Leigh" and "Hahei".
* `Reserve`: A two-level factor which describes whether the BUV's is in a marine reserve.
    + It has the levels "N" and "Y".
* `Freq`: The number of snapped counted by the BUV.

## Question of Interest
It was of interest to explore the relative count of snapper with regard to location and reservation status.

## Read in and Inspect the Data
```{r, echo = FALSE}
load(system.file("extdata", "Snap.df.rda", package = "s20x"))
```

```{r, out.width = "75%", fig.align = 'center', eval = FALSE}
Snap.df = read.table("SnapperCROPvsHAHEI.txt", header = TRUE)
interactionPlots(Freq ~ Locn * Reserve, data = Snap.df, col.width = 0)
```

```{r, out.width = "75%", fig.align = 'center', echo = FALSE}
interactionPlots(Freq ~ Locn * Reserve, data = Snap.df, col.width = 0)
```

In Leigh, it seems that there is a higher frequency of snapper counted in a marine reserve compared to a non-reserve area. In Hahei, it seems that there is little difference in the frequency of snapper counted in a marine reserve compared to a non-reserve area.
However, the interaction plot does indicate that the multiplicative effect of reserve 
could be similar at the two locations (i.e., no interaction).


## Model Building and Check Assumptions
```{r, out.width = "75%", fig.align = 'center'}
Snap.glm = glm(Freq ~ Locn * Reserve, family = poisson, data = Snap.df)
summary(Snap.glm)
1 - pchisq(14.678, 14)
anova(Snap.glm, test="Chisq")
Snap2.glm = glm(Freq ~ Locn + Reserve, family = poisson, data = Snap.df)
anova(Snap2.glm, test="Chisq")
summary(Snap2.glm)
plot(Snap2.glm, which = 1)
exp(confint(Snap2.glm))
```

```{r, echo = FALSE, message=FALSE}
conf1 = data.frame(exp(confint(Snap2.glm)))
names(conf1) <- c("lower", "upper")
resultStr1 = paste0(sprintf("%.1f", abs(conf1$lower)), " and ", sprintf("%.1f", abs(conf1$upper)))
```


## Methods and Assumption Checks
As the response variable, `Freq`, is a count, we have fitted a generalised linear model with a Poisson response distribution. We have two expanatory factors: `Locn` and `Reserve`. We initially fitted an interaction between reserve and location. The interaction term was not significant ($P$-value = 0.65) so the model was refitted with main effects only. Both factors were significant so were retained for the final model.

All assumptions were satisfied. There was no evidence of overdispersion, so we can trust the results from the Poisson model ($P$-value = 0.40).

For our final model, we assume that the snapper count for observation $i$ is Poisson with mean $\mu_i$, where 
$$\log(\mu_i) = \beta_0 + \beta_1 \times \text{Locn.Leigh}_i + \beta_2 \times \text{Reserve.Yes}_i ~,$$
and where $\text{Locn.Leigh}_i$ and $\text{Reserve.Yes}_i$ are dummy variables which take the value 1 if observation $i$ is respectively from Leigh and from a marine reserve, otherwise they are 0. 

## Executive Summary
It was of interest to explore the relative count of snapper with regard to location and reserve status.

We conclude that the expected count of snapper inside a marine reserve is between `r resultStr1[3]` times that outside of the reserve.

Additionally, the expected count of snapper in Leigh is between `r resultStr1[2]` times the expected count in Hahei.


