1. Dynamic harmonic regression
One particularly useful kind of dynamic regression is called "dynamic harmonic regression". This uses Fourier terms to handle seasonality.
2. Dynamic harmonic regression
Fourier was a French mathematician who showed that
3. Dynamic harmonic regression
a series of sine and cosine terms of the right frequencies can approximate any periodic function. We can use them for seasonal patterns when forecasting.
Fourier terms come in pairs consisting of a sine and a cosine. The frequency of these terms are called the "harmonic frequencies", and they increase with k. These Fourier terms are predictors in our dynamic regression model. The more terms we include in the model, the more complicated our seasonal pattern will be.
We choose uppercase K to control how many terms get included. The alpha and gamma values are coefficients in our regression model.
Because the seasonality is being modelled by the Fourier terms, we normally use a non-seasonal ARIMA model for the error.
One important difference between handling seasonality this way, and using a seasonal ARIMA model, is that Fourier terms assume the seasonal pattern does not change over time, whereas a seasonal ARIMA model allows the seasonal pattern to evolve over time.
4. Example: Australian cafe expenditure
This is monthly expenditure on eating out in Australia.
Note that I have set seasonal equals FALSE meaning that the ARIMA error in the model should be non-seasonal. I have also used a Box-Cox transformation by setting lambda equals 0, because the variance increases with the level of the series.
We can use the fourier function to generate all the Fourier terms to include in our model. We just have to select the value of K which determines how complicated the seasonal pattern will be.
When forecasting, we use the fourier function again to generate future values of the Fourier predictors. It must have the same value of K as was used when fitting the model. By adding the h argument, the fourier function knows we want future values not past values. h is the forecast horizon.
Using K equals 1 does not capture the seasonal pattern very well. Let's increase the value of K
5. Example: Australian cafe expenditure
to see how the forecasts change.
6. Example: Australian cafe expenditure
As K increases,
7. Example: Australian cafe expenditure
the seasonal pattern starts
8. Example: Australian cafe expenditure
to look more like the past data.
9. Example: Australian cafe expenditure
You will also notice that the ARIMA error model gets simpler, as there is less signal in the residuals when K is larger.
The best way to select K is to try a few different values and select the model that gives the lowest AICc value.
In this case, it is K equals 5.
10. Dynamic harmonic regression
The model can include other predictor variables as well as the Fourier terms. They just need to be added to the xreg matrix.
The whole process is mostly automated. The only thing you must do yourself is select K. Start with K equals 1, and slowly increase it until the AICc value stops decreasing. K can't be more than half the seasonal period.
The advantage of using Fourier terms, compared to other approaches for modelling seasonality, is that they can handle seasonality when the seasonal period m is very large. For example, with weekly data where m is approximately 52, daily data where m could be 365 if there is annual seasonality, and sub-daily data where it could be even higher.
11. Let's practice!
OK, let's see how you go using dynamic harmonic regression in the next exercise.