load("workspace.RData")
library(fpp2)

Three components: Trend Cycle, seasonal, random Why: 1. to understand the series 2. to Improve the accuracy of forecasting Moving Average To Understand trend-cycle eg. if we have a data

  autoplot(elecsales) + xlab("Year") + ylab("GWh") +
ggtitle("Annual electricity sales: South Australia")

We can compute 5-MA

ma(elecsales, 5)
## Time Series:
## Start = 1989 
## End = 2008 
## Frequency = 1 
##  [1]       NA       NA 2381.530 2424.556 2463.758 2552.598 2627.700
##  [8] 2750.622 2858.348 3014.704 3077.300 3144.520 3188.700 3202.320
## [15] 3216.940 3307.296 3398.754 3485.434       NA       NA

Lets plot it along with the original data

autoplot(elecsales, series="Data") +
autolayer(ma(elecsales,5), series="5-MA") +
xlab("Year") + ylab("GWh") +
ggtitle("Annual electricity sales: South Australia") +
scale_colour_manual(values=c("Data"="grey50","5-MA"="red"),
breaks=c("Data","5-MA"))
## Warning: Removed 4 rows containing missing values (geom_path).

Decomposition can be additive or multiplicative

elecequip %>% decompose(type="multiplicative") %>%
autoplot() + xlab("Year") +
ggtitle("Classical multiplicative decomposition
of electrical equipment index")

STL Decomposition Seasonal and Trend Decomposition using Loess.

elecequip %>%
stl(t.window=13, s.window="periodic", robust=TRUE) %>%
autoplot()

Both t.window and s.window should be odd. numbers and refer to the number of consecutive years to be used when estimating the trend-cycle and seasonal components respectively. The user must specify s.window as there is no default.

Forecasting with Decomposition

To forecast a decomposed time series, we forecast the seasonal component, and the seasonally adjusted component , separately. It is usually assumed that the seasonal component is unchanging, or changing extremely slowly, so it is forecast by simply taking the last year of the estimated component. In other words, a seasonal naïve method is used for the seasonal component.

To forecast the seasonally adjusted component, any non-seasonal forecasting method may be used. For example, a random walk with drift model, or Holt’s method or a non-seasonal ARIMA model may be used.

eg. electric equipment manufacturing

fit <- stl(elecequip, t.window=13, s.window="periodic",
robust=TRUE)
fit %>% seasadj() %>% naive() %>%
autoplot() + ylab("New orders index") +
ggtitle("Naive forecasts of seasonally adjusted data")

or you can use forecast, it by default uses STL

fit %>% forecast(method="naive") %>%
autoplot() + ylab("New orders index")

Or use stlf() method

fcast <- stlf(elecequip, method='naive')
fcast
##          Point Forecast    Lo 80     Hi 80    Lo 95     Hi 95
## Apr 2012       83.94889 79.78271  88.11506 77.57727  90.32050
## May 2012       84.45571 78.56384  90.34757 75.44488  93.46653
## Jun 2012       98.91542 91.69939 106.13145 87.87946 109.95138
## Jul 2012       88.01048 79.67813  96.34283 75.26725 100.75371
## Aug 2012       73.15820 63.84235  82.47405 58.91083  87.40556
## Sep 2012       96.78103 86.57603 106.98604 81.17382 112.38824
## Oct 2012       92.59649 81.57383 103.61916 75.73878 109.45420
## Nov 2012       93.71118 81.92745 105.49490 75.68953 111.73283
## Dec 2012      101.06159 88.56307 113.56012 81.94675 120.17644
## Jan 2013       83.77325 70.59865  96.94786 63.62443 103.92207
## Feb 2013       82.48811 68.67047  96.30575 61.35585 103.62037
## Mar 2013       97.80000 83.36794 112.23206 75.72807 119.87193
## Apr 2013       83.94889 68.92753  98.97025 60.97570 106.92208
## May 2013       84.45571 68.86730 100.04411 60.61530 108.29611
## Jun 2013       98.91542 82.77989 115.05095 74.23826 123.59258
## Jul 2013       88.01048 71.34578 104.67518 62.52401 113.49694
## Aug 2013       73.15820 55.98061  90.33578 46.88735  99.42904
## Sep 2013       96.78103 79.10544 114.45662 69.74855 123.81351
## Oct 2013       92.59649 74.43655 110.75643 64.82326 120.36972
## Nov 2013       93.71118 75.07947 112.34288 65.21644 122.20591
## Dec 2013      101.06159 81.96978 120.15341 71.86318 130.26001
## Jan 2014       83.77325 64.23216 103.31435 53.88772 113.65878
## Feb 2014       82.48811 62.50783 102.46839 51.93091 113.04531
## Mar 2014       97.80000 77.38999 118.21001 66.58558 129.01442
save.image("workspace.Rdata")