In this practical, we will focus on ridge regression.
One of the packages we are going to use is glmnet
. For this, you will probably need to install.packages("glmnet")
before running the library()
functions. GGally
is also a new package, that needs to be installed to access the ggpairs()
function.
library(MASS)
library(magrittr)
library(dplyr)
library(GGally)
library(glmnet)
library(caret)
Before starting with the exercises, it is a good idea to set your seed, so that (1) your answers are reproducible and (2) you can compare your answers with the answers provided.
set.seed(123)
The mtcars
data set from package MASS
contains fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models)
mtcars
data set. Is everything properly coded?.hp
as the response and all other features as the predictors. Try to use the exposition (%$%
) pipe.ridge
.ridge
object and the coef()
thereof.summary()
of the ridge regressionridge
twice: once with the deviance on the x-axis and once with the log of lambda on the x-axis. Hint: see ?plot.glmnet
.cv.ridge
.cv.ridge
object and run the object through plot()
.Important Note: We have used both a test/train split and a k-fold cross-validation procedure. A different seed value will result in different splits and cross-validation folds. Don’t forget that once you fix the seed, everything will become seed dependent.
End of Practical