suppressMessages(library(fpp2))
library(seasonal)

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?

we can see a postive/up trend cicle and sensational increase.

autoplot(plastics)

ggseasonplot(plastics)

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

 autoplot(plastics %>% decompose(type="multiplicative"))

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

Yes. we can see the senasational trend on the graph which goes increasing by months. starting low sales on january and goes up.

d. Compute and plot the seasonally adjusted data.

plastics %>% decompose(type="multiplicative") -> fit
autoplot(plastics, series="Data") +
  autolayer(seasadj(fit), series="Seas. Adj.") + 
  labs(title = "Plastic Sales", 
       x = "", y = "") +
  scale_colour_manual(values=c("blue","green","red"),
             breaks=c("Data","Trend","Seas. Adj."))

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?

#beginning
outliers_plastic <- plastics
outliers_plastic[1] <- outliers_plastic[1] + 500

autoplot(outliers_plastic %>% decompose(type="multiplicative"))

plastics %>% decompose(type="multiplicative") -> fit
autoplot(outliers_plastic, series="Data") +
  autolayer(seasadj(fit), series="Seas. Adj.") + 
  labs(title = "Plastic Sales", 
       x = "", y = "") +
  scale_colour_manual(values=c("blue","green","red"),
             breaks=c("Data","Trend","Seas. Adj."))

# the middle 
outliers_plastic2 <- plastics
outliers_plastic2[30] <- outliers_plastic2[30] + 500

autoplot(outliers_plastic2 %>% decompose(type="multiplicative"))

plastics %>% decompose(type="multiplicative") -> fit
autoplot(outliers_plastic2, series="Data") +
  autolayer(seasadj(fit), series="Seas. Adj.") + 
  labs(title = "Plastic Sales", 
       x = "", y = "") +
  scale_colour_manual(values=c("blue","green","red"),
             breaks=c("Data","Trend","Seas. Adj."))

# at the end
outliers_plastic3 <- plastics
outliers_plastic3[58] <- outliers_plastic[58] + 500

autoplot(outliers_plastic3 %>% decompose(type="multiplicative"))

plastics %>% decompose(type="multiplicative") -> fit
autoplot(outliers_plastic3, series="Data") +
  autolayer(seasadj(fit), series="Seas. Adj.") + 
  labs(title = "Plastic Sales", 
       x = "", y = "") +
  scale_colour_manual(values=c("blue","green","red"),
             breaks=c("Data","Trend","Seas. Adj."))

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

Yes, it makes a difference depending on where the outliers are. We can see in the graph that where the outliers are located shows an increasing point. it also shows how to affect the sensational pattern on the graph.

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?

retaildata <- readxl::read_excel("retail.xlsx", skip=1)


myts <- ts(retaildata[,"A3349709X"],
  frequency=12, start=c(1982,4))
autoplot(myts)

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

After comparing both we can see the graph shows both increasing patterns but in the decomposition- sensational we have seen a decrease in the middle and again an increase at the end. in the reminder graph we also can see more clear an increase around 2000 -02 which seems more noticeable after the decomposition.