glmnet
in RLoad glmnet
to make the functionalities available.
library(glmnetUtils)
library(tidyverse)
Ressources on glmnet
vignette("glmnet_beta")
is highly recommendable.crime <- read_csv("day06_crime.csv", col_types = cols())
Note: Due to different scales of the variables, the estimated parameters may be different in size simply due to different units. Hence, The glmnet
function automatically standardises both the response (for family = "gaussian"
) y
and the covariates x
.
crime_lasso <- glmnet(`crime rate` ~ ., alpha = 1, data = crime) ## alpha = 1: LASSO
plot(crime_lasso)
library(plotmo) # for plot_glmnet # install.packages("plotmo")
par(mfrow = c(2,2))
plot_glmnet(crime_lasso, xvar = "norm")
plot_glmnet(crime_lasso, xvar = "rlambda")
plot_glmnet(crime_lasso, xvar = "lambda")
plot_glmnet(crime_lasso, xvar = "dev")
crime_ridge <- glmnet(`crime rate` ~ ., alpha = 0, data = crime) ## alpha = 0: Ridge Regression
plot(crime_ridge)
plot_glmnet(crime_ridge, xvar = "norm")
cv_glmnet_lasso <- cv.glmnet(`crime rate` ~ ., alpha = 1, data = crime)
plot(cv_glmnet_lasso)
cv_glmnet_ridge <- cv.glmnet(`crime rate` ~ ., alpha = 0, data = crime) ## alpha = 0: Ridge
plot(cv_glmnet_ridge)
cv_glmnet_enet <- cva.glmnet(`crime rate` ~ ., data = crime)
plot(cv_glmnet_enet)
alpha_cols <- cv_glmnet_enet$alpha %>% set_names(topo.colors(length(.)), nm = .)
legend("topleft", bty = "n", title = expression(alpha),
legend = names(alpha_cols), lty = 1, col = alpha_cols)
minlossplot(cv_glmnet_enet)
There are many different types of regressions one would be interested in:
family = "binomial"
family = "poisson"
family = "multinomial"
family = "cox"
family = "mgaussian"