It is great to get the coefficients from glmnet elastic net with the tidy command. It returns the coefficients of the BestModel correctly but the related penalty seems to be incorrect or I misinterpret it. Below is the code that shows the problem:
library(tidymodels)
set.seed(123)
DataTrain <- mtcars[,1:4]
CvFolds=vfold_cv(DataTrain,v=3,repeats=3)
ModelElastNet=linear_reg(mode = "regression",
penalty = tune(), mixture=tune()) %>%
set_engine("glmnet")
TuneParameters=parameters(penalty(),mixture())
TuneGrid=grid_regular(TuneParameters,levels = c(10,10))
TuneResults=tune_grid(ModelElastNet,
mpg~.,
resamples=CvFolds,
grid = TuneGrid,
metrics = metric_set(mae))
BestHyperParameters=select_best(TuneResults,"mae")
BestModel=finalize_model(ModelElastNet,
BestHyperParameters)%>%
fit(mpg ~ ., data=DataTrain)
# The command below returns coefficients correctly
# It seems the penalty is not correct. It is rounded to the closest
# element of penalty(range()) in this case 1.
# The correct penalty/lambda is 0.5055
CoefBestModelWrong=tidy(BestModel)
# The command below returns coefficients and penalty correctly
CoefBestModel=tidy(BestModel$fit) %>% filter(step==max(step))
Thanks in advance. Carsten