Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tidymodels/workflowsets
Create a collection of modeling workflows
https://github.com/tidymodels/workflowsets
Last synced: 3 days ago
JSON representation
Create a collection of modeling workflows
- Host: GitHub
- URL: https://github.com/tidymodels/workflowsets
- Owner: tidymodels
- License: other
- Created: 2020-11-24T02:30:49.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-24T15:44:55.000Z (20 days ago)
- Last Synced: 2024-10-25T18:33:07.475Z (18 days ago)
- Language: R
- Homepage: https://workflowsets.tidymodels.org/
- Size: 118 MB
- Stars: 92
- Watchers: 7
- Forks: 10
- Open Issues: 11
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- jimsghstars - tidymodels/workflowsets - Create a collection of modeling workflows (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# workflowsets
[![R-CMD-check](https://github.com/tidymodels/workflowsets/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/tidymodels/workflowsets/actions/workflows/R-CMD-check.yaml)
[![R-CMD-check-no-suggests](https://github.com/tidymodels/workflowsets/actions/workflows/R-CMD-check-no-suggests.yaml/badge.svg)](https://github.com/tidymodels/workflowsets/actions/workflows/R-CMD-check-no-suggests.yaml)
[![Codecov test coverage](https://codecov.io/gh/tidymodels/workflowsets/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidymodels/workflowsets?branch=main)
[![Lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html)The goal of workflowsets is to allow users to create and easily fit a large number of models. workflowsets can create a _workflow set_ that holds multiple workflow objects. These objects can be created by crossing all combinations of preprocessors (e.g., formula, recipe, etc) and model specifications. This set can be tuned or resampled using a set of specific functions.
## Installation
You can install the released version of workflowsets from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("workflowsets")
```And the development version from [GitHub](https://github.com/) with:
``` r
install.packages("pak")
pak::pak("tidymodels/workflowsets")
```## Example
Sometimes it is a good idea to try different types of models and preprocessing methods on a specific data set. The tidymodels framework provides tools for this purpose: [recipes](https://recipes.tidymodels.org/) for preprocessing/feature engineering and [parsnip model](https://parsnip.tidymodels.org/) specifications. The workflowsets package has functions for creating and evaluating combinations of these modeling elements.
For example, the Chicago train ridership data has many numeric predictors that are highly correlated. There are a few approaches to compensating for this issue during modeling:
1. Use a feature filter to remove redundant predictors.
2. Apply principal component analysis to decorrelate the data.
3. Use a regularized model to make the estimation process insensitive to correlated predictors.
The first two methods can be used with any model while the last option is only available for specific models. Let's create a basic recipe that we will build on:```{r sshhh, include = FALSE}
library(tidymodels)
library(glmnet)
library(rpart)
library(vctrs)
library(Matrix)
library(rlang)
theme_set(theme_bw())
```
```{r recs}
library(tidymodels)
data(Chicago)
# Use a small sample to keep file sizes down:
Chicago <- Chicago %>% slice(1:365)base_recipe <-
recipe(ridership ~ ., data = Chicago) %>%
# create date features
step_date(date) %>%
step_holiday(date) %>%
# remove date from the list of predictors
update_role(date, new_role = "id") %>%
# create dummy variables from factor columns
step_dummy(all_nominal()) %>%
# remove any columns with a single unique value
step_zv(all_predictors()) %>%
step_normalize(all_predictors())
```To enact a correlation filter, an additional step is used:
```{r filter}
filter_rec <-
base_recipe %>%
step_corr(all_of(stations), threshold = tune())
```Similarly, for PCA:
```{r pca}
pca_rec <-
base_recipe %>%
step_pca(all_of(stations), num_comp = tune()) %>%
step_normalize(all_predictors())
```We might want to assess a few different models, including a regularized method (`glmnet`):
```{r models}
regularized_spec <-
linear_reg(penalty = tune(), mixture = tune()) %>%
set_engine("glmnet")cart_spec <-
decision_tree(cost_complexity = tune(), min_n = tune()) %>%
set_engine("rpart") %>%
set_mode("regression")knn_spec <-
nearest_neighbor(neighbors = tune(), weight_func = tune()) %>%
set_engine("kknn") %>%
set_mode("regression")
```Rather than creating all 9 combinations of these preprocessors and models, we can create a _workflow set_:
```{r set}
chi_models <-
workflow_set(
preproc = list(
simple = base_recipe, filter = filter_rec,
pca = pca_rec
),
models = list(
glmnet = regularized_spec, cart = cart_spec,
knn = knn_spec
),
cross = TRUE
)
chi_models
```
It doesn't make sense to use PCA or a filter with a `glmnet` model. We can remove these easily:```{r rm}
chi_models <-
chi_models %>%
anti_join(tibble(wflow_id = c("pca_glmnet", "filter_glmnet")),
by = "wflow_id"
)
```These models all have tuning parameters. To resolve these, we'll need a resampling set. In this case, a time-series resampling method is used:
```{r rs}
splits <-
sliding_period(
Chicago,
date,
"day",
lookback = 300, # Each resample has 300 days for modeling
assess_stop = 7, # One week for performance assessment
step = 7 # Ensure non-overlapping weeks for assessment
)
splits
```We'll use simple grid search for these models by running `workflow_map()`. This will execute a resampling or tuning function over the workflows in the `workflow` column:
```{r tune}
set.seed(123)
chi_models <-
chi_models %>%
# The first argument is a function name from the {{tune}} package
# such as `tune_grid()`, `fit_resamples()`, etc.
workflow_map("tune_grid",
resamples = splits, grid = 10,
metrics = metric_set(mae), verbose = TRUE
)
chi_models
```The `results` column contains the results of each call to `tune_grid()` for the workflows.
The `autoplot()` method shows the rankings of the workflows:
```{r plot, fig.height = 4, dev = "svg"}
autoplot(chi_models)
```or the best from each workflow:
```{r plot-best, fig.height = 4, dev = "svg"}
autoplot(chi_models, select_best = TRUE)
```We can determine how well each combination did by looking at the best results per workflow:
```{r best}
rank_results(chi_models, rank_metric = "mae", select_best = TRUE) %>%
select(rank, mean, model, wflow_id, .config)
``````{r save, eval = FALSE, echo = FALSE}
save(chi_models, file = "data/chi_models.rda", compress = "bzip2", version = 2)
```## Contributing
This project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.
- For questions and discussions about tidymodels packages, modeling, and machine learning, please [post on Posit Community](https://forum.posit.co/new-topic?category_id=15&tags=tidymodels,question).
- If you think you have encountered a bug, please [submit an issue](https://github.com/tidymodels/workflowsets/issues).
- Either way, learn how to create and share a [reprex](https://reprex.tidyverse.org/articles/articles/learn-reprex.html) (a minimal, reproducible example), to clearly communicate about your code.
- Check out further details on [contributing guidelines for tidymodels packages](https://www.tidymodels.org/contribute/) and [how to get help](https://www.tidymodels.org/help/).