Lab 03 - Cross validation

Due: Wednesday 2020-02-26 at 11:59pm

Introduction

In this lab we are going to practice cross validation. A few reminders:

Getting started

Packages

In this lab we will work with two packages: tidyverse which is a collection of packages for doing data analysis in a “tidy” way and the ISLR a package that contains our data.

Install these packages by running the following in the console.

install.packages("tidyverse")
install.packages("ISLR")

Now that the necessary packages are installed, you should be able to Knit your document and see the results.

If you’d like to run your code in the Console as well you’ll also need to load the packages there. To do so, run the following in the console.

library(tidyverse) 
library(ISLR)

Note that the packages are also loaded with the same commands in your R Markdown document.

Housekeeping

Git configuration

Your email address is the address tied to your GitHub account and your name should be first and last name.

To confirm that the changes have been implemented, run the following

Password caching

If you would like your git password cached for a week for this project, type the following in the Terminal:

Project name:

Currently your project is called Untitled Project. Update the name of your project to be “Lab 03 - Cross Validation”.

Warm up

Before we introduce the data, let’s warm up with some simple exercises.

YAML:

Open the R Markdown (Rmd) file in your project, change the author name to your name, and knit the document.

Commiting and pushing changes:

Data

For this lab, we are using the Auto data from the ISLR package.

Exercises

Conceptual questions

  1. Explain how k-fold Cross Validation is implemented.

  2. What are the advantages / disadvantages of k-fold Cross Validation compared to the Validation Set approach? What are the advantages / disadvantages of k-fold Cross Validation compared to Leave-one-out Cross Validation?

K-fold cross validation

We are trying to decide between three models of varying flexibility:

  1. Using the Auto data, shuffle the rows and add a column, k, that groups the data into two groups, the first group, k = 1, will consist of 196 rows of the data to train the model, and k = 2 will consist of 196 rows to test the model. Make sure you set a seed so you end up with the same answer each time you run this.

You can use the poly() function to fit a model with a polynomial term. For example, to fit the model \(y = \beta_0 + \beta_1 \texttt{x} + \beta_2 \texttt{x}^2 + \beta_3 \texttt{x}^3 + \epsilon\), you would run lm(y ~ poly(x, 3), data = data)

  1. Filter the data frame to the training data only. Fit the three models detailed above on this data. Then filter the data frame to the testing data only. Using the model created on the training data, predict mpg in the test data set. What is the test MSE for the three models? Which model would you choose?

  2. Using the Auto data, shuffle the rows and add a column, k, that groups the data into five groups of approximately equal size. Make sure you set a seed so you end up with the same answer each time you run this.

  3. Create a function that generalizes the approach in Exercises 3 and 4, with K as a parameter that can vary. The function should output a data frame that contains four columns:

  1. Map this function from Exercise 6 over the 5 folds and calculate the overall 5-fold Cross Validation error for each of the three models. Which model would you chose?