---
title: " HW Question Week 9 "
author: "STA2101F 2021"
date: ''
output:
  pdf_document: default
  html_document: default
fontsize: 12pt
---


```{r setup, include=FALSE}
# knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
  error = FALSE, 
  out.width = "100%",
  fig.width = 10,
  fig.height = 6.7, 
  fig.retina = 3,
  cache = FALSE)
```
```{r library}
library(faraway)
library(MASS)
data(wavesolder)
```

## Due November 25 2021 11.59 pm

**Homework to be submitted through Quercus**

*Adapted from ELM Ex. 7.3 (1st edition, Ex.9.3 2nd edition)* 

The `wavesolder` data in the library `faraway` is from a factorial experiment to assesss the effect of several factors, each at 2 levels, on the number of defects in manufacture electronic circuit cards. 

  (a) Fit a poisson and then a quasi-poisson model for the total number of defects, using all the explanatory variables. Which fit do you prefer? Why? 
  (b) Can some of the explanatory variables be omitted, in the poisson and quasi-poisson models? Explain. 
  (c) A Gamma `glm` is often used to model positive random variables. The model is parameterized by its expected value and shape: $$
  f(y;\nu, \mu) = \frac{1}{\Gamma(\nu)} \left(\frac{\nu}{\mu}\right)^\nu y^{\nu-1}\exp(-\frac{y\nu}{\mu}).$$ 
  Verify that this density has expected value $\mu$ and variance $\mu^2/\nu$. What is the canonical link function? 
  (e) Because we have three replicates we can investigate the effect of the explanatory variables on the variance of the response. Faraway suggests using the log link, because  variances are always positive, but this model fails for this data (try it). 
Fit a linear model to the log of the variances, and see whether or not some predictors can be omitted. Then see if the Gamma model with log link can be used in the reduced model. 
  (e) The delta-method (see below) for approximating the variance of a nonlinear function gives the result: $$\text{var}\{g(\bar X)\} \doteq \text{var}(\bar X)[g'\{E(\bar X)\}^2]. $$ Show that this result gives $\text{var}\{\log(s^2)\}  \doteq 2/(n-1)$, where $s^2 = \sum(Y_i - \bar Y)^2/(n-1)$.  
  
  (f) *PhD/Bonus (The delta-method)* 
   Suppose $X_1, \dots, X_n$ are independently and identically distributed, with $E(X_i)=\mu$ and ${\rm var}(X_i) = \sigma_X^2$.  Let $\bar X = n^{-1}\sum X_i$.  Show by Taylor series expansion that for $Z=g(\bar X)$:
\begin{eqnarray*}
 E(Z) &\doteq & g(\mu) , \\
{\rm var}(Z) &\doteq & \frac{\sigma_X^2}{n}\{g'(\mu)\}^2.
\end{eqnarray*}  
*Bonus-Bonus*: Show that $E(Z) = g(\mu) + O(1/n)$ and ${\rm var}(Z) = g'(\mu)^2/n + O(1/n^2)$.


     
