Due: Wednesday 2020-02-26 at 11:59pm
In this lab we are going to practice cross validation. A few reminders:
lab-03-cv-YOUR-GITHUB-HANDLE
. This repo contains a template you can build on to complete your assignment.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.
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.
Note that the packages are also loaded with the same commands in your R Markdown document.
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
If you would like your git password cached for a week for this project, type the following in the Terminal:
Currently your project is called Untitled Project. Update the name of your project to be “Lab 03 - Cross Validation”.
Before we introduce the data, let’s warm up with some simple exercises.
Open the R Markdown (Rmd) file in your project, change the author name to your name, and knit the document.
For this lab, we are using the Auto
data from the ISLR
package.
Explain how k-fold Cross Validation is implemented.
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?
We are trying to decide between three models of varying flexibility:
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.set.seed(1)
Auto_2 <- Auto %>%
--- %>% ## shuffle the data
--- ## add a column, k, to split the data into two groups
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)
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?
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.
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: