get fitted values from tidymodel implementation of glmnet

236 views Asked by At

I am performing elastic net linear regression in tidymodels using the glmnet engine.

If I were to run this directly in glmnet I could do something like this:

    cv_fit <- cv.glmnet(
        y = response_vec, 
        x = predictor_matrix, 
        nfolds = 10, 
        alpha = 0.95, 
        type.measure = "mse", 
        keep = TRUE)

I can then get the fitted values like this:

fitted_y <- cv_fit$fit.preval

However, I cannot find how to get fitted values / residuals for the glmnet model fitted using parsnip. Any help appreciated.

1

There are 1 answers

0
mbyvcm On

What I was looking for is the control argument. save_pred = TRUE ensures that fitted values are stored within the returned object:

  tuning_mod <- wf %>%
    tune::tune_grid(
      resample = rsample::vfold_cv(data = my_data, v = 10, repeats = 3),
      grid = dials::grid_regular(x = dials::penalty(), levels = 200),
      metrics = yardstick::metric_set(yardstick::rmse, yardstick::rsq),
      control = control_resamples(save_pred = TRUE)
    )

  tune::collect_predictions(tuning_mod)