0

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)
gsmafra
  • 2,434
  • 18
  • 26

0 Answers0