Machine Learning with tidymodels
30.1 The tidymodels workflow
library(tidymodels)
set.seed(123)
data <- penguins |> drop_na()
# 1. Split
split <- initial_split(data, prop = 0.8, strata = species)
train <- training(split); test <- testing(split)
folds <- vfold_cv(train, v = 5, strata = species)
# 2. Preprocess (recipe)
rec <- recipe(species ~ bill_length_mm + bill_depth_mm + flipper_length_mm + body_mass_g + sex, data = train) |>
step_dummy(all_nominal_predictors()) |>
step_normalize(all_numeric_predictors())
# 3. Model specification
rf_spec <- rand_forest(trees = 500, mtry = tune(), min_n = tune()) |>
set_engine("ranger", importance = "impurity") |>
set_mode("classification")
# 4. Workflow
wf <- workflow() |> add_recipe(rec) |> add_model(rf_spec)
# 5. Tune hyperparameters
tuned <- tune_grid(wf, resamples = folds, grid = 10, metrics = metric_set(accuracy, roc_auc))
show_best(tuned, metric = "roc_auc")
best <- select_best(tuned, metric = "roc_auc")
# 6. Finalize & evaluate on the test set
final <- finalize_workflow(wf, best) |> last_fit(split)
collect_metrics(final)
collect_predictions(final) |> conf_mat(species, .pred_class)
# 7. Use the model
model <- extract_workflow(final)
predict(model, new_data = head(test), type = "prob")30.2 Packages in tidymodels
splitting, cross-validation, bootstraps feature engineering (step_*: impute, dummy, normalize, PCA, log, interactions, text) unified model interface (engines: lm, glmnet, ranger, xgboost, lightgbm, kernlab, keras,...) bundle preprocessing + model tune / dials / finetune hyperparameter tuning (grid, Bayesian, racing) metrics (rmse, rsq, accuracy, roc_auc, f_meas,...) tidy model outputs compare many workflows model deployment & monitoring embed, textrecipes, themis encodings, text features, class imbalance
30.3 Common algorithms
linear_reg() |> set_engine("glmnet", penalty = 0.01)
logistic_reg()
decision_tree() |> set_engine("rpart")
rand_forest() |> set_engine("ranger")
boost_tree(trees = 1000, learn_rate = 0.05) |> set_engine("xgboost")
svm_rbf() |> set_engine("kernlab")
nearest_neighbor(neighbors = 5)
naive_Bayes() |> set_engine("naivebayes") # requires discrim
mlp() |> set_engine("brulee") # neural network (torch)30.4 Other ML ecosystems
caret — classic unified interface (train()), still widely used.
mlr3 — object-oriented ML framework (R6), pipelines, benchmarking.
h2o — scalable ML/AutoML.
torch / luz / keras3 — deep learning.
xgboost, lightgbm, ranger, e1071, randomForest — individual algorithm packages.
Interpretability: DALEX, vip, iml, shapviz, lime.