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/unsupervised.md
View on GitHub# Unsupervised Methods
PCA via recipes, UMAP via uwot, and k-means clustering via base R. tidymodels
is primarily a supervised learning framework; unsupervised methods are either
integrated into recipes (PCA) or use standalone R packages (uwot, stats::kmeans).
## PCA via recipes::step_pca
PCA can be added as a recipe step for dimension reduction as part of a supervised
pipeline, or used standalone for exploratory analysis.
### PCA in a Supervised Pipeline
```r
library(tidymodels)
rec <- recipe(outcome ~ ., data = train_data) |>
step_normalize(all_numeric_predictors()) |> # Always scale before PCA
step_pca(all_numeric_predictors(), num_comp = 5)
# In a workflow, PCA is applied as preprocessing before the model
wf <- workflow() |>
add_recipe(rec) |>
add_model(spec)
```
### PCA for Exploration (Standalone)
```r
library(tidymodels)
# Define recipe with PCA
rec <- recipe(~ ., data = df) |> # No outcome -- unsupervised
step_normalize(all_numeric()) |>
step_pca(all_numeric(), num_comp = 5)
# Prep and bake
prepped <- prep(rec, training = df)
pca_results <- bake(prepped, new_data = NULL)
# Inspect variance explained
tidy(prepped, number = 2, type = "variance")
# Returns: terms, value, component columns
# terms "variance" = eigenvalue
# terms "cumulative percent variance" = cumulative explained variance
```
### PCA Parameters
| Parameter | Purpose | Default |
|-----------|---------|---------|
| `num_comp` | Number of components to retain | 5 |
| `threshold` | Retain components until cumulative variance >= threshold | (not used by default) |
| `prefix` | Column name prefix for components | "PC" |
```r
# Keep enough components for 95% variance
rec <- recipe(~ ., data = df) |>
step_normalize(all_numeric()) |>
step_pca(all_numeric(), threshold = 0.95)
```
### PCA Loadings
```r
prepped <- prep(rec, training = df)
# Get loadings (component weights)
loadings <- tidy(prepped, number = 2) # number = PCA step index
# Returns tibble: terms (variable), value (loading), component (PC1, PC2, ...)
```
## UMAP via uwot
UMAP (Uniform Manifold Approximation and Projection) is not part of tidymodels.
Use the `uwot` package directly. UMAP is for **visualization only** -- do not
use UMAP distances for quantitative analysis.
### Basic UMAP
```r
library(uwot)
# Input: numeric matrix or data frame (scaled)
X_scaled <- scale(df[, numeric_cols])
set.seed(42)
umap_result <- umap(X_scaled, n_components = 2, n_neighbors = 15, min_dist = 0.1)
# umap_result is a matrix with n_components columns
umap_df <- data.frame(
UMAP1 = umap_result[, 1],
UMAP2 = umap_result[, 2]
)
```
### UMAP Parameters
| Parameter | Purpose | Default | Guidance |
|-----------|---------|---------|----------|
| `n_components` | Output dimensions | 2 | Use 2 for visualization |
| `n_neighbors` | Local neighborhood size | 15 | Smaller = more local structure |
| `min_dist` | Minimum distance in embedding | 0.01 | Smaller = tighter clusters |
| `metric` | Distance metric | "euclidean" | "cosine" for text/sparse |
| `n_epochs` | Training iterations | NULL (auto) | More = better quality |
| `scale` | Scale input | FALSE | Pre-scale with `scale()` instead |
### UMAP Visualization
```r
library(ggplot2)
umap_df$label <- df$group_variable
ggplot(umap_df, aes(x = UMAP1, y = UMAP2, color = label)) +
geom_point(alpha = 0.6, size = 1.5) +
theme_minimal() +
labs(title = "UMAP Embedding")
```
### UMAP Caveats
- **Visualization only**: UMAP distorts distances to preserve local neighborhoods.
Cluster sizes and between-cluster gaps in the plot may not be meaningful
(McInnes, Healy & Melville, 2018, Section 6).
- **Parameter sensitivity**: Results change with `n_neighbors` and `min_dist`.
Run multiple parameter values and report sensitivity.
- **Reproducibility**: Always set `set.seed()` before calling `umap()`.
- **Scaling**: Always scale numeric features before UMAP (use `scale()`).
## K-Means Clustering via stats::kmeans
Base R's `kmeans()` is the standard k-means implementation. tidymodels does not
provide its own clustering API.
### Basic K-Means
```r
# Scale features first
X_scaled <- scale(df[, numeric_cols])
set.seed(42)
km <- kmeans(X_scaled, centers = 4, nstart = 25)
# Results
km$cluster # Cluster assignment for each row
km$centers # Cluster centroids (in scaled space)
km$tot.withinss # Total within-cluster sum of squares
km$withinss # Within-SS per cluster
km$size # Number of observations per cluster
```
### Choosing k (Elbow Method)
```r
wss <- sapply(2:15, function(k) {
set.seed(42)
kmeans(X_scaled, centers = k, nstart = 25)$tot.withinss
})
plot(2:15, wss, type = "b", xlab = "k", ylab = "Total within-SS",
main = "Elbow Plot")
```
### Silhouette Score
```r
library(cluster)
km <- kmeans(X_scaled, centers = 4, nstart = 25)
sil <- silhouette(km$cluster, dist(X_scaled))
mean(sil[, "sil_width"]) # Average silhouette width
```
### K-Means Parameters
| Parameter | Purpose | Recommended |
|-----------|---------|-------------|
| `centers` | Number of clusters (k) | Determine via elbow/silhouette |
| `nstart` | Number of random initializations | 25+ |
| `iter.max` | Maximum iterations | 300 (increase if convergence warning) |
| `algorithm` | Algorithm variant | "Hartigan-Wong" (default) |
## When to Use Each Method
| Method | Purpose | Within tidymodels? |
|--------|---------|-------------------|
| PCA (step_pca) | Dimension reduction as preprocessing | Yes (recipe step) |
| PCA (standalone) | Exploratory variance analysis | Yes (prep + bake) |
| UMAP (uwot) | Nonlinear visualization of high-dim data | No (standalone) |
| K-means (stats) | Partition data into k groups | No (standalone) |
For methodology guidance on when to use clustering, PCA, or UMAP, consult
the `data-scientist` skill's `exploratory-unsupervised.md` reference; its
References section gives full citations for works cited here.
## Quick Reference
| Task | Code |
|------|------|
| PCA in recipe | `step_pca(all_numeric_predictors(), num_comp = 5)` |
| PCA threshold | `step_pca(all_numeric(), threshold = 0.95)` |
| PCA loadings | `tidy(prepped_rec, number = pca_step_index)` |
| PCA variance | `tidy(prepped_rec, number = pca_step_index, type = "variance")` |
| UMAP | `uwot::umap(X_scaled, n_components = 2)` |
| K-means | `kmeans(X_scaled, centers = k, nstart = 25)` |
| Silhouette | `cluster::silhouette(km$cluster, dist(X))` |
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: 321k | 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 |