|
72 | 72 |
|
73 | 73 | ```
|
74 | 74 |
|
| 75 | + |
| 76 | +## Dealing with weekly data example |
| 77 | + |
| 78 | +```{r echo=FALSE, message=FALSE, warning=FALSE, Weekly_data} |
| 79 | +
|
| 80 | +# The simplest approach is to use an STL decomposition to the seasonal component along with a non-seasonal method applied to the seasonally adjusted component of data. |
| 81 | +gasoline %>% stlf() %>% autoplot() |
| 82 | +
|
| 83 | +# An alternative approach is to use a dynamic harmonic regression model. Example using gasoline data again. |
| 84 | +gasoline_dreg.best <- list(aicc=Inf) |
| 85 | +
|
| 86 | +for(K in seq(26)){ |
| 87 | + gasoline_dreg <- auto.arima( |
| 88 | + #substitute seasonal component with Fourier terms. |
| 89 | + gasoline, |
| 90 | + xreg=fourier(gasoline, K=K), |
| 91 | + seasonal=FALSE |
| 92 | + ) |
| 93 | + |
| 94 | + if(gasoline_dreg$aicc < gasoline_dreg.best$aicc) |
| 95 | + { |
| 96 | + gasoline_dreg.best <- gasoline_dreg |
| 97 | + gasoline_K.best <- K |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +# forecast the next 2 years of data. If I assume that 1 year is about 52 weeks, forecast horizon(h) is 104. |
| 102 | +fc_gasoline_dreg.best <- forecast( |
| 103 | + gasoline_dreg.best, |
| 104 | + xreg=fourier(gasoline, K=gasoline_K.best, h=104) |
| 105 | + ) |
| 106 | +
|
| 107 | +autoplot(fc_gasoline_dreg.best) |
| 108 | +
|
| 109 | +# A third approach is the TBATS model. This was the subject of Question 11.2. |
| 110 | +gasoline_tbats <- tbats(gasoline) |
| 111 | +
|
| 112 | +checkresiduals(gasoline_tbats) |
| 113 | +# The residuals aren't like white noise. |
| 114 | +
|
| 115 | +fc_gasoline_tbats <- forecast(gasoline_tbats) |
| 116 | +autoplot(fc_gasoline_tbats) |
| 117 | +# The forecasts from above 3 methods are similar to each other. |
| 118 | +# At the question 11.2, I thought that the dynamic harmonic regression model will be best for the data because of unlinearity in trend. |
| 119 | +
|
| 120 | +# If there's an unlinearity of trend or irregular seasonality in the data, dynamic regression model with dummy variable(s) will be the only choice. This fact can be applied to daily or sub-daily data, too. |
| 121 | +
|
| 122 | +``` |
| 123 | + |
0 commit comments