---
title: "Lab 2"
author: "Your Name Here"
date: ""
output:
  html_document:
    toc: true
    toc_depth: 4
    theme: cerulean
    highlight: tango
---

```{r package_load}
library(ggplot2) # graphics library
library(MASS)    # contains data sets
library(ISLR)    # contains code and data from the textbook
library(knitr)   # contains kable() function
library(boot)    # contains cross-validation functions
library(gam)     # needed for additive models

options(scipen = 4)  # Suppresses scientific notation
```

### 1. Changing the author field and file name.

##### (a) Change the `author:` field on the Rmd document from Your Name Here to your own name.

##### (b) Rename this file to "Lab02_YourHameHere.Rmd", where YourNameHere is changed to your own name.

<hr>

> The next portion of the lab gets you to carry out the Lab in &sect;5.3 of ISLR (Pages 191 - 193).  You will want to have the textbook Lab open in front you as you go through these exercises.  The ISLR Lab provides much more context and explanation for what you're doing.  



### 2. The Validtion Set Approach

> You will need the `Auto` data set from the `ISLR` library in order to complete this exercise.

> Please run all of the code indicated in &sect;5.3.1 of ISLR, even if I don't explicitly ask you to do so in this document.

##### (a) Run the `View()` command on the `Auto` data to see what the data set looks like.

##### Use `qplot` to construct a scatterplot of `mpg` vs `horsepower`.  Use `stat_smooth()` to overlay a linear, quadratic, and cubic polynomial fit to the data.  

```{r}
# Edit me
```

##### (b) Use the command `set.seed(1)` to set the seed of the random number generator.  This will ensure that your answers match those in the text.

```{r}
# Edit me
```

##### (c) Use the `sample()` command to construct `train`, a vector of observation indexes to be used for the purpose of training your model.

```{r}
# Edit me
```

##### Describe what the sample() function as used above actually does.

- **Your answer here**

##### (c) Fit a linear model regression `mpg` on `horsepower`, specifying `subset = train`.  Save this in a variable called `lm.fit`.

```{r}
# Edit me
```

##### Why do we use the argument `subset = train`?

- **Your answer here**

##### (d) Calculate the MSE of `lm.fit` on the test set (i.e., all points that are not in `train`)

```{r}
# Edit me
```


##### (e) Use the `poly()` command to fit a quadratic regression model of `mpg` on `horsepower`, specifying `subset = train`.  Save this in a variable called `lm.fit2`.    

```{r}
# Edit me
```


##### Is the coefficient of the quadratic term statistically significant?  

```{r}
# Edit me
```

- **Your answer here**

##### Calculate the test MSE of `lm.fit2`.  How does it compare to the linear regression fit?

```{r}
# Edit me
```

- **Your answer here**

##### (f) Use the `poly()` command to fit a cubic regression model of `mpg` on `horsepower`, specifying `subset = train`.  Save this in a variable called `lm.fit3`.    

```{r}
# Edit me
```

##### Is the coefficient of the cubic term statistically significant?  

```{r}
# Edit me
```

- **Your answer here**

##### Calculate the test error of the cubic fit.

```{r}
# Edit me
```


##### (g) How do the test errors of the three models compare?  Which of the three models should we use?  

```{r}
# Edit me
```

- **Your answer here**

##### (h) Re-run all of the code above, but this time setting `set.seed(5)`.  You do not have to retype all of the code.  You can just change the initial `set.seed()` command and see what happens.  

- **Your answer here**

##### What does changing the seed value do?  Did this change your estimated test errors?  Why did the values change?  Should we still pick the same model?

- **Your answer here**

### 3. Leave-One-Out Cross-Validation

> This exercise introduces you to the `cv.glm()` command from the `boot` library, which automates K-fold cross-validation for Generalized Linear Models (GLMs).  Linear regression is one example of a GLM.  Logistic regression is another.  GLMs are not the same as Generalized Additive Models (GAMs).

> Please run all of the code indicated in &sect;5.3.2 of ISLR, even if I don't explicitly ask you to do so in this document.

##### (a) Use the `glm` command to fit a linear regression of `mpg` on `horsepower`.  Call the resulting model `glm.fit`  Confirm that this gives the same coefficient estimates as a linear model fit with the `lm` command.  

**Note**: You should fit the model to the entire data, not just to the training data.

```{r}
# Edit me
```

- **Your answer here**

