tidymodels-workflow
Tidymodels workflow patterns with recipes, models, workflows, resampling, tuning, and final evaluation.
What this skill does
# tidymodels Workflow Patterns
## Overview
Core workflow patterns for building machine learning models using the tidymodels ecosystem. Covers the complete pipeline from data splitting through model deployment.
## Core Workflow Components
### Data Splitting with rsample
```r
library(tidymodels)
# Basic train/test split
set.seed(123)
data_split <- initial_split(data, prop = 0.75, strata = outcome)
train_data <- training(data_split)
test_data <- testing(data_split)
# Validation set approach
data_split <- initial_validation_split(data, prop = c(0.6, 0.2))
train_data <- training(data_split)
val_data <- validation(data_split)
test_data <- testing(data_split)
```
### Recipe Creation
```r
# Create preprocessing recipe
recipe_spec <- recipe(outcome ~ ., data = train_data) |>
step_normalize(all_numeric_predictors()) |>
step_dummy(all_nominal_predictors()) |>
step_zv(all_predictors())
```
### Model Specification with parsnip
```r
# Specify model with tune placeholders
model_spec <- rand_forest(
mtry = tune(),
trees = 1000,
min_n = tune()
) |>
set_engine("ranger") |>
set_mode("classification")
```
### Workflow Assembly
```r
# Combine recipe and model
workflow_spec <- workflow() |>
add_recipe(recipe_spec) |>
add_model(model_spec)
```
### Resampling Setup
```r
# Cross-validation folds
cv_folds <- vfold_cv(train_data, v = 10, strata = outcome)
# Bootstrap samples
boot_samples <- bootstraps(train_data, times = 25)
```
### Hyperparameter Tuning
```r
# Define tuning grid
tune_grid <- grid_regular(
mtry(range = c(2, 10)),
min_n(range = c(2, 20)),
levels = 5
)
# Tune model
tune_results <- workflow_spec |>
tune_grid(
resamples = cv_folds,
grid = tune_grid,
metrics = metric_set(roc_auc, accuracy)
)
```
### Model Selection
```r
# Select best parameters
best_params <- select_best(tune_results, metric = "roc_auc")
# Finalize workflow
final_workflow <- workflow_spec |>
finalize_workflow(best_params)
```
### Final Fit
```r
# Fit on full training data, evaluate on test
final_fit <- final_workflow |>
last_fit(data_split)
# Extract metrics
collect_metrics(final_fit)
# Extract predictions
collect_predictions(final_fit)
```
### Model Extraction and Deployment
```r
# Extract fitted workflow
fitted_wf <- extract_workflow(final_fit)
# Save model
saveRDS(fitted_wf, "output/models/final_model.rds")
# Predict on new data
predictions <- predict(fitted_wf, new_data)
```
## Complete Workflow Example
```r
library(tidymodels)
tidymodels_prefer()
# 1. Load and split data
set.seed(123)
data_split <- initial_split(ames, prop = 0.75, strata = Sale_Price)
# 2. Create recipe
ames_recipe <- recipe(Sale_Price ~ ., data = training(data_split)) |>
step_log(Sale_Price, base = 10) |>
step_other(Neighborhood, threshold = 0.05) |>
step_dummy(all_nominal_predictors()) |>
step_normalize(all_numeric_predictors()) |>
step_zv(all_predictors())
# 3. Specify model
xgb_spec <- boost_tree(
trees = tune(),
tree_depth = tune(),
learn_rate = tune()
) |>
set_engine("xgboost") |>
set_mode("regression")
# 4. Create workflow
xgb_wf <- workflow(ames_recipe, xgb_spec)
# 5. Setup resampling
cv_folds <- vfold_cv(training(data_split), v = 5)
# 6. Tune hyperparameters
xgb_tune <- xgb_wf |>
tune_grid(
resamples = cv_folds,
grid = 20,
metrics = metric_set(rmse, rsq)
)
# 7. Select best and finalize
best_xgb <- select_best(xgb_tune, metric = "rmse")
final_wf <- finalize_workflow(xgb_wf, best_xgb)
# 8. Final evaluation
final_fit <- last_fit(final_wf, data_split)
collect_metrics(final_fit)
```
## Workflow Sets for Model Comparison
```r
# Create multiple preprocessing recipes
basic_recipe <- recipe(outcome ~ ., data = train) |>
step_normalize(all_numeric_predictors())
pca_recipe <- basic_recipe |>
step_pca(all_numeric_predictors(), num_comp = 5)
# Create multiple model specifications
lm_spec <- linear_reg() |> set_engine("lm")
rf_spec <- rand_forest(trees = 500) |> set_engine("ranger") |> set_mode("regression")
xgb_spec <- boost_tree() |> set_engine("xgboost") |> set_mode("regression")
# Create workflow set
wf_set <- workflow_set(
preproc = list(basic = basic_recipe, pca = pca_recipe),
models = list(lm = lm_spec, rf = rf_spec, xgb = xgb_spec)
)
# Fit all workflows
wf_results <- wf_set |>
workflow_map(
resamples = cv_folds,
grid = 10,
verbose = TRUE
)
# Compare results
autoplot(wf_results)
rank_results(wf_results, rank_metric = "rmse")
```
## Key Packages
- **rsample**: Data splitting and resampling
- **recipes**: Feature engineering
- **parsnip**: Model specification
- **workflows**: Combine preprocessing and models
- **tune**: Hyperparameter optimization
- **yardstick**: Model evaluation metrics
- **workflowsets**: Compare multiple workflows
- **broom**: Tidy model outputs
## Best Practices
1. Always set a seed before splitting data
2. Use stratified sampling for imbalanced outcomes
3. Keep test data completely separate until final evaluation
4. Use cross-validation for honest performance estimates
5. Tune hyperparameters on training data only
6. Use `last_fit()` for final evaluation on test set
7. Save the complete workflow object for deployment
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.