library(readxl, quietly = TRUE, warn.conflicts =  FALSE, verbose = F)
library(fpp2,quietly = TRUE, warn.conflicts =  FALSE, verbose = F)
library(ggplot2)
library(gridExtra)
library(seasonal)

Q1 Excercise 6.2

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

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

Based on the below, there is a positive up trend in the production. Every month production starts of slow and increases up to Jun-Aug then slowdown toward the end of the year.

autoplot(plastics)

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

d <- decompose(plastics, type="multiplicative") 

  autoplot(d) + xlab("Year") +
  ggtitle("Classical multiplicative decomposition")

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

Yes. The trend plot shows the clear uptrend in the data. Seasonal plot shows seasonality.

d. Compute and plot the seasonally adjusted data.

orig <- autoplot(plastics, series="Data") +
  autolayer(seasadj(d), series="Seasonally Adjusted") +
  ggtitle("Sales of Product A ") + 
  ylab("Monthly Sales")
orig

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?

Based on the plot below, we see that the outlier causes a big spike on both plots.

modified <- plastics
modified[20] = modified[20]+ 500 

d <- decompose(modified, type="multiplicative")

 autoplot(modified, series="Data") +
  autolayer(seasadj(d), series="Seasonally Adjusted") +
  ggtitle("Sales of Product A ") + 
  ylab("Monthly Sales")

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

Below we plot the original and modified data. 1st, 30th and 59th observations are modified by adding 500 to the original data. Based on the plots, we see that these outliers cause spikes around modified observation but the impact is temporary or local. These outliers do not impact the overall trend.

modifiedBegining <- plastics
modifiedBegining[1] = modifiedBegining[1]+ 500

dbeg <- decompose(modifiedBegining, type="multiplicative")

modifiedMiddle <- plastics
modifiedMiddle[30] = modifiedMiddle[30]+ 500

dMiddle <- decompose(modifiedMiddle, type="multiplicative")

modifiedEnd <- plastics
modifiedEnd[59] = modifiedEnd[59]+ 500

dEnd <- decompose(modifiedEnd, type="multiplicative")

beg<- autoplot(modifiedBegining, series="Data") +
  autolayer(seasadj(dbeg), series="Seasonally Adjusted") +
  ggtitle("Modified near the begining  ") + 
  ylab("Monthly Sales")

mid <- autoplot(modifiedMiddle, series="Data") +
  autolayer(seasadj(dMiddle), series="Seasonally Adjusted") +
  ggtitle("Modified near the Middle  ") + 
  ylab("Monthly Sales")

end <- autoplot(modifiedEnd, series="Data") +
  autolayer(seasadj(dEnd), series="Seasonally Adjusted") +
  ggtitle("Modified near the end  ") + 
  ylab("Monthly Sales")

grid.arrange(orig, beg, mid, end, nrow =2)

Q4 Excercise 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?

Reminder plot shows few unusual observations around 1987; we did not see these in the earlier plots. Plots below clearly show the positive trend in sales.

retaildata <- readxl::read_excel("retail.xlsx", skip=1)
lts <- ts(retaildata[,"A3349499L"], frequency=12, start=c(1982,4))
 fit <-seas(lts,x11="")
autoplot(fit) +
  ggtitle("X11 Liquor retail")

autoplot(lts, series="Data") +
  autolayer(trendcycle(fit), series="Trend") +
  autolayer(seasadj(fit), series="Seasonally Adjusted") +
  xlab("Year") + ylab("New orders index") +
  ggtitle("Liquor retail") +
  scale_colour_manual(values=c("gray","blue","red"),
             breaks=c("Data","Seasonally Adjusted","Trend"))