Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using the predict function #1223

Open
SantiagoD999 opened this issue Dec 1, 2024 · 1 comment
Open

Error when using the predict function #1223

SantiagoD999 opened this issue Dec 1, 2024 · 1 comment

Comments

@SantiagoD999
Copy link

SantiagoD999 commented Dec 1, 2024

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,

@topepo
Copy link
Member

topepo commented Jan 28, 2025

The notion for this comes from glmnet itself:

WARNING: use with care. Avoid supplying a single value for lambda (for predictions after CV use predict() instead). Supply instead a decreasing sequence of lambda values. glmnet relies on its warms starts for speed, and its often faster to fit a whole path than compute a single fit.

I'll discuss with the group; we could convert this to a warning but there is probably a good reason we made it an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants