# Arima Model with weekday dummy variables Forecast

I'm trying to create an Arima model and forecast it ahead the next 20 hours using the code and data below.

When I look at the median of df$tri for each hour and broken down by day of the week, each weekday seems to have a distinct 24 hour pattern. So I thought I would try adding dummy variables for the day of the week to my model as xreg predictors. When I plot Acast$mean, I'm just getting a flat line. So I was wondering if there was something incorrect about the way I'm creating and using the dummy variables for day of the week.

Code:

##BoxCox

Tlambda <- BoxCox.lambda(df$Tri) ##Partitioning Time Series EndTrain<-336 ValStart<-EndTrain+1 ValEnd<-ValStart+20 tsTrain <-df$$Tri[1:EndTrain] tsValidation<-df$$Tri[ValStart:ValEnd] ##Weekday variables Copydf<-df Copydf$$Weekday<-as.factor(weekdays(as.Date(Copydf$$DateTime, "%Y-%m-%d"))) Weekdays <- Copydf$Weekday[1:nrow(Copydf)]
xreg1 <- model.matrix(~as.factor(Weekdays)+0)[, 1:7]
colnames(xreg1) <- c("Friday", "Monday", "Saturday", "Sunday","Thursday","Tuesday","Wednesday")

xreg1Train<-xreg1[1:EndTrain,]
xreg1Val<-xreg1[ValStart:ValEnd,]

##Checking effect of Weekday
Arima.fit <- auto.arima(tsTrain, lambda = Tlambda, xreg=xreg1Train, stepwise=FALSE, approximation = FALSE )

##Forecast

Acast<-forecast(Arima.fit,xreg=xreg1Val, h=20)

Acast$mean  Data:  dput(df$Tri[1:336])
c(11, 14, 17, 5, 5, 5.5, 8, NA, 5.5, 6.5, 8.5, 4, 5, 9, 10, 11,
7, 6, 7, 7, 5, 6, 9, 9, 6.5, 9, 3.5, 2, 15, 2.5, 17, 5, 5.5,
7, 6, 3.5, 6, 9.5, 5, 7, 4, 5, 4, 9.5, 3.5, 5, 4, 4, 9, 4.5,
6, 10, NA, 9.5, 15, 9, 5.5, 7.5, 12, 17.5, 19, 7, 14, 17, 3.5,
6, 15, 11, 10.5, 11, 13, 9.5, 9, 7, 4, 6, 15, 5, 18, 5, 6, 19,
19, 6, 7, 7.5, 7.5, 7, 6.5, 9, 10, 5.5, 5, 7.5, 5, 4, 10, 7,
5, 12, 6, NA, 4, 2, 5, 7.5, 11, 13, 7, 8, 7.5, 5.5, 7.5, 15,
7, 4.5, 9, 3, 4, 6, 17.5, 11, 7, 6, 7, 4.5, 4, 4, 5, 10, 14,
7, 7, 4, 7.5, 11, 6, 11, 7.5, 15, 23.5, 8, 12, 5, 9, 10, 4, 9,
6, 8.5, 7.5, 6, 5, 8, 6, 5.5, 8, 11, 10.5, 4, 6, 7, 10, 11.5,
11.5, 3, 4, 16, 3, 2, 2, 8, 4.5, 7, 4, 8, 11, 6.5, 7.5, 17, 6,
6.5, 9, 12, 17, 10, 5, 5, 9, 3, 8.5, 11, 4.5, 7, 16, 11, 14,
6.5, 15, 8.5, 7, 6.5, 11, 2, 2, 13.5, 4, 2, 16, 11.5, 3.5, 9,
16.5, 2.5, 4.5, 8.5, 5, 6, 7.5, 9.5, NA, 9.5, 8, 2.5, 4, 12,
13, 10, 4, 6, 16, 16, 13, 8, 12, 19, 19, 5.5, 8, 6.5, NA, NA,
NA, 15, 12, NA, 6, 11, 8, 4, 2, 3, 4, 10, 7, 5, 4.5, 4, 5, 11.5,
12, 10.5, 4.5, 3, 4, 7, 15.5, 9.5, NA, 9.5, 12, 13.5, 10, 10,
13, 6, 8.5, 15, 16.5, 9.5, 14, 9, 9.5, 11, 15, 14, 5.5, 6, 14,
16, 9.5, 23, NA, 19, 12, 5, 11, 16, 8, 11, 9, 13, 6, 7, 3, 5.5,
7.5, 19, 6.5, 5.5, 4.5, 7, 8, 7, 10, 11, 13, NA, 12, 1.5, 7,
7, 12, 8, 6, 9, 15, 9, 3, 5, 11, 11, 8, 6, 3, 7.5)

