Created
February 21, 2016 07:24
-
-
Save smrmkt/8afc161cca478e781bb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(glmnet) | |
library(caret) | |
library(psych) | |
# load data | |
data(cars) | |
pairs.panels(cars) | |
# lm | |
fit.lm <- glm(Price ~ ., data = cars) | |
summary(fit.lm) | |
# lasso | |
fit.glmnet.lasso <- glmnet(as.matrix(cars[, -1]), | |
as.matrix(cars[, 1]), | |
alpha = 1) | |
plot(fit.glmnet.lasso) | |
## cv | |
fit.glmnet.lasso.cv <- cv.glmnet(as.matrix(cars[, -1]), | |
as.matrix(cars[, 1]), | |
nfold = 5, | |
alpha = 1) | |
plot(fit.glmnet.lasso.cv) | |
fit.glmnet.lasso.cv$lambda.min | |
fit.glmnet.lasso.cv$lambda.1se | |
coef(fit.glmnet.lasso.cv, s = fit.glmnet.lasso.cv$lambda.min) | |
# ridge | |
fit.glmnet.ridge <- glmnet(as.matrix(cars[, -1]), | |
as.matrix(cars[, 1]), | |
alpha = 0) | |
plot(fit.glmnet.ridge) | |
## cv | |
fit.glmnet.ridge.cv <- cv.glmnet(as.matrix(cars[, -1]), | |
as.matrix(cars[, 1]), | |
nfold = 5, | |
alpha = 0) | |
plot(fit.glmnet.ridge.cv) | |
fit.glmnet.ridge.cv$lambda.min | |
fit.glmnet.ridge.cv$lambda.1se | |
coef(fit.glmnet.ridge.cv, s = fit.glmnet.ridge.cv$lambda.min) | |
# ElasticNet | |
fit.glmnet.elasticnet.cv <- cv.glmnet(as.matrix(cars[, -1]), | |
as.matrix(cars[, 1]), | |
nfold = 5, | |
alpha = 0.5) | |
plot(fit.glmnet.elasticnet.cv) | |
fit.glmnet.elasticnet.cv$lambda.min | |
fit.glmnet.elasticnet.cv$lambda.1se | |
coef(fit.glmnet.elasticnet.cv, s = fit.glmnet.elasticnet.cv$lambda.min) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment