Exercise from Chapter 6

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 sales of product A. Can you identify seasonal fluctuations and/or a trend-cycle?

autoplot(plastics)+defaulttheme

ggseasonplot(plastics, year.labels = T, year.labels.left = T)+defaulttheme

ggsubseriesplot(plastics)+defaulttheme

yes, seasonal fluctuations are identified in the dataset where there is an increase in plastic manufacturing in the summer months that subside in colder months as shown in both the subseries and seasonal plots. additionally, there is a trend cycle where there is a general increase in production over time.

b.

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

plastics_decomp_mult<-
  plastics %>% decompose(type="multiplicative") 

plastics_decomp_mult %>%  autoplot() + defaulttheme

c. 

Do the results support the graphical interpretation from part a?

Yes the results do support the interpretations from part a as the trend portion of the series increases overtime and constant seasonality is depicted in the seasonal portion of the data.

d.

Compute and plot the seasonally adjusted data.

autoplot(plastics, series = "Raw Data")+
  autolayer(seasadj(plastics_decomp_mult), series = "Seasonally Adjusted Decomposition")+
  autolayer(trendcycle(plastics_decomp_mult), series = "Trend Decomposition")+
  labs(title = "Plastics", subtitle = "Seasonally Adjusted")+defaulttheme

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 outler?

Randval<-(1:length(plastics))

set.seed(435)
valchange<-sample(Randval,1)

plastics_out<-plastics

plastics_out[valchange]<-plastics_out[valchange]+500

plastics_out_decomp_mult<-
  plastics_out %>% decompose(type="multiplicative") 

plastics_out_decomp_mult %>%  autoplot() + defaulttheme

autoplot(plastics, series = "Raw Data")+
  autolayer(seasadj(plastics_out_decomp_mult), series = "Seasonally Adjusted Decomposition")+
  autolayer(trendcycle(plastics_out_decomp_mult), series = "Trend Decomposition")+
  labs(title = "Plastics", subtitle = "Seasonally Adjusted")+defaulttheme

the effect of the seasonally adjusted data is an extreme event creating an abberation in the seasonal decomposition that is not seen in the non-outlier dataset

f. 

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

plastics_out<-plastics

plastics_out[2]<-plastics[2]+500

plastics_out_decomp_mult<-
  plastics_out %>% decompose(type="multiplicative") 

autoplot(plastics, series = "Raw Data")+
  autolayer(seasadj(plastics_out_decomp_mult), series = "Seasonally Adjusted Decomposition")+
  autolayer(trendcycle(plastics_out_decomp_mult), series = "Trend Decomposition")+
  labs(title = "Plastics", subtitle = "Seasonally Adjusted")+defaulttheme

plastics_out<-plastics
plastics_out[55]<-plastics[55]+500

plastics_out_decomp_mult<-
  plastics_out %>% decompose(type="multiplicative") 

autoplot(plastics, series = "Raw Data")+
  autolayer(seasadj(plastics_out_decomp_mult), series = "Seasonally Adjusted Decomposition")+
  autolayer(trendcycle(plastics_out_decomp_mult), series = "Trend Decomposition")+
  labs(title = "Plastics", subtitle = "Seasonally Adjusted")+defaulttheme

the effect of the outlier on where it occurs in the time series seems to have similar effect where there is a brief period of a spike in seasonality that then subsides back to the original series regardless of where this occurs.

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?

library(httr)
url1<-"https://otexts.com/fpp2/extrafiles/retail.xlsx"
GET(url1, write_disk(tf <- tempfile(fileext = ".xlsx")))
## Response [https://otexts.com/fpp2/extrafiles/retail.xlsx]
##   Date: 2021-02-28 04:13
##   Status: 200
##   Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
##   Size: 639 kB
## <ON DISK>  C:\Users\REGIST~1\AppData\Local\Temp\RtmpeG77eb\filef97810252802.xlsx
retaildata  <- readxl::read_excel(tf, skip = 1)

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

autoplot(myts)+defaulttheme

ggseasonplot(myts, year.labels = T, year.labels.left = T)+defaulttheme

myts %>% seas(x11="") %>% autoplot()+defaulttheme

The decomposition did not reveal anything extra about the data however it did make more transparent the fluctuations and magnitude of increases/decreases in the trend overtime as well as the decreasing magnituded of the seasonality.