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

Set final workflow in last_fit() to trained #294

Closed
wants to merge 3 commits into from

Conversation

juliasilge
Copy link
Member

The objects that end up in the .workflow column of last_fit() output currently have trained as false, meaning you can't predict on them. This PR sets:

res$.workflow[[1]]$trained <- TRUE

in the same way that fit.workflow does, so that these workflows can be used for prediction.

library(tidymodels)

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

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

car_wf <-
  workflow() %>%
  add_formula(mpg ~  .) %>%
  add_model(lin_mod)

car_fit <- last_fit(car_wf, split = tr_te_split)

predict(car_fit$.workflow[[1]], new_data = training(tr_te_split))
#> # A tibble: 24 x 1
#>    .pred
#>    <dbl>
#>  1  21.9
#>  2  22.4
#>  3  25.4
#>  4  21.6
#>  5  21.9
#>  6  13.3
#>  7  24.5
#>  8  17.6
#>  9  19.4
#> 10  13.7
#> # … with 14 more rows

Created on 2020-10-06 by the reprex package (v0.3.0.9001)

@DavisVaughan
Copy link
Member

I think this is an issue with workflows. If you fit the workflow with fit(), then this method runs which sets $trained <- TRUE, but tune uses .fit_pre() and .fit_model() individually so it doesn't get run.

https://github.com/tidymodels/workflows/blob/40482ae456f3e96fc977dfa1bba60270a23e9f76/R/fit.R#L55-L75

I need to think a little about whether .fit_model() should just set $trained <- TRUE before it returns

@DavisVaughan
Copy link
Member

Okay, I have a lower level fix that will have to come in a few stages. Really workflows needs to export .fit_finalize() which would set $trained to TRUE, and then the code path functions like fit_resamples() would use it internally after calling the individual pieces of .fit_pre() and .fit_model() separately.

This will require a few preliminary PRs though, so I'm happy to handle this one

@juliasilge
Copy link
Member Author

OK, I'll take out the trained assignment and just leave in the tests, to be merged in after the changes in workflows and here. 👍

@github-actions
Copy link

github-actions bot commented Mar 6, 2021

This pull request 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 Mar 6, 2021
@simonpcouch simonpcouch deleted the last-fit-predict branch March 7, 2023 16:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants