Skip to Content
Statistics for Data Science
book

Statistics for Data Science

by James C. Mott, Rajprasath Subramanian, Shaikh Salamatullah, James D. Miller, Vijayakumar Ramdoss
November 2017
Beginner content levelBeginner
286 pages
8h 13m
English
Packt Publishing
Content preview from Statistics for Data Science

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R for Data Science

R for Data Science

Hadley Wickham, Garrett Grolemund
Learning Data Science

Learning Data Science

Sam Lau, Joseph Gonzalez, Deborah Nolan

Publisher Resources

ISBN: 9781788290678Supplemental Content