library(fpp2)
library(readxl)
library(seasonal)

Decomposition

6.2

a. Plot the time series of sales of product A. Can you identify seasonal fluctuations and/or a trend-cycle?

There is an increasing trend based on a 12 month moving average and there is seasonal fluctuations in which sales are higher in the summer and lower in the winter.

autoplot(plastics) + 
  labs(title = "Plastic Monthly Sales", 
       x = "Year", y = "")

b. Use a classical multiplicative decomposition to calculate the trend-cycle and seasonal indices.

plastics %>% decompose(type = "multiplicative") -> classical
autoplot(classical) + 
  labs(title = "Plastic Monthly Sales",
       x = "Year", y = "")

head(trendcycle(classical), 12)
##        Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
## 1       NA       NA       NA       NA       NA       NA 976.9583 977.0417
##        Sep      Oct      Nov      Dec
## 1 977.0833 978.4167 982.7083 990.4167
head(seasonal(classical), 12)
##         Jan       Feb       Mar       Apr       May       Jun       Jul
## 1 0.7670466 0.7103357 0.7765294 0.9103112 1.0447386 1.1570026 1.1636317
##         Aug       Sep       Oct       Nov       Dec
## 1 1.2252952 1.2313635 1.1887444 0.9919176 0.8330834

c. Do the results support the graphical interpretation from part a?

Yes, the results support the graphical interpretation from part a. The trend is increasing and there is seasonality.

d. Compute and plot the seasonally adjusted data.

autoplot(plastics, series="Data") +
  autolayer(trendcycle(classical), series="Trend") + 
  autolayer(seasadj(classical), series="Seas. Adj.") + 
  labs(title = "Plastic Monthly Sales", 
       x = "Year", y = "") +
  scale_color_brewer(palette = "Set2")

e. Change one observation to be an outlier (e.g., add 500 to one observation), and recompute the seasonally adjusted data. What is the effect of the outlier?

plastics_with_outlier <- plastics
plastics_with_outlier[30] <- plastics_with_outlier[30] + 500
decompose_plastics_with_outlier <- decompose(plastics_with_outlier, type="multiplicative")
seasonally_adjusted_plastics_with_outlier <- plastics_with_outlier / decompose_plastics_with_outlier$seasonal

autoplot(plastics, series = 'Data') +
  autolayer(seasadj(classical), series = 'without outlier') +
  autolayer(seasonally_adjusted_plastics_with_outlier, series = 'with outlier') +
  labs(title = "Plastic Monthly Sales",
       x = "Year", y = "") +
    scale_color_brewer(palette = "Set2")

f. Does it make any difference if the outlier is near the end rather than in the middle of the time series?

There is a difference if the outlier is near the end rather than in the middle of the time series. If the outlier is in the middle of the time series the seasonally-adjusted time series is affected. If the outlier is near either of the ends of the time series the seasonally-adjusted time series would be the only thing affected.

6.3

Decompose the series using X11. Does it reveal any outliers, or unusual features that you had not noticed previously?

There are some spikes in the remainder early on (circa 1983) and around 2000. That indicates the presence of some outliers. Yes, the decomposition using X11 reveals Outliers not noticed previously. They are evident in the remainder component specifically, in the early years of the time series, and in the later years.

retaildata <- readxl::read_excel("retail.xlsx", skip=1)
retail <- ts(retaildata[, "A3349337W"], frequency = 12, start = c(1982, 4))
x11_retail <- seas(retail, x11="")
autoplot(x11_retail) +
  labs(title="X11 Decomposition")