Solved – Conditional model using function tslm in R package forecast

forecastingrregressiontime series

I would like to use tslm with data that has intraday seasonality and a different pattern on business days and on non-business days.
If data.ts is my time series then I would like to use something like

tslm(data.ts~season|businesss.dummy)

Thus I want to model season given that the dummy for this hour is True or False.
I don't want

tslm(data.ts~season + businesss.dummy)

as this would just give a parallel shift on business days.
I know that I can subset the data before applying the model and thus get business day data and non-business day data only but can I achieve this aim more elegantly using the right formula in tslm?
Thanks!

Best Answer

You can do this by setting up the seasonal factors yourself. I'm assuming you have hourly data over three weeks, and that each week has 7 days.

x <- ts(rnorm(21*24),f=24)
dow <- rep(rep(1:7,rep(24,7)),3)
business.dummy <- (dow<=5)
seasons <- cycle(x)
seasons[!business.dummy] <- seasons[!business.dummy] + 24
seasons <- factor(seasons,levels=1:48,
    labels=c(paste("Week",1:24),paste("Weekend",1:24)))
fit <- tslm( x ~ seasons - 1)

The seasons factor has 48 levels, the first 24 corresponding to weekday hours, and the second 24 corresponding to weekend hours. You can generalize to allow other non-business days by setting the relevant values of business.dummy to FALSE.