Skip to Content
Course content

62: Random Forests with randomForest Package

Click on the "Edit" button in the top corner of the screen to edit your slide content.

If you've spent any time in the machine learning world, you've probably heard that Random Forests are the "reliable workhorse" of predictive modeling. They're harder to break than a single decision tree and usually require less preprocessing than a neural network. But because they are so forgiving, I see a lot of developers fall into a trap where they treat the randomForest package as a black box that magically produces a perfect model.

The trap of the "Perfect" Training Score

Let's say we're building a model to predict customer churn for a subscription service. You have a dataset with 10,000 rows and 20 features—things like monthly_spend, support_tickets_opened, and contract_length. The naive approach looks something like this:

library(randomForest)
# The naive way: training and testing on the same data
rf_model <- randomForest(churn ~ ., data = churn_data)
predictions <- predict(rf_model, churn_data)
confusionMatrix(predictions, churn_data$churn)

When you run this, you'll likely see an accuracy score that makes you feel like a genius—maybe 98% or 99%. The problem is that you're cheating. Random Forests are essentially a collection of deep decision trees. Because these trees can keep splitting until every single training observation is in its own leaf, the model can effectively "memorize" your data. When you test on the same data you trained on, you aren't measuring prediction; you're measuring memory.

Relying on Out-of-Bag Error and Validation Splits

To actually know if your model works, you need to separate your concerns. I always tell my team to look at the Out-of-Bag (OOB) error first. One of the coolest things about the randomForest package is that it does some of the work for you. Since each tree is built using a bootstrap sample (a random subset) of the data, there's always a portion of the data that a specific tree didn't see. This is the "out-of-bag" data.

Instead of trusting a training score, look at the OOB estimate provided in the model summary. Better yet, split your data properly. It costs you a bit of your training volume, but it saves you from deploying a model that crashes and burns the moment it hits real-world data.

# The professional way: Split and Validate
set.seed(42)
train_idx <- sample(1:nrow(churn_data), 0.7 * nrow(churn_data))
train_set <- churn_data[train_idx, ]
test_set <- churn_data[-train_idx, ]

# We set ntree to 500 as a starting point, but we keep an eye on the OOB error
rf_model <- randomForest(churn ~ ., data = train_set, ntree = 500, importance = TRUE)
print(rf_model) # Check the OOB estimate here!

# Now we test on data the model has NEVER seen
test_predictions <- predict(rf_model, test_set)

Tuning mtry vs. Computing Budget

Most people just use the default mtry value (which is usually the square root of the number of predictors for classification). While the defaults are decent, they aren't always optimal. mtry controls how many variables are randomly sampled as candidates at each split. If mtry is too high, your trees become too similar to each other, and you lose the benefit of the "forest"—you're basically just running the same tree 500 times.

If you find your OOB error is plateauing too early, I suggest experimenting with mtry. However, there's a trade-off: increasing the number of trees (ntree) and tuning mtry increases the memory footprint and CPU time. In a production environment, a model that takes 10 minutes to train but is 1% more accurate than one that takes 10 seconds is often a bad trade. I usually start with the defaults, check the importance() of the variables, and only tune mtry if the OOB error suggests the model is underfitting.




📋 Practical Task

Predicting Loan Defaults with Optimized Feature Sampling

You have been handed a dataset called loan_data containing information on past borrowers (features include credit_score, annual_income, loan_amount, and employment_years) and a binary target variable default_status (Yes/No).

Your task is to implement a Random Forest model that avoids the "naive" pitfalls discussed in the lesson. Please perform the following steps:

  • Split the loan_data into a 70% training set and a 30% testing set.
  • Train a randomForest model on the training set, ensuring you enable the importance = TRUE flag.
  • Extract and print the Out-of-Bag (OOB) error rate from the model object.
  • Generate predictions for the test set and calculate the final accuracy by comparing the predictions to the actual default_status.
  • Use the importance() function to identify which of the four features is the strongest predictor of loan default.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.