R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
62: Random Forests with randomForest Package
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_datainto a 70% training set and a 30% testing set. - Train a
randomForestmodel on the training set, ensuring you enable theimportance = TRUEflag. - 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.
There are no comments for now.