Introduction
Random forests build multiple decision trees and combine their predictions. They improve accuracy and reduce overfitting.
Building Random Forest
library(randomForest)
# Regression
rf <- randomForest(y ~ ., data = train, ntree = 100)
# Classification
rf <- randomForest(target ~ ., data = train, ntree = 100)
# Print summary
print(rf)
# Variable importance
importance(rf)
varImpPlot(rf)
Predictions
# Predict
pred <- predict(rf, test)
# For classification
pred <- predict(rf, test, type = "class")
pred <- predict(rf, test, type = "prob")
Tuning
# Tune parameters
tuneRF(train[, -target], train$target)
Using Caret
library(caret)
train(target ~ ., data = train, method = "rf")
Summary
Random forests are powerful and robust. Use them for high accuracy predictions.