##### (b) Run all of the code in &sect;5.3.2.   Construct a plot of `cv.error`, the vector of LOOCV error estimates for polynomials of degree 1-5.  

**Note**: The computations take some time to run, so I've set `cache = TRUE` in the code chunk header to make sure that the code below doesn't re-execute at knit time unless this particular chunk has changed.

```{r, cache = TRUE}
# Edit me (insert cross-validation code here)
```

```{r}
# Edit me (insert plot code here)
```

##### (c) Which degree model has the lowest LOOCV estimate of test error?  

```{r}
# Edit me
```

- **Your answer here**

##### (d) Which model should we choose?  Is this the model with the lowest LOOCV estimate of test error?  Explain.

- **Your answer here**

### 4. K-fold Cross-validation  

> Please run all of the code indicated in &sect;5.3.3 of ISLR

##### (a)  Run all of the code in the $k$-Fold Cross-validation Lab section.

```{r, cache = TRUE}
# Edit me
```

##### (b) Construct a plot of 10-fold CV error vs. degree.  

```{r}
# Edit me
```

##### (c) Which model has the lowest 10-fold CV estimate of test error?

```{r}
# Edit me
```

- **Your answer here**

##### (d) Which model should we choose?  Is this the model with the lowest 10-fold CV estimate of test error?  Explain.

- **Your answer here**

### 5. Additive Models and Splines

> Please run all of the code indicated in &sect;7.8.3 of ISLR, up until the loading of the `akima` package.  We have not yet studied logistic regression, so you are not asked to do the logistic regression analysis that starts at the bottom of p. 297.  The material on ANOVA testing may also be unfamiliar to you.  You may skip it.

##### (a) Think carefully about what each line of code below is doing.  Write comments in the code below to explain what each line is responsible for.  

```{r, fig.width = 8, fig.height = 4}
# Your comment here
gam1 <- lm(wage ~ ns(year, 4) + ns(age, 5) + education, data=Wage)

# Your comment here
gam.m3 <- gam(wage ~ s(year, 4) + s(age, 5) + education, data=Wage)

# Your comment here
par(mfrow=c(1,3))

# Your comment here
plot(gam.m3, se=TRUE,col="blue")

# Your comment here
plot.gam(gam1, se=TRUE, col="red")

# Your comment here
gam.m1 <- gam(wage ~ s(age,5) + education, data=Wage)

# Your comment here
gam.m2 <- gam(wage ~ year + s(age, 5) + education, data=Wage)

# Your comment here
anova(gam.m1, gam.m2, gam.m3, test="F")

# Your comment here
summary(gam.m3)

# Your comment here
preds <- predict(gam.m2, newdata=Wage)

# Your comment here
gam.lo <- gam(wage ~ s(year, df=4) + lo(age, span=0.7) + education, data=Wage)

# Your comment here
plot.gam(gam.lo, se=TRUE, col="green")

# Your comment here
gam.lo.i <- gam(wage ~ lo(year, age, span=0.5) + education, data=Wage)
```
##### (a) Describe the estimated marginal relationship between `wage` and `age` in the `gam.lo` model.  How do you interpret the dependence plot between `wage` and `education`? 

##### (b) Looking at the fit plots, does it look like an additive model is a good fit for the data, or are we "overcomplicating things", and a linear model would've probably done fine also?

```{r}
# Edit me
```

- **Your answer here**

##### (c)  **Optional**:  Use the ANOVA test output described in the ISLR discussion of the lab to help better answer part (b).

```{r}
# Edit me
```

- **Your answer here**

### 6. Splines and cross-validation

> The `splines` library has a `smooth.spline()` command with built-in cross-validated smoothness selection.  We will now give an example of using this command.

##### The code below is adapted from the bottom half of page 293.  Add comments to the code below indicating what each line of code is doing.

```{r}
agelims <- range(Wage$age)

# Your comment here
with(Wage, plot(age, wage, xlim = agelims, cex=0.5, col = "darkgrey"))
title("Smoothing Spline")

# Your comment here
fit <- with(Wage, smooth.spline(age, wage, df=16))

# Your comment here
fit2 <- with(Wage, smooth.spline(age, wage, cv=TRUE))

# Your comment here
fit2$df

# Your comment here
lines(fit, col="red", lwd=2)
lines(fit2, col="blue", lwd=2)
```

##### Based on the documentation for the `smooth.spline` function, can you figure out what kind of cross-validation is done when `cv = TRUE`?  i.e., It's $K$-fold CV for what choice of $K$?

- **Your answer here**