Load Data
setwd("~/Documents/MSAE/Predictive Analytics")
retaildata <- read.csv("retaildata.csv")
Creating Tsibble
retaildata <- retaildata %>%
mutate(Month = yearmonth(Month)) %>%
tsibble(index = Month)
retaildata$Retail = as.numeric(retaildata$Retail)
Training Set (1993-2018) & Test Set (2019)
training <- retaildata %>%
filter(year(Month) < 2019 )
test <- retaildata %>%
filter(year(Month) == 2019)
Time-Series Plot
training %>% autoplot() +
labs(title = "Monthly Sales for Total Retail Sales, 1993-2018") +
xlab("Month") +
ylab("Sales ($ in Millions)")
## Plot variable not specified, automatically selected `.vars = Retail`

Seasonal Plot
training %>% gg_season(Retail) +
labs(title = "Seasonality of Retail Sales") +
xlab("Month") +
ylab("Sales ($ in Millions)")

Neural Net Model
neuralnet_model <- training %>% model(NNETAR(log(Retail)))
report(neuralnet_model)
## Series: Retail
## Model: NNAR(1,1,2)[12]
## Transformation: log(Retail)
##
## Average of 20 networks, each of which is
## a 2-2-1 network with 9 weights
## options were - linear output units
##
## sigma^2 estimated as 0.001233
neuralnet_fcst <- neuralnet_model %>% forecast(test)
neuralnet_fcst %>% autoplot(training) +
labs(title = "Neural Net model") +
xlab("Month") +
ylab("Sales ($ in Millions)")

neuralnet_fcst %>% autoplot(test) +
labs(title = "Neural Net model") +
xlab("Month") +
ylab("Sales ($ in Millions)")

nn_accuracy <- accuracy(neuralnet_fcst, test)
ARIMA
ARIMA_model <- training %>%
model(ARIMA(log(Retail)))
ARIMA_fcst <- ARIMA_model %>% forecast(test)
ARIMA_plot <- ARIMA_model %>% forecast(test) %>% autoplot(test) +
labs(title = "ARIMA model") +
xlab("Month") +
ylab("Sales ($ in Millions)")
ARIMA_accuracy <- accuracy(ARIMA_fcst, test)
report(ARIMA_model)
## Series: Retail
## Model: ARIMA(2,1,1)(0,1,1)[12]
## Transformation: log(Retail)
##
## Coefficients:
## ar1 ar2 ma1 sma1
## -0.9444 -0.5358 0.3599 -0.7502
## s.e. 0.0887 0.0554 0.0955 0.0450
##
## sigma^2 estimated as 0.0004077: log likelihood=741.59
## AIC=-1473.18 AICc=-1472.98 BIC=-1454.68
ARIMA_model %>% gg_tsresiduals(lag_max = 12) + labs(title = "ARIMA model")

ARIMA_plot

ETS model
ETS_model <- training %>%
model(ETS(log(Retail)))
ETS_fcst <- ETS_model %>% forecast(test)
ETS_plot <- ETS_model %>% forecast(test) %>% autoplot(test) +
labs(title = "ETS model")+
xlab("Month") +
ylab("Sales ($ in Millions)")
ETS_accuracy <- accuracy(ETS_fcst, test)
report(ETS_model)
## Series: Retail
## Model: ETS(A,Ad,A)
## Transformation: log(Retail)
## Smoothing parameters:
## alpha = 0.4632852
## beta = 0.02082179
## gamma = 0.1708057
## phi = 0.9799992
##
## Initial states:
## l[0] b[0] s[0] s[-1] s[-2] s[-3] s[-4]
## 11.92483 0.008770624 0.1910545 0.01162845 -0.008418121 -0.03626672 0.02312176
## s[-5] s[-6] s[-7] s[-8] s[-9] s[-10]
## 0.006584932 0.01812502 0.03780529 -0.007010001 0.003748631 -0.1284263
## s[-11]
## -0.1119474
##
## sigma^2: 4e-04
##
## AIC AICc BIC
## -595.5131 -593.1786 -528.1390
ETS_model %>% gg_tsresiduals(lag_max = 12) + labs(title="ETS Model")

ETS_plot

Accuracy Metrics and Discussion
accuracy_metrics = rbind("ETS" = ETS_accuracy,
"ARIMA" = ARIMA_accuracy,
"Neural Network" = nn_accuracy
)
accuracy_metrics <- accuracy_metrics %>% arrange(RMSE)
accuracy_metrics
## # A tibble: 3 × 10
## .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## * <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ARIMA(log(Retail)) Test 2063. 6731. 5700. 0.396 1.28 NaN NaN -0.0736
## 2 NNETAR(log(Retail)) Test -3803. 7986. 6221. -0.934 1.43 NaN NaN -0.155
## 3 ETS(log(Retail)) Test 5701. 9624. 8137. 1.19 1.79 NaN NaN 0.226