O'Reilly logo

Practical Predictive Analytics by Ralph Winters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

An example – ElasticNet

To illustrate regularization for our PainGLM data, we will use the ElasticNet regularization algorithm (contained within the glmnet package), which combines several different methods of regularization.

Start by creating some dummy variables using the model.matrix() function which we have used previously. Then, merge in the Duration variable to form a new matrix:

dummy.vars <- model.matrix(df$Pain ~ df$Treatment + df$Gender + df$Age + df$Duration)[,-1]x <- as.matrix(data.frame(df$Duration,dummy.vars))head(x)

This is the following output:

  df.Treatment.T.B. df.Treatment.T.P. df.Gender.T.M. df.Age df.Duration1                 0                 1              0     68           12                 0                 1              1     66          263                 0                 0              0     71          124                 0                 0              1     71          175                 1                 0              0     66          126                 0                 0              0     64          17

Next, run Lasso regularization ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required