Forecasting weekly data
With weekly data, it is difficult to handle seasonality using ETS or ARIMA models as the seasonal length is too large (approximately 52). Instead, you can use harmonic regression which uses sines and cosines to model the seasonality.
The fourier()
function makes it easy to generate the required harmonics. The higher the order (\(K\)), the more "wiggly" the seasonal pattern is allowed to be. With \(K=1\), it is a simple sine curve. You can select the value of \(K\) by minimizing the AICc value. As you saw in the video, fourier()
takes in a required time series, required number of Fourier terms to generate, and optional number of rows it needs to forecast:
> # fourier(x, K, h = NULL)
> fit <- auto.arima(cafe, xreg = fourier(cafe, K = 6),
seasonal = FALSE, lambda = 0)
> fit %>%
forecast(xreg = fourier(cafe, K = 6, h = 24)) %>%
autoplot() + ylim(1.6, 5.1)
The pre-loaded gasoline
data comprises weekly data on US finished motor gasoline products. In this exercise, you will fit a harmonic regression to this data set and forecast the next 3 years.
This is a part of the course
“Forecasting in R”
Exercise instructions
- Set up an
xreg
matrix calledharmonics
using thefourier()
method ongasoline
with order \(K=13\) which has been chosen to minimize the AICc. - Fit a dynamic regression model to
fit
. Setxreg
equal toharmonics
andseasonal
toFALSE
because seasonality is handled by the regressors. - Set up a new
xreg
matrix callednewharmonics
in a similar fashion, and then compute forecasts for the next three years asfc
. - Finally, plot the forecasts
fc
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set up harmonic regressors of order 13
harmonics <- fourier(___, K = ___)
# Fit regression model with ARIMA errors
fit <- auto.arima(___, xreg = ___, seasonal = ___)
# Forecasts next 3 years
newharmonics <- fourier(___, K = ___, h = ___)
fc <- forecast(___, xreg = ___)
# Plot forecasts fc
___
This exercise is part of the course
Forecasting in R
Learn how to make predictions about the future using time series forecasting in R including ARIMA models and exponential smoothing methods.
The time series models in the previous chapters work well for many time series, but they are often not good for weekly or hourly data, and they do not allow for the inclusion of other information such as the effects of holidays, competitor activity, changes in the law, etc. In this chapter, you will look at some methods that handle more complicated seasonality, and you consider how to extend ARIMA models in order to allow other information to be included in the them.
Exercise 1: Dynamic regressionExercise 2: Forecasting sales allowing for advertising expenditureExercise 3: Forecasting electricity demandExercise 4: Dynamic harmonic regressionExercise 5: Forecasting weekly dataExercise 6: Harmonic regression for multiple seasonalityExercise 7: Forecasting call bookingsExercise 8: TBATS modelsExercise 9: TBATS models for electricity demandExercise 10: Your future in forecasting!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.