DAAF-Contribution-Community/daaf / .claude/skills/tidymodels
tidymodels
VerifiedR machine learning with tidymodels: recipes (preprocessing), parsnip (model specs), workflows (fit pipelines), tune (hyperparameters), rsample (resampling). Engines: ranger (RF), glmnet (lasso/ridge), xgboost, kknn. Model interpretation (permutation importance, PDP/ICE, SHAP via DALEX/iml/kernelshap) and fairness assessment (fairmodels). UMAP via uwot. Use when execution language is R. Python equivalent: scikit-learn. For econometric regression use fixest or r-stats.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 19.6 KB |
| references/engines.md | markdown | 9.3 KB |
| references/evaluation.md | markdown | 6.2 KB |
| references/fairness.md | markdown | 7.1 KB |
| references/gotchas.md | markdown | 9.6 KB |
| references/interpretation.md | markdown | 9.5 KB |
| references/models.md | markdown | 7.8 KB |
| references/quickstart.md | markdown | 5.4 KB |
| references/recipes.md | markdown | 7.2 KB |
| references/resampling.md | markdown | 7.2 KB |
| references/tuning.md | markdown | 7.4 KB |
| references/unsupervised.md | markdown | 6.3 KB |
| references/workflows.md | markdown | 5.2 KB |
references/quickstart.md
View on GitHub# Quickstart
The complete tidymodels workflow: recipe, model specification, workflow, fit,
predict. Covers the fundamental paradigm difference from scikit-learn and shows
end-to-end examples for both classification and regression.
## Installation
No installation needed in DAAF: `tidymodels`, all four engine packages
(`ranger`, `glmnet`, `xgboost`, `kknn`), and `uwot` (UMAP) are pre-installed
in the container. Runtime installs are blocked in DAAF (`install.packages()`
is refused both at the command line and inside executed scripts — see
CLAUDE.md § Runtime Package Installation). If a package beyond these is
needed, escalate to the user to add it to the Dockerfile (user additions
block near the end) and rebuild.
### Verify Installation
```r
library(tidymodels)
cat("tidymodels version:", as.character(packageVersion("tidymodels")), "\n")
cat("recipes version:", as.character(packageVersion("recipes")), "\n")
cat("parsnip version:", as.character(packageVersion("parsnip")), "\n")
```
## Core Concept: The Recipe/Workflow Paradigm
tidymodels separates ML into three distinct objects:
| Object | Purpose | scikit-learn Equivalent |
|--------|---------|------------------------|
| **Recipe** | Preprocessing specification | `ColumnTransformer` + scalers/encoders |
| **Model spec** (parsnip) | Algorithm + engine + mode | `RandomForestClassifier(...)` |
| **Workflow** | Bundles recipe + model | `Pipeline([...])` |
Key difference from scikit-learn: in scikit-learn, preprocessing steps are
positional inside a Pipeline. In tidymodels, the recipe is a self-contained
specification that knows which columns to transform based on roles and selectors,
not position.
## Train/Test Split
```r
library(tidymodels)
# --- Basic split ---
set.seed(42)
split <- initial_split(data, prop = 0.8)
train_data <- training(split)
test_data <- testing(split)
# --- Stratified split (preserves outcome distribution) ---
set.seed(42)
split <- initial_split(data, prop = 0.8, strata = outcome)
train_data <- training(split)
test_data <- testing(split)
```
## Supervised Example: Classification (End-to-End)
```r
library(tidymodels)
# --- Data ---
set.seed(42)
split <- initial_split(iris, prop = 0.8, strata = Species)
train_data <- training(split)
test_data <- testing(split)
# --- 1. Recipe ---
rec <- recipe(Species ~ ., data = train_data) |>
step_normalize(all_numeric_predictors())
# --- 2. Model spec ---
spec <- rand_forest(trees = 500) |>
set_engine("ranger") |>
set_mode("classification")
# --- 3. Workflow ---
wf <- workflow() |>
add_recipe(rec) |>
add_model(spec)
# --- 4. Fit ---
fit <- wf |> fit(data = train_data)
# --- 5. Predict + evaluate ---
preds <- fit |> augment(new_data = test_data)
# augment() returns the test data with .pred_class and .pred_{level} columns
accuracy(preds, truth = Species, estimate = .pred_class)
```
## Supervised Example: Regression (End-to-End)
```r
library(tidymodels)
# --- Data ---
set.seed(42)
split <- initial_split(mtcars, prop = 0.8, strata = mpg)
train_data <- training(split)
test_data <- testing(split)
# --- 1. Recipe ---
rec <- recipe(mpg ~ ., data = train_data) |>
step_normalize(all_numeric_predictors())
# --- 2. Model spec ---
spec <- linear_reg(penalty = 0.01, mixture = 1) |>
set_engine("glmnet") |>
set_mode("regression")
# --- 3. Workflow ---
wf <- workflow() |>
add_recipe(rec) |>
add_model(spec)
# --- 4. Fit ---
fit <- wf |> fit(data = train_data)
# --- 5. Predict + evaluate ---
preds <- fit |> augment(new_data = test_data)
rmse(preds, truth = mpg, estimate = .pred)
rsq(preds, truth = mpg, estimate = .pred)
```
## The augment() Pattern
`augment()` is the preferred way to get predictions in tidymodels. It returns
the original data with prediction columns appended:
| Column | When | Content |
|--------|------|---------|
| `.pred` | regression | Numeric prediction |
| `.pred_class` | classification | Predicted class factor |
| `.pred_{level}` | classification | Probability for each class level |
```r
# augment returns a tibble with predictions joined to the data
results <- fit |> augment(new_data = test_data)
# For classification, you get both class predictions and probabilities
# results$.pred_class -- predicted class
# results$.pred_setosa -- probability of setosa
# results$.pred_versicolor -- etc.
```
## Reproducibility with set.seed()
R uses `set.seed()` for reproducibility. Set it before any random operation:
```r
set.seed(42)
split <- initial_split(data, prop = 0.8)
set.seed(42)
folds <- vfold_cv(train_data, v = 10)
```
Unlike scikit-learn where `random_state` is per-estimator, R's `set.seed()` is
global. Set it once before each random operation for reproducibility.
## Data Format: tibbles and data frames
tidymodels works with R data frames and tibbles. Unlike scikit-learn (which
often requires numeric matrices), tidymodels handles factors and characters
natively -- the recipe does the encoding.
```r
# Factors are fine -- recipe handles encoding
df <- tibble(
outcome = factor(c("yes", "no", "yes", "no")),
feature1 = c(1.0, 2.0, 3.0, 4.0),
category = factor(c("a", "b", "a", "b"))
)
# Recipe will encode 'category' via step_dummy()
rec <- recipe(outcome ~ ., data = df) |>
step_dummy(all_nominal_predictors())
```
## Next Steps
- Need preprocessing steps? See `recipes.md`
- Need to choose a model? See `models.md`
- Need cross-validation? See `resampling.md`
- Need hyperparameter tuning? See `tuning.md`
- Coming from scikit-learn or caret? See `gotchas.md`
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |