---
title: "Exercise 8.14"
author: "Per August Jarval Moen"
date: "2024"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
In exercise 7.31 we used a Poisson GLM to investigate the relationship between race and the number of victims of homicide a person knows. We found that the Poisson GLM was overdispersed, and fitted a Negative Binomial model to account for the overdispersion. An alternative is to fit a Quasi-Poisson model which allows for overdispersion. Let the notation be as in exercise 7.31. When applying a QL method, we drop the distributional assumption, and assume only that
\begin{align*}
\mathbb{E}(Y_i) &= \mu_i = \exp(\beta_0 + \beta_1 x_i),
\intertext{and, }
\text{Var}(Y_i) &= \phi \mu_i,
\end{align*}
for some $\phi>0$. As described in the book and lectures, the QL estimates of $\beta_0, \beta_1$ will be the same as with the Poisson GLM. The only difference from the Poisson GLM is that estimated standard errors of $\hat{\beta}_0$ and $\hat{\beta}_1$ should be multiplied by $\sqrt{\hat{\phi}}$, where $\hat{\phi}$ is the generalized Pearson statistic (which estimates $\phi$).
\newline\newline
Let us fit the models and compare the point estimates and estimated standard errors.  
```{r }
library(MASS)
url = "http://users.stat.ufl.edu/~aa/glm/data/Homicides.dat"
data = read.table(url, header=TRUE)
data = data[,c("race", "count")] #removing observation number
poisson.model = glm(count~race, family=poisson(link="log"),data=data)
QL.model = glm(count  ~ race , family = quasi(link = "log", variance = "mu"), data = data)
negbin.model = glm.nb(count~race,link="log",data=data)
```
\leavevmode \newline
The point estimates from the Poisson GLM and QL method are:
```{r ,echo=FALSE}
summ = summary(poisson.model)
summ$coefficients[,"Estimate"]
```
The point estimates from the Negative Binomial model are:
```{r ,echo=FALSE}
summ2 = summary(negbin.model)
summ2$coefficients[,"Estimate"]
```
The estimated standard errors under the Poisson model are
```{r ,echo=FALSE}
summ$coefficients[,"Std. Error"]
```
The estimated standard errors from the Quasi-Poisson model are
```{r ,echo=FALSE}
summ3 = summary(QL.model)
summ3$coefficients[,"Std. Error"]
```
The estimated standard errors under the Negative Binomial model are
```{r ,echo=FALSE}
summ2$coefficients[,"Std. Error"]
```
We see that the estimated standard errors from the Negative Binomial model and the Quasi-Poisson model are quite similar, and both are larger than the estimated standard errors from the Poisson model.