# Using regression trees for forecasting double-seasonal time series with trend in R

After blogging break caused by writing research papers, I managed to secure time to write something new about time series forecasting. This time I want to share with you my experiences with seasonal-trend time series forecasting using simple regression trees. Classification and **regression tree** (or decision tree) is broadly used **machine learning** method for modeling. They are favorite because of these factors:

- simple to understand (white box)
- from a tree we can extract interpretable results and make simple decisions
- they are helpful for exploratory analysis as binary structure of tree is simple to visualize
- very good prediction accuracy performance
- very fast
- they can be simply tuned by ensemble learning techniques

But! There is always some “but”, they poorly adapt when new unexpected situations (values) appears. In other words, they can not detect and adapt to change or concept drift well (absolutely not). This is due to the fact that tree creates during learning just simple rules based on training data. Simple decision tree does not compute any regression coefficients like linear regression, so trend modeling is not possible. You would ask now, so why we are talking about time series forecasting with regression tree together, right? I will explain how to deal with it in more detail further in this post.

You will learn in this post how to:

- decompose double-seasonal time series
- detrend time series
- model and forecast
**double-seasonal time series**with trend - use two types of simple
**regression trees** - set important hyperparameters related to regression tree

## Exploring time series data of electricity consumption

As in previous posts, I will use smart meter data of electricity consumption for demonstrating forecasting of seasonal time series. I created a new dataset of aggregated electricity load of consumers from an anonymous area. Time series data have the length of 17 weeks.

Firstly, let’s scan all of the needed packages for data analysis, modeling and visualizing.

Now read the mentioned time series data by `read_feather`

to one `data.table`

. The dataset can be found on my github repo, the name of the file is *DT_load_17weeks*.

And store information of the date and period of time series that is 48.

For data visualization needs, store my favorite ggplot theme settings by function `theme`

.

Now, pick some dates of the length 3 weeks from dataset to split data on the train and test part. Test set has the length of only one day because we will perform one day ahead forecast of electricity consumption.

Let’s plot the train set and corresponding average weekly values of electricity load.

We can see some trend increasing over time, maybe air conditioning is more used when gets hotter in summer. The double-seasonal (daily and weekly) character of time series is obvious.

A very useful method for visualization and analysis of time series is STL decomposition.
STL decomposition is based on Loess regression, and it decomposes time series to three parts: seasonal, trend and remainder.
We will use results from the STL decomposition to model our data as well.
I am using `stl()`

from `stats`

package and before computation we must define weekly seasonality to our time series object. Let’s look on results:

As was expected from the previous picture, we can see that there is “slight” trend increasing and decreasing (by around 100 kW so slightly large ;) ). Remainder part (noise) is very fluctuate and not seems like classical white noise (we obviously missing additional information like weather and other unexpected situations).

## Constructing features to model

In this section I will do **feature engineering** for modeling double-seasonal **time series** with trend best as possible by just available historical values.

Classical way to handle seasonality is to add seasonal features to a model as vectors of form \( (1, \dots, DailyPeriod, 1, …, DailyPeriod,…) \) for daily season or \( (1, \dots, 1, 2, \dots, 2, \dots , 7, 1, \dots) \) for weekly season. I used it this way in my previous post about GAM and somehow similar also with multiple linear regression.

A better way to model seasonal variables (features) with **nonlinear regression** methods like **tree** is to transform it to Fourier terms (sinus and cosine). It is more effective to tree models and also other nonlinear machine learning methods. I will explain why it is like that further of this post.

Fourier daily signals (terms) are defined as:

where \( ds \) is number of daily seasonality Fourier pairs and Fourier weekly terms are defines as:

where \( ws \) is a number of weekly seasonality Fourier pairs.

Another great feature (most of the times most powerful) is a lag of original time series. We can use lag by one day, one week, etc… The lag of time series can be preprocessed by removing noise or trend for example by STL decomposition method to ensure stability.

As was earlier mentioned, regression trees can’t predict trend because they logically make rules and predict future values only by rules made by training set. Therefore original time series that inputs to regression tree as dependent variable must be detrended (removing the trend part of the time series). The acquired trend part then can be forecasted by for example ARIMA model.

Let’s go to constructing mentioned features and trend forecasting.

Double-seasonal Fourier terms can be simply extracted by `fourier`

function from `forecast`

package.
Firstly, we must create multiple seasonal object with function `msts`

.

Now use `fourier`

function using two conditions for a number of K terms.
Set K for example just to 2.

It made 2 pairs (sine and cosine) of daily and weekly seasonal signals. If we compare it with approach described in previous posts, so simple periodic vectors, it looks like this:

where *four-daily* is the Fourier term for daily season, *simple-daily* is the simple feature for daily season, *four-weekly* is the Fourier term for weekly season, and *simple-weekly* is the simple feature for weekly season. The **advantage** of **Fourier terms** is that there is much more closeness between ending and starting of a day or a week, which is more natural.

Now, let’s use data from STL decomposition to forecast trend part of time series. I will use `auto.arima`

procedure from the `forecast`

package to perform this.

Let’s plot it:

Function `auto.arima`

chose ARIMA(0,2,0) model as best for trend forecasting.

Next, make the final feature to the model (lag) and construct train matrix (model matrix). I am creating lag by one day and just taking seasonal part from STL decomposition (for having smooth lag time series feature).

