# Fourier feature for multi seasonality

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.

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

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

max_k <- 10
AICs <- rep(NA,max_k+1)
AICs <- auto.arima(taylor_ts, seasonal=FALSE)$aic pb <- winProgressBar(max=max_k) for (kk in 1:max_k) { setWinProgressBar(pb,kk,paste(kk,"of",max_k)) model <- auto.arima(taylor_ts, seasonal=FALSE, xreg=fourier(taylor_ts, K=kk)) AICs[kk+1] <- model$aic
}
close(pb)

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.

• 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? Jul 28, 2022 at 8:22
• The multiple seasonalities is precisely what higher Fourier orders will capture. The regressors are not "assigned" to a frequency. Jul 28, 2022 at 8:24
• 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 Jul 28, 2022 at 10:05
• 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. Jul 28, 2022 at 10:19