Learn/R Programming/Machine Learning

Random Forest

Topic: Ensemble

Advertisement

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.

Advertisement

Advertisement

Need More Practice?

Get personalized R programming help from ChatWhole's AI-powered platform.

Get Expert Help →