Skip to content

predict.workflow() fails if a recipe step affects an outcome #31

Description

@hplieninger

As illustrated below, predict.workflow() fails if the recipe has a step that transforms the outcome.

I think, adding , outcomes = TRUE to the following line would fix the issue.

forged <- hardhat::forge(new_data, blueprint)

# Modified from: example("predict.workflow")

library("workflows")
library("parsnip")
library("recipes")

training <- mtcars[1:20,]
testing <- mtcars[21:32,]

model <- linear_reg()
model <- set_engine(model, "lm")

workflow <- workflow()
workflow <- add_model(workflow, model)

recipe  <- recipe(mpg ~ cyl + disp, training)
recipe1 <- step_log(recipe, disp)
recipe2 <- step_log(recipe, all_outcomes())

workflow1 <- add_recipe(workflow, recipe)
workflow2 <- add_recipe(workflow, recipe2)

fit_workflow1 <- fit(workflow1, training)
fit_workflow2 <- fit(workflow2, training)

predict(fit_workflow1, testing)
#> # A tibble: 12 x 1
#>    .pred
#>    <dbl>
#>  1  26.5
#>  2  15.1
#>  3  15.5
#>  4  14.4
#>  5  13.2
#>  6  27.5
#>  7  26.5
#>  8  27.1
#>  9  14.3
#> 10  22.6
#> 11  15.5
#> 12  26.5
predict(fit_workflow2, testing)
#> Error: Assigned data `log(new_data[[col_names[i]]] + object$offset, base = object$base)` must be compatible with existing data.
#> x Existing data has 12 rows.
#> x Assigned data has 0 rows.
#> i Only vectors of size 1 are recycled.

Created on 2020-04-02 by the reprex package (v0.3.0)

Activity

  1. DavisVaughan commented on Apr 3, 2020

    @DavisVaughan
    Member

    Adding outcomes = TRUE wouldn't fix this, as that would require that you always have the outcomes available at prediction time, which you generally don't unless you are doing resampling.

    This problem is outlined in the hardhat vignette https://tidymodels.github.io/hardhat/articles/forge.html#a-note-on-recipes

    Essentially you need to skip the recipe steps that specifically affect the outcome, so they aren't run at bake time. This keeps recipes from trying to look for an outcome that won't be there.

    suppressPackageStartupMessages({
      library("workflows")
      library("parsnip")
      library("recipes")
    })
    
    training <- mtcars[1:20,]
    testing <- mtcars[21:32,]
    
    model <- linear_reg()
    model <- set_engine(model, "lm")
    
    workflow <- workflow()
    workflow <- add_model(workflow, model)
    
    recipe  <- recipe(mpg ~ cyl + disp, training)
    recipe2 <- step_log(recipe, all_outcomes(), skip = TRUE)
    
    workflow2 <- add_recipe(workflow, recipe2)
    
    fit_workflow2 <- fit(workflow2, training)
    
    predict(fit_workflow2, testing)
    #> # A tibble: 12 x 1
    #>    .pred
    #>    <dbl>
    #>  1  3.25
    #>  2  2.72
    #>  3  2.75
    #>  4  2.67
    #>  5  2.59
    #>  6  3.32
    #>  7  3.25
    #>  8  3.30
    #>  9  2.67
    #> 10  3.11
    #> 11  2.75
    #> 12  3.25

    Created on 2020-04-03 by the reprex package (v0.3.0)

  2. hplieninger commented on Apr 3, 2020

    @hplieninger
    Author

    I see, thanks for the quick and informative answer. In hindsight, and after checking the respective recipes vignette again, this is clear.

  3. github-actions commented on Mar 6, 2021

    @github-actions

    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.

  4. locked and limited conversation to collaborators on Mar 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions