## ── Attaching packages ────────────────────────────────────────────────────────── fpp3 0.3 ──
## ✓ tibble 3.0.3 ✓ tsibble 0.9.2
## ✓ dplyr 1.0.2 ✓ tsibbledata 0.2.0
## ✓ tidyr 1.1.2 ✓ feasts 0.1.5
## ✓ lubridate 1.7.9 ✓ fable 0.2.1
## ✓ ggplot2 3.3.2
## ── Conflicts ───────────────────────────────────────────────────────────── fpp3_conflicts ──
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x tsibble::interval() masks lubridate::interval()
## x dplyr::lag() masks stats::lag()
## 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
##
## Attaching package: 'seasonal'
## The following object is masked from 'package:tibble':
##
## view
plastics
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1 742 697 776 898 1030 1107 1165 1216 1208 1131 971 783
## 2 741 700 774 932 1099 1223 1290 1349 1341 1296 1066 901
## 3 896 793 885 1055 1204 1326 1303 1436 1473 1453 1170 1023
## 4 951 861 938 1109 1274 1422 1486 1555 1604 1600 1403 1209
## 5 1030 1032 1126 1285 1468 1637 1611 1608 1528 1420 1119 1013
autoplot(plastics) + xlab("Month") + ylab("Sales (in thousands)") +
ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")
There is seasonality in the sales for plastic A where we see increased sales from spring through summer and then a decrease in sales in the fall and winter months. Overall, there is a general increased positive trend year over year.
ggseasonplot(plastics)
plastics %>% decompose(type="multiplicative") %>%
autoplot() + xlab("Month") +
ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")
Yes, the results support the interpretation from part A where it shows strong seasonality and an upward trend in sales numbers.
plastics %>% decompose(type="multiplicative") -> fit
autoplot(plastics, series = "Data") +
autolayer(seasadj(fit), series = "Seasonally Adjusted")
plastics_outlier <- plastics
plastics_outlier[30] <- plastics_outlier[30] + 500
plastics_outlier %>% decompose(type="multiplicative") -> ofit
autoplot(plastics_outlier, series = "Data") +
autolayer(trendcycle(ofit), series = "Trend") +
autolayer(seasadj(ofit), series = "Seasonally Adjusted")
## Warning: Removed 12 row(s) containing missing values (geom_path).
plastics_outlier %>% decompose(type="multiplicative") %>%
autoplot() + xlab("Month") +
ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")
plastics_outlier_end <- plastics
plastics_outlier_end[50] <- plastics_outlier_end[50] + 500
plastics_outlier_end %>% decompose(type="multiplicative") -> ofit
autoplot(plastics_outlier_end, series = "Data") +
autolayer(trendcycle(ofit), series = "Trend") +
autolayer(seasadj(ofit), series = "Seasonally Adjusted")
## Warning: Removed 12 row(s) containing missing values (geom_path).
plastics_outlier_end %>% decompose(type="multiplicative") %>%
autoplot() + xlab("Month") +
ggtitle("Monthly sales (in thousands) of product A for a plastics manufacturer for five years")
There seems to be a less drastic spike when the outlier is towards the end, this may be in part to the fact that the trend is increasing over time. Overall, this value would still be considered an outlier, but it seems to affect the trend less when at the end.
myts <- ts(retaildata[,"A3349413L"],
frequency=12, start=c(1982,4))
autoplot(myts)+
xlab("Year") + ylab("Sales") +
ggtitle("Retail Data")
myts %>% seas(x11="") -> fit
autoplot(fit) +
ggtitle("X11 decomposition of retail sales")
The remainder shows outliers around 2000-2001 which may not have been previously noticed.