library(fpp2)
library(scales)
library(ggplot2)
library(seasonal) # for X11
library(gridExtra)
The plastics dataset 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?
# Basic Autoplot
autoplot(plastics) + labs(title="Monthly Sales - Product A", x="Time", y="Sales") + scale_y_continuous(labels= dollar_format(scale=1)) +
theme_classic()
# Checking for seasonality
ggseasonplot(plastics, year.labels = TRUE) + labs(title="Monthly Sales - Product A", x="Time", y="Sales") + scale_y_continuous(labels= dollar_format(scale=1)) + theme_classic()
ggsubseriesplot(plastics, year.labels= TRUE) + labs(title="Monthly Sales - Product A", x="Time", y="Sales") + scale_y_continuous(labels= dollar_format(scale=1)) + theme_classic()
From the above plots, we can absolutely say that there is seasonality in the trend. In From January it starts getting increased all the way between August and September. From September onwards, it starts decreasing again. Overall there is an slight upward trend in the sales throughout five years.
Use a classical multiplicative decomposition to calculate the trend-cycle and seasonal indices.
plastics %>% decompose(type="multiplicative") %>% autoplot() + labs(title="Classic Multiplicative Decomposition")
Do the results support the graphical interpretation from part a?
Yes, the results do support interpretation from part a. Overall, there is a upward slight trend which is slowly increasing over the years. It also does support that there is seasonality in the data which increases in January till September and decreases again but this plot does not contain labels that’s why I cannot prove it.
Compute and plot the seasonally adjusted data.
season.adj <- plastics %>% decompose(type='multiplicative')
autoplot(plastics, series="Data")+ autolayer(seasadj(season.adj), series=" Seasonally Adjusted") +
labs(title="Seasonally Adjusted vs Data")
Seasonally adjusted plot indicates any variation occured due to any event rather than any seasonality which is very helpful to consider sometimes.
Change one observation to be an outlier (eg, add 500 to on observation), and recompute the seasonally adjusted data. What is the effect of the outlier?
# Replacing a value with 500
plastics2 <- plastics
plastics2[6] <- plastics[6] + 500
# Recomputing the seasonally adjusted plot
season.adj <- plastics2 %>% decompose(type='multiplicative')
autoplot(plastics2, series="Data")+ autolayer(seasadj(season.adj), series=" Seasonally Adjusted") +
labs(title="Seasonally Adjusted vs Data")
# Decompsition
plastics2 %>% decompose(type='multiplicative') %>% autoplot()
Outlier caused spike in the seaonally adjusted plot because it was not identified as a seasonal factor but rather as an event that’s why it was captured but later on it’s effect was reduced into the remainder component. It has small affect on the trend overall. We can also in the Decomposition plot as well. It did affect a little in the beginning but overall its affect was absorbed.
Does it make any difference if the outlier is near the end rather than in the middle of the time series
# Making a copy and addign outlier to the end
plastics3 <- plastics
plastics3[52] <- plastics3[52] + 500
# Recomputing the seasonally adjusted plot
season.adj <- plastics3 %>% decompose(type='multiplicative')
autoplot(plastics3, series="Data")+ autolayer(seasadj(season.adj), series=" Seasonally Adjusted") +
labs(title="Seasonally Adjusted vs Data")
# Decompsition
plastics3 %>% decompose(type='multiplicative') %>% autoplot()
I think the concept is same. Seasonally adjusted reduces all the seasonality while in this case this was just an event or outlier either in the middle or end. There should be a spike initially because of that particular event and then its effect should be absorbed in the trend. It does not matter where it falls the affect would be always same.
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.
retail <- readxl::read_excel("retail.xlsx", skip = 1) # reading the excel file
retail_x11 <- ts(retail[,8], frequency=12, start=c(1982,4)) # Selecting a column and converting into time series object
# Basic autoplot
autoplot(retail_x11)
retail_x11 %>% seas(x11="") %>% autoplot() + labs(title=" X11 Decomposition")
Overall, it seems that the trend is going upwards with some fluctuation. Although the x11 decomposition plot shows the same upward trend overall but seasonality is changing which means that sales is decreasing in that seasonal period throughout the years. There are some significant spikes and drops at few points in remainder. Significant ones are at 1983 and 2001. This shows an interesting point because basic autoplot does not go into these details which were just identified with decomposition plot using x11.