#loading the needed libraries 
library(tidyr)
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(knitr)
library(utils)
library(ggplot2)
library(forecast)
## Warning: package 'forecast' was built under R version 3.5.3
library(readxl)
## Warning: package 'readxl' was built under R version 3.5.2
library(fpp2)
## Warning: package 'fpp2' was built under R version 3.5.3
## Loading required package: fma
## Warning: package 'fma' was built under R version 3.5.3
## Loading required package: expsmooth
## Warning: package 'expsmooth' was built under R version 3.5.3
#cleaning the environment
rm(list=ls())

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?

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

Do the results support the graphical interpretation from part a?

Compute and plot the seasonally adjusted data.

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? Does it make any difference if the outlier is near the end rather than in the middle of the time series?

help(plastics)
## starting httpd help server ... done
head(plastics)
##    Jan  Feb  Mar  Apr  May  Jun
## 1  742  697  776  898 1030 1107
autoplot(plastics) +
  ggtitle("Monthly Sales of product A for a plastic manufacturer") +
  xlab("Years") +
  ylab("Monthly Sales")

#a. There are definitely seasonal fluctuations and an upward trend. 
multiplicative_decomposition <- decompose(plastics, type = "multiplicative")
autoplot(multiplicative_decomposition)

#c. Yes they do. 
autoplot(plastics, series="Data") +
  autolayer(trendcycle(multiplicative_decomposition), series="Trend") +
  autolayer(seasadj(multiplicative_decomposition), series="Seasonally Adjusted") +
  xlab("Year") + ylab("Monthly Sales in Thousands") +
  ggtitle("Plastic Product Sales") +
  scale_colour_manual(values=c("gray","blue","red"), breaks=c("Data","Seasonally Adjusted","Trend"))
## Warning: Removed 12 rows containing missing values (geom_path).

outliered_plastics <- plastics
outliered_plastics[5] <- outliered_plastics[5] + 500
multiplicative_decomposition_new <- decompose(outliered_plastics, type = "multiplicative")

autoplot(outliered_plastics, series = "Data") +
  autolayer(trendcycle(multiplicative_decomposition_new), series = "Trend") +
  autolayer(seasadj(multiplicative_decomposition_new), series = "Seasonally Adjusted") +
  xlab("Year") + ylab("Monthly Sales in Thousands") +
  ggtitle("Plastic Product Sales") +
  scale_color_manual(values=c("gray", "blue", "red"), breaks=c("Data", "Seasonally Adjusted", "Trend"))
## Warning: Removed 12 rows containing missing values (geom_path).

#The Seasonally Adjusted indices change significantly while the Trend doesn't change much. It seems that it matter for the Seasonally Adjusted indices where the oulier is but not so much for the Trend.