← Back to Model Library

Random Forest Model

Ensemble Learning for Regression

Overview

Random Forest is a popular ensemble learning method that can be used for both classification and regression tasks. For time series forecasting, it operates by constructing a multitude of decision trees at training time and outputting the mean (for regression) or mode (for classification) prediction of the individual trees. While Random Forests are not inherently designed for time series data (as they assume independent observations), they can be effectively adapted by transforming the time series problem into a supervised learning problem through **feature engineering**.

Architecture & Components

Random Forest builds upon two main concepts: decision trees and bagging (bootstrap aggregating):

  • Decision Trees: These are the weak learners in the ensemble. A decision tree recursively partitions the input space based on features to make predictions.
  • Bagging (Bootstrap Aggregating): This technique involves training multiple decision trees on different bootstrap samples (random subsets with replacement) of the training data. This reduces variance and helps prevent overfitting.
  • Random Feature Subspace: During the construction of each tree, only a random subset of features is considered for splitting at each node. This further decorrelates the trees, improving the ensemble's robustness.
  • Aggregation: For regression, the predictions from all individual trees are averaged to produce the final forecast.
  • Feature Engineering: For time series forecasting, Random Forest relies on manually engineered features to capture temporal patterns. These typically include:
    • Lagged Features: Past values of the time series itself (e.g., sales from previous days). [13, 14]
    • Rolling Window Statistics: Mean, standard deviation, min, max over a defined past window.
    • Time-Based Features: Day of week, month, year, hour, quarter, and holiday indicators. [15]
    • Seasonal Variables: Creating variables that have different values for different months or weeks to add a seasonal component. [15]
    • Decomposition: Explicitly decomposing the time series into trend, seasonality, and residuals, and then using these components as features. [16]
Random Forest Architecture Diagram

Conceptual diagram of Random Forest's ensemble approach for time series.

When to Use Random Forest

Random Forest can be a good choice for time series forecasting when:

  • You are comfortable with feature engineering: Its effectiveness in time series relies on creating relevant temporal features.
  • The time series exhibits non-linear relationships: Tree-based models can capture complex interactions between features.
  • Robustness to outliers and noise is important: The ensemble nature makes it less sensitive to extreme values.
  • You need feature importance scores: Random Forest can provide insights into which features are most influential.
  • You are dealing with intermittent data: It can perform well for data with many zero values. [15]
  • You need a model that is less prone to overfitting than single decision trees.

Pros and Cons

Pros

  • Robust to Outliers & Noise: Ensemble averaging reduces the impact of individual noisy data points.
  • Handles Non-Linear Relationships: Can model complex interactions between features.
  • Provides Feature Importance: Offers insights into which features are most predictive.
  • Less Prone to Overfitting: Bagging and random feature selection help prevent overfitting compared to single decision trees.
  • Versatile: Can be used for both univariate and multivariate time series.

Cons

  • Requires Feature Engineering: Not a native time series model; requires manual creation of lagged, rolling, and time-based features. [15]
  • Struggles with Extrapolation: As a tree-based model, it cannot predict values outside the range seen in the training data, making it unsuitable for strong trends that extend beyond observed data. [16]
  • Less Interpretable: Ensemble of many trees makes it harder to interpret than a single decision tree.
  • Computationally Intensive: Can be expensive for very large datasets due to building many trees.
  • Does Not Explicitly Handle Temporal Dependencies: Assumes observations are independent, requiring careful feature engineering to capture time-dependent structures. [17]

Example Implementation

Here's an example of implementing Random Forest for time series forecasting in Python. The key steps involve transforming the time series into a supervised learning problem with lagged features, splitting data chronologically, and then training and predicting with `RandomForestRegressor`.

Python Example (using `scikit-learn` library)

                        
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error
import matplotlib.pyplot as plt

# 1. Create sample time series data
date_range = pd.date_range(start='2020-01-01', periods=300, freq='D')
# Simulate data with trend, seasonality, and noise
values = (100 + np.arange(300) * 0.5 + # Trend
          20 * np.sin(np.arange(300) * 2 * np.pi / 30) + # Monthly seasonality
          np.random.randn(300) * 5) # Noise
df = pd.DataFrame({'date': date_range, 'value': values})

# 2. Feature Engineering (Transform time series into supervised learning problem) [13, 14]
# Create lagged features
def create_lagged_features(df, lag_steps):
    for i in range(1, lag_steps + 1):
        df[f'lag_{i}'] = df['value'].shift(i)
    return df

df = create_lagged_features(df.copy(), 7) # Use last 7 days as features

# Add time-based features
df['day_of_week'] = df['date'].dt.dayofweek
df['month'] = df['date'].dt.month
df['year'] = df['date'].dt.year

# Drop rows with NaN values created by lagging
df = df.dropna()

# 3. Splitting Data (Chronological Split)
split_date = '2020-09-01'
train = df[df['date'] < split_date]
test = df[df['date'] >= split_date]

features = [col for col in df.columns if col not in ['date', 'value']]
target = 'value'

X_train, y_train = train[features], train[target]
X_test, y_test = test[features], test[target]

# 4. Create and Train Random Forest Model [13]
# n_estimators: number of trees in the forest
reg = RandomForestRegressor(n_estimators=100, random_state=42, max_depth=10, min_samples_leaf=5)
reg.fit(X_train, y_train)

# 5. Make Predictions
predictions = reg.predict(X_test)

# 6. Evaluate Model Performance
mae = mean_absolute_error(y_test, predictions)
rmse = np.sqrt(mean_squared_error(y_test, predictions))
print(f"MAE: {mae:.3f}")
print(f"RMSE: {rmse:.3f}")

# 7. Plotting Results
plt.figure(figsize=(14, 7))
plt.plot(train['date'], train['value'], label='Training Data', color='blue')
plt.plot(test['date'], y_test, label='Actual Test Data', color='orange')
plt.plot(test['date'], predictions, label='Random Forest Predictions', color='green', linestyle='--')
plt.title('Random Forest Time Series Forecasting')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.grid(True)
plt.show()
                        

Dependencies & Resources

Dependencies: pandas, numpy, scikit-learn, matplotlib (for plotting).