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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper function to extract fitted workflow from last_fit() output #378

Closed
juliasilge opened this issue May 3, 2021 · 3 comments
Closed
Labels
feature a feature request or enhancement

Comments

@juliasilge
Copy link
Member

In tidymodels/tidymodels#58, @hfrick pointed out that it is pretty awkward to get out the fitted workflow from the output of last_fit(); it's all $.workflow[[1]]. 馃槪 This is what you have to do to get a fitted workflow you can use for prediction.

library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#>   method                   from   
#>   required_pkgs.model_spec parsnip

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

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

lin_mod <- linear_reg() %>%
  set_engine("lm")

spline_wfl <-
  workflow() %>%
  add_recipe(spline_rec) %>%
  add_model(lin_mod)

final_fitted <- last_fit(spline_wfl, car_split)

collect_metrics(final_fitted)
#> # A tibble: 2 x 4
#>   .metric .estimator .estimate .config             
#>   <chr>   <chr>          <dbl> <chr>               
#> 1 rmse    standard       5.74  Preprocessor1_Model1
#> 2 rsq     standard       0.503 Preprocessor1_Model1
collect_predictions(final_fitted)
#> # A tibble: 8 x 5
#>   id               .pred  .row   mpg .config             
#>   <chr>            <dbl> <int> <dbl> <chr>               
#> 1 train/test split 18.1      5  18.7 Preprocessor1_Model1
#> 2 train/test split 31.1      9  22.8 Preprocessor1_Model1
#> 3 train/test split 16.9     14  15.2 Preprocessor1_Model1
#> 4 train/test split 20.4     15  10.4 Preprocessor1_Model1
#> 5 train/test split 18.0     16  10.4 Preprocessor1_Model1
#> 6 train/test split 28.3     19  30.4 Preprocessor1_Model1
#> 7 train/test split  9.04    24  13.3 Preprocessor1_Model1
#> 8 train/test split 23.0     30  19.7 Preprocessor1_Model1


final_fitted$.workflow[[1]]
#> 鈺愨晲 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  
#>   -19.69774      1.09769     -0.02326     -3.05865     -4.28722      2.80367  
#>          vs           am         gear         carb    disp_ns_1    disp_ns_2  
#>    -3.37155      0.18869      4.33774      0.52292    -12.82725      4.55395

Created on 2021-05-03 by the reprex package (v2.0.0)

What do we think about a little helper function to take care of this, like we have the helper functions for the metrics and predictions? We have collect_metrics(), collect_predictions() -- maybe collect_workflow()? It's nice and parallel from the same structure. I feel like pull_xx is another obvious choice but it is getting overloaded quickly for us.

@juliasilge juliasilge added the feature a feature request or enhancement label May 3, 2021
@topepo
Copy link
Member

topepo commented May 4, 2021

We should review this at the next group meeting. I feel that we should make the pull_* functions generics (but there are other options).

@topepo
Copy link
Member

topepo commented Jul 19, 2021

Closed by #388

@topepo topepo closed this as completed Jul 19, 2021
@github-actions
Copy link

github-actions bot commented Aug 3, 2021

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants