---
title: "Overdisp"
author: ""
date: "26/10/2022"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=TRUE}
knitr::opts_chunk$set(echo = TRUE)
library(faraway)
library(SMPracticals)
data(troutegg)
```

```{r}
ftable(xtabs(cbind(survive,total) ~ location + period, data = troutegg))

bmod <- glm(cbind(survive,total-survive) ~ 
              location + period, 
            family = binomial, 
            data = troutegg)

summary(bmod)
plot.glm.diag(bmod)
# plot(bmod)

bmod2 <- glm(cbind(survive,total-survive) ~ 
               location + period,
             family = quasibinomial,
             data = troutegg)

summary(bmod2)
plot.glm.diag(bmod2) ## see SM Section 10.2.3
```
The summary tables from either model are not that helpful. We can get an idea of the relative importance of location and period using analysis of the deviance. I am following the text (2nd edition p.59).
```{r analysis of deviance}
phihat <- sum(residuals(bmod, type="pearson")^2/12) #12 is the residual degrees of freedom
drop1(bmod, scale=phihat, test = "F")  #F-test
```
Finally the plots of the \textcolor{blue}{empirical logits} can be helpful to see what's going on.
```{r plot}
elogits <- with(troutegg, log((survive+0.5)/(total-survive+0.5)))
with(troutegg, interaction.plot(period,location,elogits))
```
Location 5 appears to have lower survival rates than the other 4 locations, and survival rates decrease somewhat over time.              