Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weekly forecasts with holidays

I use Hyndman's forecast package to produce a somewhat accurate tbats forecast at the weekly level, but I have significant errors on holidays. How can I include holidays in the model? Also, Arima has been shown to fit my weekly data poorly. So holidays would have to be added in a non-arima way.

I have seen two solutions. One https://robjhyndman.com/hyndsight/dailydata/ shows how to add holidays as dummy variables with fourier terms. The problem is dummy variables take the form of 1 or 0. I know that different holidays have different effects that a 1 or 0 would not capture. Black Friday, for example, is very different from Chinese New Year.

Another solution is have seen is here https://robjhyndman.com/hyndsight/forecast7-part-2/ where covariate nnetr change is used as an alternative to auto.arima with seasonal dummy variables. The problem is I don't see how to write the R code to input my holidays. An example would be useful.

like image 985
Alex Avatar asked Oct 22 '17 12:10

Alex


2 Answers

The following did everything I needed it to do.

k=23
#forecast holidays
#bool list of future holidays
holidayf <- c(0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0)
h <- length(holidayf)

#given holidays
holiday <- df[,2] 
y <- ts(df[,1],start = 2011,frequency = 52)
z <- fourier(y, K=k)
zf <- fourier(y, K=k, h=h)
fit <- auto.arima(y, xreg=cbind(z,holiday), seasonal=FALSE)
fc <- forecast(fit, xreg=cbind(zf,holidayf), h=h)
fc %>% autoplot()
summary(fit)

To solve the problem of different holidays having different effect, I simply added additional holiday dummy variables. For example, you can make a vector of good holidays and a vector of bad holidays and cbind them then put them in xreg. I did not show this in the above code, but it is straight forward.

like image 87
Alex Avatar answered Oct 03 '22 22:10

Alex


The benchmark for time series modeling for use by official statistics agencies is x13-arima-seats by the US Census bureau. It deals with seasonal effects as well as with "parametric" holidays including, say, the Chinese New Year as well as Easter.

The functionality is available in R via the seasonal package which installs and uses the underlying x13-arima-seats binary.

And there is also a full-feature interactive website giving access to most-if-not-all features.

like image 42
Dirk Eddelbuettel Avatar answered Oct 03 '22 21:10

Dirk Eddelbuettel