## ── Attaching packages ────────────────────────────────────────────────────────── fpp3 0.3 ──
## ✓ tibble      3.0.3     ✓ tsibble     0.9.2
## ✓ dplyr       1.0.2     ✓ tsibbledata 0.2.0
## ✓ tidyr       1.1.2     ✓ feasts      0.1.5
## ✓ lubridate   1.7.9     ✓ fable       0.2.1
## ✓ ggplot2     3.3.2
## ── Conflicts ───────────────────────────────────────────────────────────── fpp3_conflicts ──
## x lubridate::date()   masks base::date()
## x dplyr::filter()     masks stats::filter()
## x tsibble::interval() masks lubridate::interval()
## x dplyr::lag()        masks stats::lag()
## Loading required package: forecast
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Loading required package: fma
## Loading required package: expsmooth
## 
## Attaching package: 'seasonal'
## The following object is masked from 'package:tibble':
## 
##     view

DATA 624 HW #3

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

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

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
autoplot(plastics) + xlab("Month") + ylab("Sales (in thousands)") +
  ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")

There is seasonality in the sales for plastic A where we see increased sales from spring through summer and then a decrease in sales in the fall and winter months. Overall, there is a general increased positive trend year over year.

ggseasonplot(plastics)

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

plastics %>% decompose(type="multiplicative") %>%
  autoplot() + xlab("Month") +
  ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")

Do the results support the graphical interpretation from part a?

Yes, the results support the interpretation from part A where it shows strong seasonality and an upward trend in sales numbers.

Compute and plot the seasonally adjusted data.

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

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_outlier <- plastics
plastics_outlier[30] <- plastics_outlier[30] + 500

plastics_outlier %>% decompose(type="multiplicative") -> ofit
autoplot(plastics_outlier, series = "Data") +
  autolayer(trendcycle(ofit), series = "Trend") +
  autolayer(seasadj(ofit), series = "Seasonally Adjusted")
## Warning: Removed 12 row(s) containing missing values (geom_path).

plastics_outlier %>% decompose(type="multiplicative") %>%
  autoplot() + xlab("Month") +
  ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")

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

plastics_outlier_end <- plastics
plastics_outlier_end[50] <- plastics_outlier_end[50] + 500

plastics_outlier_end %>% decompose(type="multiplicative") -> ofit
autoplot(plastics_outlier_end, series = "Data") +
  autolayer(trendcycle(ofit), series = "Trend") +
  autolayer(seasadj(ofit), series = "Seasonally Adjusted")
## Warning: Removed 12 row(s) containing missing values (geom_path).

plastics_outlier_end %>% decompose(type="multiplicative") %>%
  autoplot() + xlab("Month") +
  ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")

There seems to be a less drastic spike when the outlier is towards the end, this may be in part to the fact that the trend is increasing over time. Overall, this value would still be considered an outlier, but it seems to affect the trend less when at the end.

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?

myts <- ts(retaildata[,"A3349413L"],
  frequency=12, start=c(1982,4))
autoplot(myts)+
  xlab("Year") + ylab("Sales") +
  ggtitle("Retail Data")

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

The remainder shows outliers around 2000-2001 which may not have been previously noticed.