Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Last active April 28, 2017 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpghayes/dbe636d510e6acfa50bf5741289ea7cc to your computer and use it in GitHub Desktop.
Save alexpghayes/dbe636d510e6acfa50bf5741289ea7cc to your computer and use it in GitHub Desktop.
glmnet factor example
library(ggplot2)
library(glmnet)
x <- model.matrix(mpg ~ factor(cyl) + factor(gear) + wt * factor(gear), mtcars)
y <- mtcars$mpg
x
fit <- cv.glmnet(x, y, alpha = 1) # lasso when alpha = 1, ridge when alpha = 0
coef(fit, s = fit$lambda.min)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment