workflows

repository·main·Indexed 42 days ago

https://github.com/tidymodels/workflows

A tidymodels ecosystem package that bundles recipes for pre-processing and parsnip models into a single object. It simplifies machine learning pipelines by providing unified execution of recipe prepping and model fitting via a single fit() call, and offers a streamlined interface for tuning parameters.

Tokens
953
Snippets
3
Records
6
Agent score
3%

What's inside workflows

  1. What is a workflow and why use it?

    main

    A workflow is an object that bundles together pre-processing (recipe), modeling (parsnip), and post-processing requests into a single unit.

    Using workflows provides several advantages:

    • Simplified Management: You don't need to track separate recipe and model objects in your workspace.
    • Unified Execution: You can execute recipe prepping, model fitting, and postprocessor estimation (including data splitting) using a single call to fit().
    • Easier Tuning: When combined with the tune package, workflows provide a simpler interface for defining custom tuning parameter settings.
  2. How workflows are structured

    main

    A workflow is organized into three distinct stages: pre, fit, and post. This structure defines how data is prepared, how models are trained, and how results are processed.

    1. pre (Preprocessing)

    This stage manages data preparation and is divided into specific "actions":

    • Formula, Recipes, and Variables actions: These correspond to the three preprocessor types in hardhat. A workflow can only use one of these per workflow.
    • Case weights action: Controls how case weights are extracted from data and passed to the parsnip model. This action is forced to run before preprocessor actions.

    2. fit (Model Fitting)

    This stage manages the model training process. It currently contains a single "model" action.

    3. post (Postprocessing)

    This stage manages post-training adjustments. It currently contains a single "tailor" action.

    Execution Lifecycle

    When you call fit() on a specified workflow, the package loops through all defined actions and calls the S3 fit() method for each individual action. This mechanism is conceptually similar to how recipes uses prep() for each step.

  3. Workaround for unsupported model types using model formulas

    main

    If a specific model type appears to be unsupported by the standard workflows model, you can often bypass restrictions by supplying a "model formula". This formula is passed directly through to parsnip as a workaround. Use the add_model(formula = ...) syntax to implement this.

    # Example workaround pattern
    workflow %>%
      add_model(model_spec, formula = your_custom_formula)
  4. Install the workflows package

    main

    You can install the stable version from CRAN or the development version from GitHub.

    From CRAN:

    install.packages("workflows")

    From GitHub (using pak):

    # install.packages("pak")
    pak::pak("tidymodels/workflows")
  5. Create and fit a workflow

    main

    To use a workflow, you initialize it with workflow(), then add your pre-processing steps using add_recipe() and your model specification using add_model(). Once configured, you can prepare the recipe and fit the model to your data in one step using fit().

    library(recipes)
    library(parsnip)
    library(workflows)
    
    # 1. Define a recipe
    spline_cars <- recipe(mpg ~ ., data = mtcars) |>
      step_ns(disp, deg_free = 10)
    
    # 2. Define a model
    bayes_lm <- linear_reg() |
      set_engine("stan")
    
    # 3. Bundle into a workflow
    car_wflow <- workflow() |
      add_recipe(spline_cars) |
      add_model(bayes_lm)
    
    # 4. Fit the entire workflow to data
    car_wflow_fit <- fit(car_wflow, data = mtcars)
    library(recipes)
    library(parsnip)
    library(workflows)
    
    spline_cars <- recipe(mpg ~ ., data = mtcars) |>
      step_ns(disp, deg_free = 10)
    
    bayes_lm <- linear_reg() |
      set_engine("stan")
    
    car_wflow <- workflow() |
      add_recipe(spline_cars) |
      add_model(bayes_lm)
    
    car_wflow_fit <- fit(car_wflow, data = mtcars)
  6. Modify an existing workflow

    main

    You can alter the components of an existing workflow using the following functions:

    • update_recipe(): Replace the current recipe.
    • update_model(): Replace the current model.
    • remove_recipe(): Remove the recipe from the workflow.
    • remove_model(): Remove the model from the workflow.