Lab 04 - Ridge Lasso Elastic Net

Due: Tuesday 2020-03-24 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 tidymodels for statistial modeling.

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(tidymodels)

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 04 - Ridge, Lasso, and Elastic Net”.

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 a data frame currently in music.csv. This data frame includes 72 predictors that are components of audio files and one outcome, lat, the latitude of where the music originated. We are trying to predict the location of the music’s origin using audio components of the music.

Exercises

  1. Set a seed of 7. Split the music data into a training and test set with 50% of the data in the training and 50% in the testing. Call your training set music_train and your testing set music_test. Describe these data sets (how many observations, how many variables).
music <- read_csv("music.csv")
## Parsed with column specification:
## cols(
##   .default = col_double()
## )
## See spec(...) for full column specifications.
  1. We are interested in predicting the latitude (lat) of the music’s origin from all other variables. Fit a linear model using least squares on the training set. Report the test root mean squared error obtained.

  2. Fit a ridge regression model on the training set with \(\lambda\) chosen using 10-fold cross valiation. Report the \(\lambda\) chosen and explain why. Report the test root mean squared error obtained using testing portion of the initially split data frame.

  3. Fit a lasso model on the training set with \(\lambda\) chosen using cross valiation. Report the \(\lambda\) chosen and explain why. Report the test root mean squared error obtained using testing portion of the initially split data frame.

  4. Fit an elastic net model on the training set with \(\lambda\) and \(\alpha\) chosen using cross valiation. Report the \(\lambda\) chosen and explain why. Report the test root mean squared error obtained using testing portion of the initially split data frame.

  5. Comment on the results obtained. How accurately can we predict the latitude of where the music originated? Is there much difference among the test errors resulting from these four approaches?