---
title: "STAT 3340 Assignment 5, Fall 2025 - due Sunday,  December 7 at 11:59 PM"
author: "Your name here"
date: 'Banner:  B00??????'
output:
  pdf_document: default
  html_document: default
  word_document: default
---
=================================================================================================================


1. The data set ``fish'' has data on fish lengths, age and
  water temperature.   

The following reads the data,  centres the age variable by
subtracting its mean, and calculates the square of the centred age
variable

```{r}
fish=read.csv("http://chase.mathstat.dal.ca/~bsmith/stat3340/Data/fish.csv",header=T)
age=fish$age
age=age-mean(age)
age2=age^2
length=fish$length
temp=fish$temp
```

The following fits the linear model $length = \beta_0+\beta_1 temp  +\epsilon$
and displays the summary output.
```{r}
lm1=lm(length~temp)
summary(lm1)
anova(lm1)
```

Following are a plot of residuals vs fitted values, and a normal probability
plot of the residuals.  
```{r}
fit1=fitted(lm1)
e1=residuals(lm1)
plot(fit1,e1,xlab="fitted values",ylab="residuals")
qqnorm(e1) 
qqline(e1)
```

   + 1a) Comment briefly on the plots.  Do one or more of the assumptions of the linear model appear
to be violated?  Which one(s)?

   + 1b)  Following is an added variable plot which helps to decide whether age should be added to the model, 
and to determine the functional form of age to use - eg. linear, quadratic, cubic $\ldots$  The points on the plot  are coloured according to the value of temp.

```{r}
lm2=lm(age~temp)
summary(lm2)
anova(lm2)
e2=residuals(lm2)
plot(e2,e1,col=temp)
```

Which functional form of age seems more appropriate, a linear or a quadratic term?  

2.   In class we talked about how we can consider regression of $y$ on $X_1$ and $X_2$ to
be the result of three regressions. In this question we apply this approach where $y$ is
length, 
   + 2a)
    $lm1$ contains the result of regressing length on temp, with the residuals stored in e1.
   + 2b)  $lm2$ contains the result of regressing age on temp, with the residuals stored in e2.

   + 2c) Regress the residuals $e1$ on the residuals $e2$. Do not include an intercept. Use
the formula $lm(e1 \sim e2-1)$. Print the $summary$ and $anova$ outputs.
```{r}
#lm3=lm( ...
#summary(lm3)
#anova(lm3)
```

   + 2d) Fit the model including $age$ and $temperature$, and show the $summary$ and $anova$
outputs.
```{r}
#lmfull=lm( ...
#summary(lmfull)
#anova(lmfull)
```

   + 2e)  Show that the coefficient of $age$ in $lmfull$ is 
is the same as that in the regression of 
$e1$ on $e2$.  Ans:  The coefficient of $age$ equals ??? in both cases.
   + 2f) Use {Step 3} in the notes  to show that the intercept and the coefficient of $temp$ in 
the $lmfull$ fit are the same as those reconstructed from the three stage regression process.  
(This is what we did in class with the tree data.  That is, substitute for $e_1$ and $e_2$ in the equation
$e_1 = \alpha  e_2$, where $\alpha$ is the coefficient from the 3rd regression.
Isolate length on the left hand side, and calculate the regression coefficients on the
right hand side.)
   + 2g) Show that the residual sum of squares from the third regression equals that of the $lm$
fit to the full model.  Ans:  The error SS equals ??? in both cases.
   + 2h) Show that $SSR(\beta_2| \beta_1)$, 
the extra regression sum of squares explained by
$age$ is the same in the third regression as in the $anova$ output for the full model.
Ans:  the regression sum of squares is ??? in both cases.


3.  It is apparent from the added variable plot in 1b that a nonlinear term in age should be added.

   + 3a) The following fits the model $y = \beta_0 + \beta_1 temp + \beta_2 age + \beta_3 age^2 + e$, evaluates the fitted values 
and the residuals, plots residuals (on y axis) vs fitted values (on x axis), and shows a normal
QQ plot of the residuals.  \bf Comment on the plots, and in particular, whether any of the assumptions
of the regression analysis appear to be violated.

```{r}
# enter your work here
lmbig=lm(length~temp+age+age2)
summary(lmbig)
anova(lmbig)
fbig=fitted(lmbig)
rbig=residuals(lmbig)
qqnorm(rbig); qqline(rbig)
plot(fbig,rbig)
```


   
   + 3b) now do the same for the model 
   
   $$y = \beta_0 + \beta_1 temp + \beta_2 age + \beta_3 age^2 + \beta_4 temp \times age +
   \beta_5 temp \times age^2 + e$$

 which includes the interaction of age and temperature, and the interaction of $age^2$ and temperature.
 That is using the R code "lm(length~ temp+age+age2+temp:age + temp:age2)".
 
 + 3c)  Test the hypothesis $H_0: \beta_4 = \beta_5 = 0$.  Report the observed value of $F$, the numerator and denominator degrees of freedom, and the p-value.
 
 

