Is there any way to retrieve the trained model from last_fit?

Is there any way to retrieve the trained model from last_fit?
Does it just want to check the performance of the test set?

last_fit <- last_fit(workflow,initial_split_object)

Additionally, is there any way to evaluate the performance of the training set?
I'm predicting both test and train on a model that I fit() every time.

Isn't this a beautiful way?

predict(last_fit_model,train) %>% 
  bind_cols(train) %>% 
  rmse(truth=target, estimate=.pred)

predict(last_fit_model,train) %>% 
  bind_cols(train) %>% 
  rmse(truth=target, estimate=.pred)

thank you!

If you are using the most recent versions:

suppressPackageStartupMessages(library(recipes))
suppressPackageStartupMessages(library(rsample))
suppressPackageStartupMessages(library(parsnip))
suppressPackageStartupMessages(library(tune))
suppressPackageStartupMessages(library(broom))

set.seed(6735)
tr_te_split <- initial_split(mtcars)

spline_rec <- recipe(mpg ~ ., data = mtcars) %>% step_ns(disp)

lin_mod <- linear_reg()

spline_res <- last_fit(lin_mod, spline_rec, split = tr_te_split)

# get the fitted workflow
spline_res %>% 
  extract_workflow()
#> ══ Workflow [trained] ══════════════════════════════════════════════════════════
#> Preprocessor: Recipe
#> Model: linear_reg()
#> 
#> ── Preprocessor ────────────────────────────────────────────────────────────────
#> 1 Recipe Step
#> 
#> • step_ns()
#> 
#> ── Model ───────────────────────────────────────────────────────────────────────
#> 
#> Call:
#> stats::lm(formula = ..y ~ ., data = data)
#> 
#> Coefficients:
#> (Intercept)          cyl           hp         drat           wt         qsec  
#>   23.087028     0.326218     0.005969    -0.009576    -0.902839     0.185826  
#>          vs           am         gear         carb    disp_ns_1    disp_ns_2  
#>    1.492756     4.101555     0.174875    -1.278962   -15.149506    -4.905087

# We don't have a tidymodels api like `collect_metrics()` for getting training 
# set statistics. We generally think that it is a bad idea. You can still do it 
# in a few lines though

spline_res %>% 
  extract_workflow() %>% 
  augment(training(tr_te_split)) %>% 
  rmse(mpg, .pred)   # or use with a metric set
#> # A tibble: 1 × 3
#>   .metric .estimator .estimate
#>   <chr>   <chr>          <dbl>
#> 1 rmse    standard        1.69

Created on 2021-10-27 by the reprex package (v2.0.0)

1 Like

thank you.

I assumed that only empty workflows could be retrieved.
I didn't think it was possible to retrieve trained workflows.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.