How to regularize the intercept with glmnet

南楼画角 提交于 2019-12-13 03:44:52

问题


I know that glmnet does not regularize the intercept by default, but I would like to do it anyway. I was taking a look at this question and tried to do what whuber suggested (adding a constant variable and turning the parameter intercept to FALSE) , but as a result glmnet is not fitting the added constant as well.

library(dplyr)
library(glmnet)

X <-
  mtcars %>%
  mutate(intercept = 1) %>%
  select(-c(mpg)) %>%
  as.matrix()

y <-
  mtcars %>%
  select(mpg) %>%
  as.matrix()

model <- glmnet(X, y, intercept = FALSE, alpha = 0, lambda = 0)

coef(model)

来源:https://stackoverflow.com/questions/49888214/how-to-regularize-the-intercept-with-glmnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!