Install Libraries

#Libraries
#install.packages(forecast)
library(forecast)
## Warning: package 'forecast' was built under R version 3.4.4
## Warning in as.POSIXlt.POSIXct(Sys.time()): unknown timezone 'zone/tz/2020a.
## 1.0/zoneinfo/America/New_York'
library(fpp2)
## Warning: package 'fpp2' was built under R version 3.4.4
## Loading required package: ggplot2
## Loading required package: fma
## Loading required package: expsmooth
library(ggplot2)

Question 6.2

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

6.2A

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

Yes. There is a slight upward trend and a seasaonality effect occuring in the middle of each year.

autoplot(plastics) + ggtitle('Product A Time Series')

6.2B

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

plastics %>% decompose(type = 'multiplicative') %>% autoplot() + ggtitle('Multiplicative Decomposition - Product A ')

6.2C

Do the results support the graphical interpretation from part a? Yes!

# Yes

6.2D

Compute and plot the seasonally adjusted data.

plastics %>% decompose(type="multiplicative") -> fit
autoplot(plastics, series = "Data") +  ggtitle('Seasonally Adjusted - Product A ') + autolayer(seasadj(fit), series = "Seasonally Adjusted")

6.2E

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 caused a spike in the seasonally adjusted data.

plastics
##    Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
## 1  742  697  776  898 1030 1107 1165 1216 1208 1131  971  783
## 2  741  700  774  932 1099 1223 1290 1349 1341 1296 1066  901
## 3  896  793  885 1055 1204 1326 1303 1436 1473 1453 1170 1023
## 4  951  861  938 1109 1274 1422 1486 1555 1604 1600 1403 1209
## 5 1030 1032 1126 1285 1468 1637 1611 1608 1528 1420 1119 1013
plastics -> plastics_outlier
plastics_outlier[33] <- plastics_outlier[33] + 500
plastics_outlier %>% decompose(type="multiplicative") -> fit
autoplot(plastics_outlier, series = "Data") + autolayer(seasadj(fit), series = "Seasonally Adjusted") +  ggtitle('Seasonally Adjusted - Product A With Outlier - Middle of Series')

6.2F

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

No it does not. The same effect occurs.

plastics
##    Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
## 1  742  697  776  898 1030 1107 1165 1216 1208 1131  971  783
## 2  741  700  774  932 1099 1223 1290 1349 1341 1296 1066  901
## 3  896  793  885 1055 1204 1326 1303 1436 1473 1453 1170 1023
## 4  951  861  938 1109 1274 1422 1486 1555 1604 1600 1403 1209
## 5 1030 1032 1126 1285 1468 1637 1611 1608 1528 1420 1119 1013
plastics -> plastics_outlier
plastics_outlier[56] <- plastics_outlier[56] + 500
plastics_outlier %>% decompose(type="multiplicative") -> fit
autoplot(plastics_outlier, series = "Data") + autolayer(seasadj(fit), series = "Seasonally Adjusted") + ggtitle('Seasonally Adjusted - Product A With Outlier - End of Series')

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?

No I do not see any outliers or unusual features that I had not notice previously.

library(readxl)
## Warning: package 'readxl' was built under R version 3.4.4
retaildata <- readxl::read_excel("retail.xlsx", skip=1)
## readxl works best with a newer version of the tibble package.
## You currently have tibble v1.3.4.
## Falling back to column name repair from tibble <= v1.4.2.
## Message displays once per session.
myts <- ts(retaildata[,"A3349873A"],frequency=12, start=c(1982,4))
autoplot(myts)

library(seasonal)
myts %>% seas(x11="") -> fit
autoplot(fit) +
  ggtitle("X11 Decomposition of Retail Time Series")