H. Sherry Zhang
Department of Statistics and Data Sciences
University of Texas at Austin
library(usethis) create_from_github("SDS322E-26FALL/1301-ml3", fork = FALSE)
So far, for your group’s question:
In Decision trees and random forest (Nov 11), we tuned a random forest for the penguins.
Today: build a tuned random forest for your group’s question.
The same workflow as the penguins example (trees.R):
Step 0: Training-testing split
Step 1: Specify the model, with mtry and min_n left to tune
Step 2: Specify the pre-processing recipe
Step 3: Build a workflow
Step 4: Fit the model
mtry and min_n and refit on the whole training dataStep 5/6: Predict and calculate the metrics on the testing data
| Response | Step 1 | Step 6 |
|---|---|---|
| numeric | rand_forest(mode = "regression", trees = 500, mtry = tune(), min_n = tune()) |
rmse, rsq |
| binary | rand_forest(mode = "classification", trees = 500, mtry = tune(), min_n = tune()) |
accuracy, roc_auc |
set_engine("ranger"), as in the lecturemtry: from 1 to \(p\), the number of predictors in your recipe. Check \(p\) with prep() and bake()min_n: e.g. from 2 to 20trees: don’t tune it, 500 is plentyTuning fits (number of combinations) \(\times\) (number of folds) forests, each with 500 trees. This adds up quickly: 6 values of mtry \(\times\) 4 values of min_n \(\times\) 10 folds = 240 forests = 120,000 trees
Start small, check that everything works, then scale up:
system.time(fit(rf_wf_fixed, data = ..._train)) with fixed values, e.g. mtry = 3, min_n = 10levels = c(3, 2) and vfold_cv(v = 5)slice_sample(n = 5000)set.seed() before the folds and before tune_grid(), so the results are reproduciblesaveRDS(rf_tune_res, "rf_tune_res.rds"), so you don’t have to rerun themIn your group, use your question from part I and build a tuned random forest. Share at the end:
autoplot()) and the best mtry and min_nThe script ml-practice-3.R in the repository loads the four datasets and has the steps as comments. The penguins script from Nov 11 (trees.R) has all the code you need.