I am trying to forecast a univariate time series with multiple seasonality

  1. Something like this:

    library(forecast) fit <- auto.arima(y, seasonal=FALSE, xreg=fourier(y, K=8))

  2. Based on visualization my data show frequency of 7, 31 and 365. It is answered in few stack exchange answers that The value of K for fourier term can be chosen by minimizing the AIC.

How would the pseudo code look like to find K for for each frequency ( 7, 31 and 365), to minimize AIC.


1 Answer 1


You can simply iterate over reasonable numbers of Fourier orders and extract the AIC values, like this:

taylor_ts <- ts(as.vector(taylor), frequency=336)

max_k <- 10
AICs <- rep(NA,max_k+1)
AICs[1] <- auto.arima(taylor_ts, seasonal=FALSE)$aic

pb <- winProgressBar(max=max_k)
for (kk in 1:max_k) {
    model <- auto.arima(taylor_ts, seasonal=FALSE, xreg=fourier(taylor_ts, K=kk))
    AICs[kk+1] <- model$aic

plot(0:max_k,AICs,type="o", pch=19, las=1)


Alternatively, consider defining your time series as an msts object and using a method that is explicitly built for forecasting series with multiple seasonalities.

  • $\begingroup$ In the snippet you have shown, you have taken one frequency. I am asking about time series with multiple seasonality/frequency. What should be order of iteration in that case? $\endgroup$
    – Anant
    Jul 28, 2022 at 8:22
  • $\begingroup$ The multiple seasonalities is precisely what higher Fourier orders will capture. The regressors are not "assigned" to a frequency. $\endgroup$ Jul 28, 2022 at 8:24
  • $\begingroup$ I am confused. So, there are two terms first: the seasonal periodicity (ex: 7 days, 31 days, 365 days etc) and second if number of sin and cosine terms for each of distinct seasonal periodicity. My question is what should be K for each of 7, 31, and 365 days seasonal periodicity $\endgroup$
    – Anant
    Jul 28, 2022 at 10:05
  • $\begingroup$ If you want a sine wave that has one cycle every month (to cover day-in-month seasonality), that is exactly the same as having a sine wave that has 12 cycles every year. So accounting for multiple seasonalities is exactly the same as modeling longer seasonalities with higher Fourier terms. You may want to go higher than the 10 terms I used - but then again, you will quickly overfit that way. Also, you might want to model the weekdays using dummies, since the weekend is often qualitatively different from working days in a way that is not easy to capture with continuous harmonics. $\endgroup$ Jul 28, 2022 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.