> lines(forecast(dowlongs, 48)$mean, col = "green", lwd = 2) > abline(v = 8, lty = 2) > legend("topleft", bty = "n", + c("Seasonal AR", "Seasonal AR with DoW", "Long Seasonal AR"), + fill = c("blue", "red", "green"))  Results fell apart for the first two models this time. Since there isn't an underlying daily pattern any more, the model with external regressors was only able to see that certain days of the week are higher or lower on average, but the pattern from hour to hour was missed. The weekly seasonal model however was still about to see the weekly repeat and make a reasonable model. ## Your Data Now that we have seen the importance of seasonality in our models, lets see what happens if we try running auto.arima again, but this time making your data a seasonal time series. > tsTrain <- ts(tsTrain, frequency = 24) > (dowsmodel <- auto.arima(tsTrain)) Series: tsTrain ARIMA(0,0,0)(1,0,0)[24] with non-zero mean Coefficients: sar1 mean 0.0508 8.4899 s.e. 0.0579 0.2452 sigma^2 estimated as 17.31: log likelihood=-928.96 AIC=1863.91 AICc=1863.98 BIC=1875.36 > (dowxreg <- auto.arima(tsTrain, xreg = dowreg)) Series: tsTrain Regression with ARIMA(0,0,0)(1,0,0)[24] errors Coefficients: sar1 intercept Tues Weds Thurs Fri Sat Sun 0.0841 7.7401 -0.4165 2.3504 0.0211 1.1671 1.8975 0.3029 s.e. 0.0587 0.5991 0.8074 0.8496 0.8617 0.8520 0.8461 0.8288 sigma^2 estimated as 16.63: log likelihood=-919.55 AIC=1857.1 AICc=1857.65 BIC=1891.45 > (dowlongs <- auto.arima(ts(tsTrain, frequency = 24*7))) Series: ts(tsTrain, frequency = 24 * 7) ARIMA(0,0,2) with non-zero mean Coefficients: ma1 ma2 mean 0.2433 -0.0171 8.5118 s.e. 0.0583 0.0506 0.2782 sigma^2 estimated as 16.51: log likelihood=-921.06 AIC=1850.12 AICc=1850.24 BIC=1865.39 > plot(forecast(dowsmodel, 24), PI = FALSE, xlim = c(8, 16), + main = "Forecasts from Various Seasonal AR models for Different DoW Effects") > lines(forecast(dowxreg, 24, xreg = dowreg[1:24, ])$mean, col = "red", lwd = 2)
> lines(ts(forecast(dowlongs, 24)\$mean, start = 15, frequency = 24),
+       col = "green", lwd = 2)
> abline(v = 8, lty = 2)
> legend("topleft", bty = "n",
+        c("Seasonal AR", "Seasonal AR with DoW", "Long Seasonal AR"),
+        fill = c("blue", "red", "green"))


The "each weekday seems to have a distinct 24 hour pattern" doesn't seem to be happening as seen by the trouble fitting a the weekly seasonal model, but there does seem to be a daily seasonality the models are picking up on since. Personally, I would trust the plain seasonal model (no external regressors) the most since it is less prone to over fitting than the one with external regressors, but that is your call. In general, you might feel disappointed since the forecasts don't look much like your data. This is because there is a lot of noise in your data that the model still can't account for.

## Conclusions

1. A seasonal model will allow your data to find repeating patterns in your data.
2. Adding external regressors to your model can allow the model to find the underlying pattern when the pattern is obscured by another influence.
3. If every day of the week has a different pattern, that is a weekly seasonality, not a day of week effect.
• Why not add both daily effects and hourly effects and allow for unusual values to be detected and remedied with pulse indicators being added to the model where needed ? – IrishStat Aug 31 '17 at 17:15
• @IrishStat Do you mean an ARIMA model with two different seasonalities? If so, that does sound appealing, but I am not aware of any implementation for such a model in the languages I am familiar with. You could of course use a different model like tbats which is based on exponential smoothing, but the OP was requesting an ARIMA based model. – Barker Aug 31 '17 at 20:37
• i meant a deterministic model with 1 + 6 + 23 predictors and then identifying/incorpoarating an approriate arima model and potential outliers/level shifts/seasonal pulses/time trends AND arma. IN this way the OP gets his arma strtcture bur it is integrated into needed deterministic structure, there is no double seasonality in ARIMA. – IrishStat Aug 31 '17 at 21:53
• @IrishStat I would hesitate to put a model with so many predictors on this size dataset. In particular, if the pattern really does show a differently pattern every day of the week (which I am not convince it does), you would need 24*7 - 1 predictors to account for the fact that "2pm" has a different pattern on Monday than on Tuesday. If you want to account for multiple seasonalities, I would be more inclined to try including Fourier terms to avoid parameter space explosion. – Barker Aug 31 '17 at 22:58
• If you use 6 day-of-the-week and 23 hour of the day that's 29 = 1 for the constant. You can search/try to see what interaction series are significant )which essentially forms a minimally sufficient set . Then when you add any needed ARIMA structure you are home along with intervention detection to ferret out unusual values. I do this routinely with great results. . – IrishStat Sep 1 '17 at 0:35

Hyndman's docs say the xreg vector needs to have the same number of rows as the time series. In your code, where defining 'Weekdays' you are missing a comma before the closing square bracket.

If this external regressor approach doesn't work I'd try fitting a seasonal arima model with m=7 manually.