#install.packages('Metrics')
library('Metrics')
###Method 1: Naive Approach ###
#install.packages('Metrics')
library('Metrics')

Part A. Most Recent Value as Forecast

# Time Series Data
week <- 1:6
values <- c(17, 13, 15, 11, 17, 14)
forecast_a <- values[-length(values)] #Excludes the last value
actual_a <- values[-1] #Exclude the first sale
mse_a <- mean((actual_a - forecast_a)^2)
mse_a #Mean square  error is 16.2
## [1] 16.2
mae_a <- mean(abs(actual_a - forecast_a))
mae_a
## [1] 3.8
mape_a <- mean(abs((actual_a - forecast_a) / actual_a)) * 100
mape_a
## [1] 27.43778
#Forecast the sales for Week 7
forecast_week7_a <- tail(values, 1)
forecast_week7_a
## [1] 14
#Interpreation: The number of tonic bottles projected to be sold in week 7 is 14
#Calculate Cumulative Averages
cumulative_averages <- cumsum(values[-length(values)]) / (1:(length(values) - 1))
cumulative_averages
## [1] 17.0 15.0 15.0 14.0 14.6

Part B: Average of All Data as Forecast

forecast_b <- cumulative_averages
actual_b <- values[-1] #Exclude the first value
mse_b <- mean((actual_b - forecast_b)^2)
mse_b #Mean square  error is 8.272
## [1] 8.272
mae_b <- mean(abs(actual_b - forecast_b))
mae_b
## [1] 2.32
mape_b <- mean(abs((actual_b - forecast_b) / actual_b)) * 100
mape_b
## [1] 17.81313
#Forecast the sales for Week 7
forecast_week7_b <- tail(values, 1) #Average of all weeks as forecast for week 7
forecast_week7_b
## [1] 14
#Interpreation: The number of tonic bottles projected to be sold in week 7 is 14

Part C. Comparison

better_method <- ifelse(mse_a < mse_b, "Most Recent Value", "Average of All Data")

#Results
list(
  MSE_Most_Recent_Value = mse_a,
  MAE_Most_Recent_Value = mae_a,
  MAPE_Most_Recent_Value = mape_a,
  Forecast_Week7_Most_Recent = forecast_week7_a,
  MSE_Average = mse_b,
  MAE_Average = mae_b,
  MAPE_Average = mape_b,
  Forecast_Week7_Average = forecast_week7_b,
  Better_Method = better_method
)
## $MSE_Most_Recent_Value
## [1] 16.2
## 
## $MAE_Most_Recent_Value
## [1] 3.8
## 
## $MAPE_Most_Recent_Value
## [1] 27.43778
## 
## $Forecast_Week7_Most_Recent
## [1] 14
## 
## $MSE_Average
## [1] 8.272
## 
## $MAE_Average
## [1] 2.32
## 
## $MAPE_Average
## [1] 17.81313
## 
## $Forecast_Week7_Average
## [1] 14
## 
## $Better_Method
## [1] "Average of All Data"