Get Started

Harmonic regression for multiple seasonality

Harmonic regressions are also useful when time series have multiple seasonal patterns. For example, taylor contains half-hourly electricity demand in England and Wales over a few months in the year 2000. The seasonal periods are 48 (daily seasonality) and 7 x 48 = 336 (weekly seasonality). There is not enough data to consider annual seasonality.

auto.arima() would take a long time to fit a long time series such as this one, so instead you will fit a standard regression model with Fourier terms using the tslm() function. This is very similar to lm() but is designed to handle time series. With multiple seasonality, you need to specify the order \(K\) for each of the seasonal periods.

# The formula argument is a symbolic description
# of the model to be fitted

> args(tslm)
function (formula, ...)

tslm() is a newly introduced function, so you should be able to follow the pre-written code for the most part. The taylor data are loaded into your workspace.

This is a part of the course

“Forecasting in R”

View Course

Exercise instructions

  • Fit a harmonic regression called fit to taylor using order 10 for each type of seasonality.
  • Forecast 20 working days ahead as fc. Remember that the data are half-hourly in order to set the correct value for h.
  • Create a time plot of the forecasts.
  • Check the residuals of your fitted model. As you can see, auto.arima() would have done a better job.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Fit a harmonic regression using order 10 for each type of seasonality
fit <- tslm(taylor ~ fourier(___, K = c(10, 10)))

# Forecast 20 working days ahead
fc <- forecast(___, newdata = data.frame(fourier(___, K = ___, h = ___)))

# Plot the forecasts
___

# Check the residuals of fit
___
Edit and Run Code

This exercise is part of the course

Forecasting in R

IntermediateSkill Level
4.9+
14 reviews

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 seasonality
Exercise 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.

Start Learning for Free