June 2017
Beginner to intermediate
576 pages
15h 22m
English
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 ...