# Can a random forest learn time series correlations?

We have a data set with a set of N engineered features in each row. These features are actually a time series.

I am being asked to use them to train a random-forest-type model for classification. However no effort is being made to treat the features as a time series such as account for correlations or seasonality. The model formula is just identical to that of an additive linear model: y ~ b0 + b1 + b2...b120

The lead insists that the random forest, because it learns complex interactions, will learn the appropriate correlations foe the time series. Is this right?

Random Forest treats each row independently, so it will ignore any kind of time series correlations. You can verify this by shuffling your data before training, your model (aside from non-stochastic nature of Random Forest) will be the same.

• sorry if I was unclear but what about correlations within the row. the rows themselves are each a time series of information Nov 12, 2021 at 20:29
• @barnhillec, Just to clarify, your features are something like: day 1, day 2, day 3, ... day n, day n+1? If so, Random Forest will not care about order of features either. Nov 12, 2021 at 20:53
• right. no it wouldn't care about the order of the input. but would it learn the correlations that were present in the series the way a time series model would? Nov 12, 2021 at 20:54

XGBoost, LightGBM, and CatBoost are almost always better than Random Forest in accuracy and they are similar to Random Forest. LightGBM is also much faster to calculate. They all work best if the time series features are added to the data. Lags, moving averages, standard deviations of different number of periods help improve accuracy significantly.

Just look at M5 Forecasting Accuracy Wallmart Kaggle competition (https://www.kaggle.com/c/m5-forecasting-accuracy). The first 5 places are 4 LightGBM models and 1 DeepAR model.

The first place and, if I remember correctly, all other first places with LightGBM models, had:

1. lags
2. moving averages
3. moving standard deviations
4. features based on price

About 10 years ago the winning models were Random Forest for classification and different time series algorithms like Exponential Smoothing for time series. But now not any one of the winning solution uses Random Forest. And the boosting algorithms gain significantly from feature engineering.

In my opinion this kind of feature engineering was a new development several years ago that allowed the tree algorithms to beat simple and even more complicated usual time series algorithms when you have additional external data (like prices). This should also apply to Random Forest.

I am working full-time as a data scientist at a consulting company making demand forecasting to improve supply chain processes. I was involved with about 6 clients to create demand forecasts and I was once competing with a competitor for the best accuracy and won.

• Some of this is super helpful, adding some lags or averages would be one way to help the time series along. I think my question is more technical though. Is the random forest going to learn the time series correlations? Nov 13, 2021 at 1:14
• If you only have one last data point as your "X" then no. y_pred is a function of "X" and will only deliver the result based on the one value provided. But if you have some of the previous data in "X"(I call it lags) then the model can learn some correlations. Though it is not as efficient as also adding other features. Nov 13, 2021 at 15:44