The plastics data set consists of the monthly sales (in thousands) of product A for a plastics manufacturer for five years.
library(fpp2)
## Loading required package: ggplot2
## Loading required package: forecast
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Loading required package: fma
## Loading required package: expsmooth
#help(plastics)
autoplot(plastics) +
xlab('Year') +
ylab('Sales(thousands)') +
ggtitle('Sales of plastic product')
Clearly there is a upward trend, seasonality and cyclic.
plastics %>% decompose(type="multiplicative") %>%
autoplot() + xlab("Year") +
ggtitle("Classical multiplicative decomposition
of plastic product Sales")
Yes, After decomposing, There is still an upward trend, seasonality.In the residual plot, i see some randomness towards at the end.
#install.packages('seasonal')
library(seasonal)
# use the same classical multiplicative decompisition
plastics %>% decompose(type="multiplicative")-> fit
autoplot(plastics, series="Data") +
autolayer(trendcycle(fit), series="Trend") +
autolayer(seasadj(fit), series="Seasonally Adjusted") +
xlab("Year") + ylab("Sales(Thousand)") +
ggtitle("Plastic Sales") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
## Warning: Removed 12 row(s) containing missing values (geom_path).
Lets add an outlier(in- fact a big value than normal) to somewhere middle of the obseration
plastics_cp1 <- plastics
plastics_cp1[32] <- 5000
plastics_cp1 %>% decompose(type="multiplicative")-> fit
autoplot(plastics_cp1, series="Data") +
autolayer(trendcycle(fit), series="Trend") +
autolayer(seasadj(fit), series="Seasonally Adjusted") +
xlab("Year") + ylab("Sales(Thousand)") +
ggtitle("Plastic Sales") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
## Warning: Removed 12 row(s) containing missing values (geom_path).
Observation
The trend and seasonal component has caused a spike at center to accommodate the outlier.
Lets add the same outlier towards the end of the series. We see that the same spike is observed towards the end of the series. Recall that classical decomposition methods are not robust to outliers, may need alternate methods to get better representation for trend-cycle and seasonal pattern.
plastics_cp2 <- plastics
plastics_cp2[50] <- 5000
plastics_cp2 %>% decompose(type="multiplicative")-> fit
autoplot(plastics_cp2, series="Data") +
autolayer(trendcycle(fit), series="Trend") +
autolayer(seasadj(fit), series="Seasonally Adjusted") +
xlab("Year") + ylab("Sales(Thousand)") +
ggtitle("Plastic Sales") +
scale_colour_manual(values=c("gray","blue","red"),
breaks=c("Data","Seasonally Adjusted","Trend"))
## Warning: Removed 12 row(s) containing missing values (geom_path).
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?
Lets load the retail time series data and plot the original time-series
retaildata <- readxl::read_excel('C:\\Users\\charls.joseph\\Documents\\Cuny\\Data624\\week1\\retail.xlsx', skip=1)
myts <- ts(retaildata[,"A3349397X"],
frequency=12, start=c(1982,4))
autoplot(myts) +
xlab('Year') +
ylab("Turn Over")
Lets decompose the retail timeseries using X11 method
myts %>% seas(x11="") -> fit
autoplot(fit) +
ggtitle("X11 decomposition of retail Turn Over")
I had previously noted an upward trend and clear seasonality from the original plot. But after decomposing and looking at the reminder, I see there is an outlier somewhere in 2001. I havent noticed that before.