Model Assessment

  1. mtcars is a data set of information on cars that is distributed with R. You can access it by typing its name
head(mtcars)
  1. For penalised regression we need to scale predictors. In this example we want to predict mpg, the efficiency of each car. The code below will separate the response from the predictors and scale the predictors to have 0 mean, unit variance.
y_train = mtcars[, 1]
x_train = mtcars[, -1]
x_scaled = scale(x_train)
  1. Fit a lasso model to this data.
library(glmnet)
fit = glmnet(x_scaled, y_train, alpha = 1)
  1. Use cv.glmnet to find a good choice of penalty for the lasso model
fit_cv = cv.glmnet(x_scaled, y_train, alpha = 1)
  1. You can extract coefficents from the object returned in question 4 with (in my case I stored the result of cross validation in fit_cv). How do these compare to standard linear regression?
coef(fit_cv, s = "lambda.min")


jr-packages/jroreillyregression documentation built on Dec. 23, 2019, 8:20 p.m.