Load Datasets

library(ggplot2)
library(readxl)
library(dplyr)
library(forecast)
library(Metrics)

df <- read_excel("ESM_input_file.xlsx")
head(df)
## # A tibble: 6 × 6
##   country wkx   rcvweek             Nig   actual_qty pred_qty
##   <chr>   <chr> <dttm>              <chr>      <dbl>    <dbl>
## 1 US      WK1   2022-07-17 00:00:00 all     46092410 54387453
## 2 US      WK1   2022-07-24 00:00:00 all     44542767 49551401
## 3 US      WK1   2022-07-31 00:00:00 all     48076127 50466409
## 4 US      WK1   2022-08-07 00:00:00 all     42987951 46944085
## 5 US      WK1   2022-08-14 00:00:00 all     35498449 39843109
## 6 US      WK1   2022-08-21 00:00:00 all     37246398 37727077
# Subsets US week1
USwk1<-df%>%
  filter(country=="US",wkx=="WK1")

smoting<-ets(USwk1$actual_qty, model = "AAN", 
             damped = TRUE, alpha = 0.85, beta = 0.025, gamma = 0.1)

# Insert Fitted into USwk1
USwk1$fitted<-fitted(smoting)

Visualization

ggplot(USwk1, aes(x = rcvweek)) +
  geom_line(aes(y = actual_qty/10^6, color = "Actual Quantity")) +
  geom_line(aes(y = pred_qty/10^6, color = "SQRF Predicted Quantity")) +
  geom_line(aes(y = fitted/10^6, color = "HW Predicted Quantity")) +
  theme_bw() +
  ylab("Quantity [in million]") +
  xlab("Receive week") +
  ggtitle("Actual vs Predicted Quantities - US WK1") +
  theme(plot.title = element_text(hjust = 0.5))

# Calculate MAE and MSE for actual_qty vs pred_qty
mae_actual_pred <- mae(USwk1$actual_qty/10^6, USwk1$pred_qty/10^6)
mse_actual_pred <- mse(USwk1$actual_qty/10^6, USwk1$pred_qty/10^6)


# Calculate MAE and MSE for actual_qty vs fitted
mae_actual_fitted <- mae(USwk1$actual_qty/10^6, USwk1$fitted/10^6)
mse_actual_fitted <- mse(USwk1$actual_qty/10^6, USwk1$fitted/10^6)

paste0("MSE of Actual Quantity vs HW Predicted: ", mse_actual_fitted)
## [1] "MSE of Actual Quantity vs HW Predicted: 36.5775826749728"
paste0("MSE of Actual Quantity vs SQRF Predicted: ", mse_actual_pred)
## [1] "MSE of Actual Quantity vs SQRF Predicted: 25.1244278821846"
paste0("MAE of Actual Quantity vs HW Predicted: ", mae_actual_fitted)
## [1] "MAE of Actual Quantity vs HW Predicted: 4.81464112356374"
paste0("MAE of Actual Quantity vs SQRF Predicted: ", mae_actual_pred)
## [1] "MAE of Actual Quantity vs SQRF Predicted: 3.83445615068493"

In this sample I used two accuracy method, MAE and MSE, the best predicted is determine by the smallest value of MAE and MSE. In case above SQRF has Smallest value, which mean SQRF are better prediction in US-WK1 case compare to HW Prediction.

Predicted Error Plot

ggplot(USwk1, aes(x = rcvweek)) +
  geom_line(aes(y = actual_qty/10^6 - pred_qty/10^6, color = "Actual-  SQRF Predicted")) +
  geom_line(aes(y = actual_qty/10^6- fitted/10^6, color = "Actual - HW Predicted")) +
  theme_bw() + geom_hline(yintercept = 0, linetype = 2)+
  ylab("Quantity [in million]") +
  xlab("Receive week")+
  ggtitle("Actual - Predicted Error - US WK1") +
  theme(plot.title = element_text(hjust = 0.5))

Plot above show the Actual- SQRF Predicted and Actual - HW Predicted.