The accuracy of forecast (or fitted values of a model) will be measured by MAPE, let’s defined it:

## RPART (CART) tree

In the next two sections, I will describe two **regression tree** methods. The first is **RPART**, or CART (Classification and Regression Trees), the second will be CTREE. RPART is recursive partitioning type of binary tree for classification or regression tasks. It performs a search over all possible splits by maximizing an information measure of node impurity, selecting the covariate showing the best split.

I’m using `rpart`

implementation from the same named package. Let’s go forward to modeling and try default settings of `rpart`

function:

It makes many interesting outputs to check, for example we can see a table of nodes and corresponding errors by `printcp(tree_1)`

or see a detailed summary of created nodes by `summary(tree_1)`

. We will check variable importance and number of created splits:

We can see that most important variables are Lag and cosine forms of the daily and weekly season. The number of splits is 10, ehm, is it enough for time series of length 1008 values?

Let’s plot created rules with fancy `rpart.plot`

function from the same named package:

We can see values, rules, and percentage of values split each time. Pretty simple and interpretable.

Now plot fitted values to see results of the `tree_1`

model.

And see the error of fitted values against real values.

Whups. It’s a little bit simple (rectangular) and not really accurate, but it’s logical result from a simple tree model.
The key to achieving better results and have more accurate fit is to set manually control hyperparameters of rpart.
Check `?rpart.control`

to get more information.
The “hack” is to change cp (complexity) parameter to very low to produce more splits (nodes). The `cp`

is a threshold deciding if each branch fulfills conditions for further processing, so only nodes with fitness larger than factor cp are processed. Other important parameters are the minimum number of observations in needed in a node to split (`minsplit`

) and the maximal depth of a tree (`maxdepth`

).
Set the `minsplit`

to 2 and set the `maxdepth`

to its maximal value - 30.

Now make simple plot to see depth of the created tree…

That’s little bit impressive difference than previous one, isn’t it? Check also number of splits.

600 is higher than 10 :)

Let’s plot fitted values from the model `tree_2`

:

And see the error of fitted values against real values.

Much better, but obviously the model can be overfitted now.

Add together everything that we got till now, so forecast load one day ahead. Let’s create testing data matrix:

Predict detrended time series part with `tree_2`

model + add the trend part of time series forecasted by ARIMA model.

Let’s plot the results and compare it with real values from `data_test`

.

Not bad. For clarity, compare forecasting results with model without separate trend forecasting and detrending.

We can see that RPART model without trend manipulation has higher values of the forecast. Evaluate results with MAPE forecasting measure.

We can see the large difference in MAPE. So detrending original time series and forecasting separately trend part really works, but not generalize the result now. You can read more about RPART method in its great package vignette.

## CTREE

The second simple regression tree method that will be used is **CTREE**. Conditional inference trees (CTREE) is a statistical approach to recursive partitioning, which takes into account the distributional properties of the data. CTREE performs multiple test procedures that are applied to determine whether no significant association between any of the feature and the response (load in the our case) can be stated and the recursion needs to stop.
In **R** CTREE is implemented in the package `party`

in the function `ctree`

.

Let’s try fit simple `ctree`

with a default values.

Constructed tree can be again simply plotted by `plot`

function, but it made many splits so it’s disarranged.

Let’s plot fitted values from `ctree_1`

model.

And see the error of fitted values against real values.

Actually, this is pretty nice, but again, it can be tuned.

For available hyperparameters tuning check `?ctree_control`

. I changed hyperparameters `minsplit`

and `minbucket`

that have similar meaning like the cp parameter in RPART. The `mincriterion`

can be tuned also, and it is significance level (1 - p-value) that must be exceeded in order to implement a split. Let’s plot results.

And see the error of fitted values against real values.

It’s better. Now forecast values with `ctree_2`

model.

And compare CTREE with RPART model.

Slightly better MAPE value with RPART, but again now it can not be anything to generalize. You can read more about CTREE method in its great package vignette. Try to forecast future values with all available electricity load data with sliding window approach (window of the length of three weeks) for a period of more than three months (98 days).

## Comparison

Define functions that produce forecasts, so add up everything that we learned so far.

I created `plotly`

boxplots graph of MAPE values from four models - CTREE simple, CTREE with detrending, RPART simple and RPART with detrending. Whole evaluation can be seen in the script that is stored in my GitHub repository.

We can see that **detrending time series** of electricity consumption improves the accuracy of the forecast with the combination of both **regression tree** methods - **RPART** and **CTREE**. My approach works as expected.

The habit of my posts is that animation must appear. So, I prepared for you two animations (animated dashboards) using `animation`

, `grid`

, `ggplot`

and `ggforce`

(for zooming) packages that visualize results of forecasting.

We can see that in many days it is almost perfect forecast, but on some days it has some potential for improving.

## Conclusion

In this post, I showed you how to solve **trend** appearance in **seasonal time series** with using a **regression tree** model. **Detrending time series** for regression tree methods is a important (must) procedure due to the character of decision trees. The trend part of a time series was acquired by STL decomposition and separately forecasted by a simple ARIMA model. I evaluated this approach on the dataset from smart meters measurements of electricity consumption. The regression (decision) tree is a great technique for getting simple and interpretable results in very fast computational time.

In the future post, I will focus on enhancing the predictive performance of simple regression tree methods by ensemble learning methods like Bagging, Random Forest, and similar.