I used the Thanksgiving break to push a new update of the TSstudio package to CRAN (version 0.1.3). The new version includes an update for the ts_backtesting
function along with two new function – ts_to_prophet
for converting time series objects to a prophet input format (i.e., ds
and y
columns), and ccf_plot
for lags plot between two time series. The package can be installed from either CRAN or Github:
1 |
|
1 |
|
1 |
|
Converting time series object to a prophet format
The ts_to_prophet
function converting ts
, xts
and zoo
objects into prophet input format (i.e., data frame with two columns – ds for date and y for the series values). For instance, convertig the USgas
series to a prophet object:
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
In the case of a ts
object, where the index is not a date object, the function extracts the time component from the first observation and use it along with the frequency of the series to estimate the date column of the prophet data frame. For instance, in the case of a monthly series, where the time object provides only the year and the month, by default the day component of the date object will be set to 1. Alternatively, if known, you can set the date of the first observation with the start
argument. For example, if the USgas
series is being captured during the mid of the month (or every 15th of the month):
1 |
|
1 |
|
Similarly, the function can handle xts
and zoo
objects:
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
Lags plots of two series
The second function, ccf_plot
, provides an interactive and intuitive visualization of the cross-correlation between two time series, by plotting a series against another series (and its lags) and calculating the correlation between the two with the ccf
function. For instance, let’s use the function to plot the relationship between the unemployment rate and the total vehicle sales in the US:
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
The function automatically aligned and used only the overlapping observations of the two series before calculating the cross-correlation values between the series and the lags of the second series (where the 0 lag represents the series itself, and negative lags represent the leading lags). The title of each plot specifies the lag number and the cross-correlation value. The lags
argument of the function defines the number of lags in the plot, where the use of negative lags defines the leading indicators. For example, setting the lags
argument to -6:6 will plot the first 6 lags, the series itself and the first 6 leading lags of the series:
1 |
|
Forecasting with backtesting and xreg
The ts_backtesting
function for training and testing multiple models (e.g., auto.arima
, HoltWinters
, nnetar
, etc.) with backtesting approach, is now supporting the xreg
component of the auto.arima
, nnetar
( forecast package)and their embedment in the hybridModel
model ( forecastHybrid package). The use of the xreg
component is straightforward and required two components:
-
The predictors – or the regressors component in a vector or matric format will be used as an input to the model
xreg
argument. The length of this input must be aligned with the length of the input series -
The future values of the predictors – a vector or matrix must correspond to the inputs which used as predictors, where the length of this component must be aligned to the forecast horizon (or the
h
argument of the function). This setting of this component is done with thexreg.h
argument
For instance, let’s forecast the monthly consumption of natural gas in US in the next 5 years (or 60 months) by regressing the USgas
series with its Fourier terms, using auto.arima
, nnetar
and hybridModel
models. We will use the fourier
function from the forecast package to generate both the inputs for the regression model (x_reg
) and future values for the forecast itself (x_reg.forecast
):
1 |
|
Note that the ts_backtesting
function automatically split and aligned the xreg
component according to the expanding window movement of the function. We will set the function to run backtesting using 6 periods/splits to train auto.arima
, nnetar
and hybridModel
models, in order to examine the performance of the models over time:
1 |
|
1 |
|
We can now review the performance of each model using the summary plot:
1 |
|
The summary plot provides the error distribution of each model and the plot forecasting model which performed best on the backtesting. The output contains the models’ performance on the backtesting (i.e., summary plot and leaderboard). In this case, since we set the error
argument to MAPE
, the function selected the auto.arima
final forecast. Yet, you can see in the plot that the error rate of the hybrid
model is more stable compared to the auto.arima
and it might be a better choice (the hybrid
contains both the auto.arima
and other models, which potentially helps to hedge the error). All the models’ information available on the Forecast_Final
folder. For example, you can pull the auto.arima
model and check its residuals:
1 |
|
The plan for future releases is to expend the functionality of the ts_backtesting
function, by adding additional models (e.g., tslm
, prophet
, etc.) and expend the window setting of the backtesting (adding sliding window option).
Related