Description
Hello to all,
The problem
I have been using the excellent parsnip package, I have been running some simulations to assess the effects of changing the regularization parameter. Now, there are cases in which I get the following message:
Error in .check_glmnet_penalty_predict()
:
! The glmnet model was fit with a single penalty value of 10. Predicting with a value of 10 will give incorrect results from glmnet()
.
Run rlang::last_trace()
to see where the error occurred.
This even though In all the simulations I am using the same penalty value of 10. Here post I reproducible example, all the simulations run smoothly but for some reason, the 120th simulation (that uses the same data generating process and parametrization) gives me this error.
Reproducible example
## copy your code to the clipboard and run:
reprex::reprex(si = TRUE)
library(tidymodels)
asses_cv<-1
n_folds<-1
pen_val<-seq(from=10,to=10,length.out=1)
tuning_g<-expand_grid(penalty=pen_val,mixture=c(1))
for (i in 1:120){
set.seed(i)
x1<-rnorm(100)
x2<-rnorm(100)
y<-3+rnorm(100)
train<-tibble(y,x1,x2)
lm_elne<-linear_reg(penalty = tune(),mixture = tune()) %>%
set_engine("glmnet",path_values = pen_val,thresh=1E-25)
rec<-recipe(y~.,train)
folds<-rolling_origin(train,initial = (NROW(train)-n_folds-asses_cv+1),assess = asses_cv)
wf_elne<-workflow() %>%
add_model(lm_elne) %>%
add_recipe(rec)
lm_elne_res<-tune_grid(wf_elne,resamples=folds,grid=tuning_g,metrics = yardstick::metric_set(rmse))
wf_elne %>%
finalize_workflow(select_best(lm_elne_res,metric = "rmse")) %>%
fit(train) %>%
predict(train[1,])
print(i)
}
I would appreciate any comment.
Thank you very much,