Random Forest Regression in R: Build, Tune, and Evaluate Predictive Models

Random Forest Regression is one of the most powerful machine learning techniques for predicting continuous numerical outcomes. It combines hundreds of decision trees to generate accurate and robust predictions while minimizing overfitting.

Unlike traditional linear regression, Random Forest can model complex nonlinear relationships and interactions among variables without requiring strict statistical assumptions.

This tutorial demonstrates how to build a Random Forest regression model in R, optimize model parameters, evaluate performance, identify the most influential predictors, and generate predictions.

What Is Random Forest Regression?

Random Forest Regression is an ensemble learning algorithm that constructs many decision trees using bootstrap samples of the training data.

For regression tasks, the prediction from each tree is averaged to produce the final prediction.

Compared with a single decision tree, Random Forest generally provides:

  • Higher prediction accuracy
  • Better generalization
  • Lower variance
  • Greater robustness to noisy data

When Should You Use Random Forest Regression?

Random Forest Regression is suitable when:

  • The response variable is continuous.
  • Relationships between variables are nonlinear.
  • Predictor variables interact with one another.
  • High predictive accuracy is desired.
  • Feature importance is needed.
  • Traditional regression assumptions are violated.

Typical applications include:

  • House price prediction
  • Stock price forecasting
  • Demand forecasting
  • Customer lifetime value estimation
  • Sales prediction
  • Environmental modeling
  • Healthcare analytics

Example Dataset

Suppose we want to predict a continuous variable called Likeability using six predictor variables:

  • Attribute1
  • Attribute2
  • Attribute3
  • Attribute4
  • Attribute5
  • Attribute6

Our objective is to understand which attributes influence Likeability and build a model capable of predicting future values.

Load the Data

Assume the dataset is stored in an Excel workbook.

library(openxlsx)

data <- read.xlsx(
"D:/rawdata.xlsx",
sheet = "Sheet1"
)

Split the Dataset

Machine learning models should always be evaluated using unseen observations.

A common practice is an 80:20 train-test split.

set.seed(123)

train_index <- sample(
seq_len(nrow(data)),
size = 0.8 * nrow(data)
)

train_data <- data[train_index, ]

test_data <- data[-train_index, ]

Random sampling provides a more representative split than selecting rows sequentially.

Load the Random Forest Package

library(randomForest)

Find the Optimal mtry Value

The mtry parameter specifies the number of predictor variables randomly selected at each tree split.

The tuneRF() function helps identify an appropriate value.

best_mtry <- tuneRF(

train_data[, -ncol(train_data)],

train_data$Likeability,

stepFactor = 1.5,

improve = 0.01,

trace = TRUE

)

Example output:

mtry = 2  OOB Error = 3.42

mtry = 3 OOB Error = 2.91

mtry = 4 OOB Error = 2.61

mtry = 5 OOB Error = 2.58

Select the value with the lowest Out-of-Bag (OOB) error.

Build the Random Forest Model

Train the regression model.

rf_model <- randomForest(

Likeability ~ .,

data = train_data,

ntree = 500,

mtry = 4,

importance = TRUE

)

View the Model

print(rf_model)

Example output:

Type of random forest:
Regression

Number of trees:
500

Number of variables tried at each split:
4

Mean of squared residuals:
2.00

% Var explained:
78.6

Interpreting the Results

The output provides several useful statistics.

Number of Trees

More trees generally improve stability, although gains diminish beyond a certain point.

Mean Squared Residuals

Lower values indicate better predictive accuracy.

Percentage of Variance Explained

The percentage of variance explained is similar in interpretation to the coefficient of determination (R²).

For example:

Variance Explained = 78.6%

This means the model explains approximately 79% of the variation in the response variable.

What Is Out-of-Bag (OOB) Error?

Random Forest automatically estimates prediction error using observations not included in each tree’s bootstrap sample.

Advantages include:

  • No separate validation dataset required
  • Efficient model evaluation
  • Helps tune hyperparameters

Lower OOB error generally indicates better model performance.

Feature Importance

One of Random Forest’s greatest strengths is estimating predictor importance.

Plot feature importance.

varImpPlot(rf_model)

Or obtain numerical values.

importance(rf_model)

Variables with larger importance values contribute more strongly to prediction accuracy.

Feature Selection Using Boruta

The Boruta package identifies statistically important variables.

library(Boruta)

important <- Boruta(

Likeability ~ .,

data = train_data

)

print(important)

Example output:

Confirmed Important

Attribute2

Attribute3

Attribute4

Attribute6

Rejected

Attribute1

Attribute5

Boruta compares each predictor with randomized “shadow” features to determine whether its importance exceeds what would be expected by chance.

Make Predictions

Predict the response variable for new observations.

predictions <- predict(

rf_model,

newdata = test_data
)

Display the predictions.

predictions

Evaluate Prediction Accuracy

Compare predicted values with actual observations.

results <- data.frame(

Actual = test_data$Likeability,

Predicted = predictions

)

results

Common regression evaluation metrics include:

  • Mean Absolute Error (MAE)
  • Mean Squared Error (MSE)
  • Root Mean Squared Error (RMSE)
  • R-squared (R²)

Example RMSE calculation:

rmse <- sqrt(

mean(

(results$Actual - results$Predicted)^2

)

)

rmse

Improving Model Performance

If prediction accuracy is lower than expected, consider the following:

  • Increase the number of observations.
  • Collect more informative predictor variables.
  • Tune mtry, ntree, and nodesize.
  • Remove noisy or irrelevant features.
  • Handle missing values appropriately.
  • Detect and address outliers.
  • Perform feature engineering.
  • Use cross-validation for hyperparameter tuning.

While larger datasets often improve model stability, there is no universal minimum sample size such as 100 observations. The required sample size depends on the complexity of the problem, the number of predictors, and the variability in the data.

Applications of Random Forest Regression

Random Forest Regression is widely used for:

  • Real estate price prediction
  • Sales forecasting
  • Financial risk modeling
  • Demand forecasting
  • Crop yield prediction
  • Energy consumption forecasting
  • Healthcare outcome prediction
  • Manufacturing quality prediction
  • Insurance claim estimation
  • Customer lifetime value prediction

Advantages

Random Forest Regression offers several benefits:

  • Handles nonlinear relationships automatically.
  • Robust to overfitting.
  • Works well with large numbers of predictors.
  • Requires minimal data preprocessing.
  • Handles interactions between variables.
  • Estimates feature importance.
  • Performs well on complex datasets.

Limitations

Despite its strengths, Random Forest has some drawbacks:

  • Less interpretable than linear regression.
  • Computationally intensive for very large datasets.
  • Does not extrapolate well beyond the range of training data.
  • Hyperparameter tuning may be necessary for optimal performance.

Conclusion

Random Forest Regression is a versatile and highly effective machine learning algorithm for predicting continuous outcomes. By averaging predictions from multiple decision trees, it delivers strong predictive performance while reducing overfitting and capturing complex nonlinear relationships.

In R, the randomForest package makes it straightforward to train regression models, evaluate performance using Out-of-Bag error and regression metrics, assess feature importance, and generate predictions for new data. Combining Random Forest with feature selection techniques such as Boruta and careful hyperparameter tuning can further improve model accuracy and help identify the variables that contribute most to predictive performance.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

eight − two =