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

autoplot(plastics)

# Answer: Yes there is a seasonal fluctuation (peaks in the latter part of the summer) and upward trend. I cannot see an obvious cycle.

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

 plastics %>% decompose(type="multiplicative") %>%
  autoplot() + xlab("Year") +
  ggtitle("Classical multiplicative decomposition")

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

Answer: Yes trend and cycle appears as per graphical interpretation.

d. Compute and plot the seasonally adjusted data.

seasadj_plastics_fit<-decompose(plastics,type="multiplicative")
seasadj_plastics_data <-seasadj(seasadj_plastics_fit)
autoplot(seasadj_plastics_data)

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[10]<-plastics_with_outlier[10]+500
seasadj_plastics_fit_with_outlier<-decompose(plastics_with_outlier,type="multiplicative")
seasadj_plastics_data_with_outlier <-seasadj(seasadj_plastics_fit_with_outlier)
autoplot(seasadj_plastics_data_with_outlier)

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

plastics_with_outlier <- plastics
plastics_with_outlier[60]<-plastics_with_outlier[60]+500
seasadj_plastics_fit_with_outlier<-decompose(plastics_with_outlier,type="multiplicative")
seasadj_plastics_data_with_outlier <-seasadj(seasadj_plastics_fit_with_outlier)
autoplot(seasadj_plastics_data_with_outlier)

plastics_with_outlier %>% decompose(type="multiplicative") %>%
  autoplot() + xlab("Year") +
  ggtitle("Classical multiplicative decomposition")

plastics_with_outlier_mid <- plastics
plastics_with_outlier_mid [30]<-plastics_with_outlier_mid[30]+500
seasadj_plastics_fit_with_outlier_mid<-decompose(plastics_with_outlier_mid,type="multiplicative")
seasadj_plastics_data_with_outlier_mid <-seasadj(seasadj_plastics_fit_with_outlier_mid)
autoplot(seasadj_plastics_data_with_outlier_mid)

plastics_with_outlier_mid %>% decompose(type="multiplicative") %>%
  autoplot() + xlab("Year") +
  ggtitle("Classical multiplicative decomposition")

# Answer : There is a visible seaosonality bump when the outlier is in the middle, as opposed to none when the outlier is added to the end.

6.2 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?

library(seasonal)

retaildata <- readxl::read_excel("retail.xlsx", skip=1)
myts <- ts(retaildata[,"A3349335T"],
  frequency=12, start=c(1982,4))

series <- myts

library(seasonal)
myts %>% seas(x11="") -> fit
autoplot(fit) +
  ggtitle("X11 decomposition ")

# Answer : A few outliers emerged that I have not see before ex: Dec 1989:1.0528002. Another feature I have not noticed is how seasonality is more pronounced at the beginning of the series and then its becomes less peaked.