---
title: "Lab Sheet 2 Solution"
author: "Jennifer Chan"
date: "06/03/2020"
output: html_document
---


```{r setup, warning=F, message=F}
library(tidyverse)
library(broom)
library(patchwork)
knitr::opts_chunk$set(echo = TRUE, tidy = FALSE, fig.align = 'center', fig.pos = 'H')
```

## Question 1

### (a)
```{r}
olympic <- read.table("data/olympic.txt", sep = "\t", header = TRUE)
```

### (b)
```{r}
tail(olympic)
```

There are two unusual features in the dataset: the last three entries of HighJump are missing, and Year is relative to 1900 rather than the actual year.

### (c)

```{r}
olympicMetric <- olympic %>% 
  mutate_at(.vars = vars(-Year), 
            .funs = funs(./39.3701)) %>% # converts all inches to metres
  mutate(Year = Year + 1900) # changes the relative year to the actual year
  
head(olympicMetric)
```

## Question 2

### (a)

```{r q2a, fig.lp = "fig:", fig.cap = "Scatterplot of long jump distance against high jump distance."}
ggplot(olympicMetric, aes(LongJump, HighJump)) + 
  geom_point() + 
  labs(x="Long Jump", y="High Jump")
```

There appears to be a linear trend.

### (b)
```{r}
olympicLm <- lm(HighJump ~ LongJump, data = olympicMetric) 
```

### (c)
The summary output form `olympicLm` is given below
```{r}
summary(olympicLm)
```

Hence, the point estimates are $\hat{\beta}_0=`r coef(olympicLm)[1]`$, $\hat{\beta}_1=`r coef(olympicLm)[2]`$, and $\hat{\sigma}=`r sigma(olympicLm)`$.

### (d) 

```{r q2d1, fig.lp = "fig:", fig.cap = "Diagnostic plots for the linear regression."}
fit_df <- olympicLm %>% augment()
g1 <- fit_df %>% 
  ggplot(aes(1, .resid)) + 
  geom_boxplot() + labs(x="", y="Residual") + 
  theme(axis.text.x = element_blank())

g2 <- fit_df %>% 
  ggplot(aes(LongJump, .resid)) + 
  geom_point() + labs(x="Long Jump", y="Residual") + 
  geom_hline(yintercept=0, color="blue")

g3 <- fit_df %>% 
  ggplot(aes(sample=.resid)) + 
  stat_qq() + stat_qq_line(color="blue")

g1 + g2 + g3

```

```{r q2d2, fig.lp = "fig:", warning=F, fig.cap = "Fitted regression line"}
ggplot(olympic, aes(LongJump, HighJump)) + 
  geom_point() + geom_smooth(method="lm", se=F)
```

There are no clear violations of the assumptions on the residuals. The regression appears appropriate.

## Question 3

### (a)
The 95\% confidence intervals for $\beta_0$ (intercept) and $\beta_1$ (LongJump) are given below
```{r}
confint(olympicLm)
```

We need to following to calculate the 95\% CI for $\sigma^2$
```{r}
RSS <- deviance(olympicLm)
alpha <- 0.05
n <- nrow(olympic) # 20
p <- length(coef(olympicLm)) # 2
df <- n - p
a <- qchisq(alpha / 2, df)
b <- qchisq(1 - alpha / 2, df)
```

Hence, the 95\% confidence interval for $\sigma^2$ is
```{r}
c(RSS / b, RSS / a)
```


### (b)

The missing values of HighJump are the last 3 values of the dataset. The point estimates from the regression are respectively

```{r}
olympicLm$coefficients[1] + olympicLm$coefficients[2] * olympicMetric$LongJump[21:23]
```

### (c)

It is more appropriate to construct prediction intervals, as the missing observations can be thought of as random variables not parameters (in which case 
confidence intervals are more appropriate).

### (d)

The 99\% prediction intervals for the missing values of HighJump are

```{r}
predict(olympicLm, newdata = data.frame(LongJump = olympicMetric$LongJump[21:23]),
        interval = "prediction", level = 0.99)
```

