Parameter Setup

The first parameter to the glmnet() function must be a matrix of features (which we can create using the R function, model.matrix()). The second parameter is a vector with the output variable. Finally, the alpha parameter is a switch between ridge regression (0) and lasso (1). The following code sets up for our example:

# --- load the package 
library(glmnet) 
# --- create our parameter data 
cars_train_mat <- model.matrix(Price ~ .-Saturn, cars_train)[,-1] 
lambdas <- 10 ^ seq(8, -4, length = 250) 
  
The model.matrix R function creates a matrix by expanding factors to a set of summary variables (depending on the contrasts) and expanding interactions similarly.
 # --- create regression model cars_models_ridge <- glmnet(cars_train_mat, ...

Get Statistics for Data Science now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.