Time Series Decomposition

library(fpp2)
library(seasonal)
library(zoo)
library(plotly)

Question 6.2

The plastics data set consists of the monthly sales (in thousands) of product A for a plastics manufacturer for five years.

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

There are seasonal fluctuations in this time series - peaks in the middle of the year and troughs in the beginning and end of each year. The series also shows an upwards trend.

data(plastics)
## Warning in data(plastics): data set 'plastics' not found
ggplotly(autoplot(plastics))
  1. Use a classical multiplicative decomposition to calculate the trend-cycle and seasonal indices.
plastics %>% decompose(type="multiplicative") -> fit
trendcycle(fit)
##         Jan       Feb       Mar       Apr       May       Jun       Jul
## 1        NA        NA        NA        NA        NA        NA  976.9583
## 2 1000.4583 1011.2083 1022.2917 1034.7083 1045.5417 1054.4167 1065.7917
## 3 1117.3750 1121.5417 1130.6667 1142.7083 1153.5833 1163.0000 1170.3750
## 4 1208.7083 1221.2917 1231.7083 1243.2917 1259.1250 1276.5833 1287.6250
## 5 1374.7917 1382.2083 1381.2500 1370.5833 1351.2500 1331.2500        NA
##         Aug       Sep       Oct       Nov       Dec
## 1  977.0417  977.0833  978.4167  982.7083  990.4167
## 2 1076.1250 1084.6250 1094.3750 1103.8750 1112.5417
## 3 1175.5000 1180.5417 1185.0000 1190.1667 1197.0833
## 4 1298.0417 1313.0000 1328.1667 1343.5833 1360.6250
## 5        NA        NA        NA        NA        NA
seasonal(fit)
##         Jan       Feb       Mar       Apr       May       Jun       Jul
## 1 0.7670466 0.7103357 0.7765294 0.9103112 1.0447386 1.1570026 1.1636317
## 2 0.7670466 0.7103357 0.7765294 0.9103112 1.0447386 1.1570026 1.1636317
## 3 0.7670466 0.7103357 0.7765294 0.9103112 1.0447386 1.1570026 1.1636317
## 4 0.7670466 0.7103357 0.7765294 0.9103112 1.0447386 1.1570026 1.1636317
## 5 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
## 2 1.2252952 1.2313635 1.1887444 0.9919176 0.8330834
## 3 1.2252952 1.2313635 1.1887444 0.9919176 0.8330834
## 4 1.2252952 1.2313635 1.1887444 0.9919176 0.8330834
## 5 1.2252952 1.2313635 1.1887444 0.9919176 0.8330834
  1. Do the results support the graphical interpretation from part a?

Yes - the trend cycle numbers are gradually increasing (with a mild decline towards the end of the time series). The seasonal indices are higher in the middle of the year and lower at the start and end of the year. This confirms the seasonality and trend cycle seen from the time plot.

  1. Compute and plot the seasonally adjusted data.
ggplotly(autoplot(seasadj(fit)))
  1. 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?

The outlier results in a sharp spike in the month that it was inserted into.

plastics_outlier <- plastics
plastics_outlier[3] <- plastics[3] + 500
plastics_outlier %>% decompose(type="multiplicative") -> fit
ggplotly(autoplot(seasadj(fit)))
  1. Does it make any difference if the outlier is near the end rather than in the middle of the time series?

The outlier results in a sharp spike wherever it is inserted, whether near the end or the middle.

plastics_outlier <- plastics
plastics_outlier[58] <- plastics[58] + 500
plastics_outlier %>% decompose(type="multiplicative") -> fit
ggplotly(autoplot(seasadj(fit)))

Question 6.3

Recall your retail time series data (from Exercise 3 in Section 2.10). Decompose the series using X11. Does it reveal any outliers, or unusual features that you had not noticed previously?

This reveals a sharp spike right after the year 2000 that I had not previously noticed. Because of the seasonality in the time series, I did not notice the jump in retail sales.

temp = tempfile(fileext = ".xlsx")
dataURL <- "https://otexts.com/fpp2/extrafiles/retail.xlsx"
download.file(dataURL, destfile=temp, mode='wb')
retaildata <- readxl::read_excel(temp, skip=1)
myts <- ts(retaildata[,"A3349396W"], frequency=12, start=c(1982,4))

myts %>% seas(x11="") -> fit
autoplot(seasadj(fit)) +
  ggtitle("X11 decomposition of A3349396W")