#install.packages("seasonal")
library(GGally)
## Loading required package: ggplot2
library(fpp2)
## Loading required package: forecast
## Loading required package: fma
##
## Attaching package: 'fma'
## The following object is masked from 'package:GGally':
##
## pigs
## Loading required package: expsmooth
library(readxl)
library(seasonal)
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?
autoplot(plastics) + xlab("Year") + ylab("No of sales in thousands") +
ggtitle("Monthly sales of a plastic product sales")
Yes, I can identify that the data has an upward trend and has a almost constant seasonality
autoplot(plastics, series="Data") +
autolayer(ma(plastics, 12), series="12-MA") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics Sales") +
scale_colour_manual(values=c("Data"="grey","12-MA"="red"),
breaks=c("Data","12-MA"))
plastics %>% decompose(type="multiplicative") %>%
autoplot() + xlab("Year") +
ggtitle("Classical multiplicative decomposition
of plastics sales index")
Yes the result matches the graphical representation
Since the seas() requires years greater than 1000. We will assume that the year started from 2000 up to 2005
plastics2 <- plastics %>%
ts(start=2000, frequency=12)
Let’s plot the X11 decomposition
plastics2 %>% seas(x11="") -> fit
autoplot(fit) +
ggtitle("X11 decomposition of plastics sales index")
Let’s plot the seasonal adjusted chart of X11 decomposition
autoplot(plastics2, series="Data") +
autolayer(trendcycle(fit), series="Trend") +
autolayer(seasadj(fit), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using X11 decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
Let’s try the same problem using the multiplicative decomposition
plastics %>% decompose(type="multiplicative") -> fit2
autoplot(fit2) +
ggtitle("Multiplicative decomposition of plastics sales index")
autoplot(plastics, series="Data") +
autolayer(trendcycle(fit2), series="Trend") +
autolayer(seasadj(fit2), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using Multiplicative Decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
Let’s try the same problem using the additive decomposition
plastics %>% decompose(type="additive") -> fit3
autoplot(fit3) +
ggtitle("Additive decomposition of plastics sales index")
autoplot(plastics, series="Data") +
autolayer(trendcycle(fit3), series="Trend") +
autolayer(seasadj(fit3), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using additive decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
I will add 1000 to the Year 1 November record.
plastics3 <- plastics
plastics3[11]<-plastics3[11]+1000
Let’s plot the data
autoplot(plastics3)
Let’s try the same problem using the additive decomposition
plastics3 %>% decompose(type="additive") -> fit3
autoplot(fit3) +
ggtitle("Additive decomposition of plastics sales index")
autoplot(plastics3, series="Data") +
autolayer(trendcycle(fit3), series="Trend") +
autolayer(seasadj(fit3), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using additive decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
Let’s plot the X11 decomposition
plastics4 <- plastics3 %>%
ts(start=2000, frequency=12)
plastics4 %>% seas(x11="") -> fit
autoplot(fit) +
ggtitle("X11 decomposition of plastics sales index")
Let’s plot the seasonal adjusted chart of X11 decomposition
autoplot(plastics4, series="Data") +
autolayer(trendcycle(fit), series="Trend") +
autolayer(seasadj(fit), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using X11 decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
Let’s try the same problem using the multiplicative decomposition
plastics3 %>% decompose(type="multiplicative") -> fit2
autoplot(fit2) +
ggtitle("Multiplicative decomposition of plastics sales index")
autoplot(plastics3, series="Data") +
autolayer(trendcycle(fit2), series="Trend") +
autolayer(seasadj(fit2), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using Multiplicative Decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
Summary,
This shows the outlier has an effect on the seasonally adjusted plot especially for the classical decomposition. In the X11 it does not have much effect
I think it does make a difference for the mulitplicative and addiditve decompositions. However, let’s test it out in the multiplicative by adding the outlier at the end and at the middle then comparing
At the middle outlier
plastics5 <- plastics
plastics5[30]<-plastics5[30]+1000
Let’s try the same problem using the multiplicative decomposition
plastics5 %>% decompose(type="multiplicative") -> fit2
autoplot(fit2) +
ggtitle("Multiplicative decomposition of plastics sales index")
autoplot(plastics5, series="Data") +
autolayer(trendcycle(fit2), series="Trend") +
autolayer(seasadj(fit2), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using Multiplicative Decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
At the ende outlier
plastics6 <- plastics
plastics6[60]<-plastics6[60]+1000
Let’s try the same problem using the multiplicative decomposition
plastics6 %>% decompose(type="multiplicative") -> fit2
autoplot(fit2) +
ggtitle("Multiplicative decomposition of plastics sales index")
autoplot(plastics6, series="Data") +
autolayer(trendcycle(fit2), series="Trend") +
autolayer(seasadj(fit2), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Plastics sales using Multiplicative Decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
The above plots confirm my supicion that their will be a difference with the seasonality of other earlier years if the outlier is at the middle vs the end. At the end we see the effect is not much but at the middle the effect is significant.
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("C:/Users/Mezu/Documents/retail.xlsx", skip=1)
myts <- ts(retaildata[,"A3349873A"],
frequency=12, start=c(1982,4))
autoplot(myts)
Let’s plot the X11 decomposition
myts %>% seas(x11="") -> fit
autoplot(fit) +
ggtitle("X11 decomposition of Retail sales index")
Let’s plot the seasonal adjusted chart of X11 decomposition
autoplot(myts, series="Data") +
autolayer(trendcycle(fit), series="Trend") +
autolayer(seasadj(fit), series="Seasonally Adjusted") +
xlab("Year") + ylab("New orders index") +
ggtitle("Retail sales using X11 decomposition") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
This data does not really show a significant outlier. However, there is a case to be made for after the year 2000. There seems to be a significant drop in sales more than normal. We see this in the residual plot