Search⌘ K
AI Features

Random Forests with tidymodels

Explore how to train and evaluate random forest models using the tidymodels workflow in R. Understand the role of setting seeds for reproducibility, the impact on model comparisons, and the risks of overfitting to seed values. Learn to interpret model performance metrics such as error rates and confusion matrices.

We'll cover the following...

Training a random forest

One advantage of the workflow approach of tidymodels is the ability to change the machine learning algorithm quickly. The following code compares using a CART classification tree vs. a random classification forest.

R
#================================================================================================
# Specify a CART classification tree
#
titanic_model <- decision_tree() %>%
set_engine("rpart") %>%
set_mode("classification")
#================================================================================================
# Specify a random classification forest
#
titanic_model <- rand_forest(trees = 500) %>%
set_engine("randomForest") %>%
set_mode("classification")

The code above uses the rand_forest() function from the parsnip package to specify using the random forest algorithm with default hyperparameter values. The code further specifies using the randomForest package’s algorithm to train a random classification forest via the ...