library(readxl)
library(tidyquant)
library(fpp3)
library(moments)
library(tsibble)
library(tsibbledata)
library(ggplot2)
library(dplyr)
library(seasonal)
library(seasonalview)
library(fabletools)
library(fable)
library(ggfortify)
library(quantmod)
library(GGally)
bacon_data <- read_excel("/Users/Peter Cook/Documents/Economics and Finance/Business Forecasting/Forecasting Data/Bacon.xlsx")

bacon_tsibble <- bacon_data %>%
  mutate(Month = yearmonth(Month)) %>%
  as_tsibble(index = Month)

autoplot(bacon_tsibble) +
  labs(title = "Monthly Price of Bacon per Pound") + 
  labs(subtitle = "From Jan 2000 to Sep 2022")
## Plot variable not specified, automatically selected `.vars = Price`

#I cleaned the data in excel, imported it into R, created a Tsibble, and created a graph of the data using the autoplot function. There is a definite upward trend, but I don’t see any cycle or seasonality.

bacon_expo_smooth <- bacon_tsibble %>%
  model(ETS(Price ~ error("A") + trend("N") + season("N")))
  
bacon_expo_smooth %>%
  forecast(h = 4)%>%
  autoplot(bacon_tsibble) +
  geom_line(aes(y = .fitted), col="#D55E00",
            data = augment(bacon_expo_smooth)) +
  labs(title = "Four Month Forecast of Bacon Prices per Pound") +
  labs(subtitle = "Using Simple Exponential Smooth")

#I produced a four period of forecast using a simple exponential smoothing method. This forecast is not ideal because the simple exponential smoothing method is only suitable when the data has no trend or seasonality. Our data has an obvious upwards trend.

bacon_tsibble %>%
  model(
    `Holt's method` = ETS(Price ~ error("A") +  trend("A") + season("N"))) %>%
  forecast(h = 4) %>%
  autoplot(bacon_tsibble) +
  labs(title = "Four Month Forecast of Bacon Prices per Pound") +
  labs(subtitle = "Using Holt's Linear Trend")

#I created a four period forecast of bacon prices using Holt’s Linear trend, which is just an expanded version of simple exponential smoothing that allows for forecasting of data with a trend. I think that this will likely be the most accurate forecast because it is capturing the large, upward trend of the data and our forecast is so near into the future that I don’t believe overforecasting will be an issue as it is with longer periods.

bacon_tsibble %>%
  model(
    `Damped Holt's method` = ETS(Price ~ error("A") +
                       trend("Ad", phi = 0.9) + season("N"))) %>%
  forecast(h = 4) %>%
  autoplot(bacon_tsibble) +
  labs(title = "Four Month Forecast of Bacon Prices per Pound") +
  labs(subtitle = "Using Holt's Damped Linear Trend")

#Created a four period forecast using Holt’s linear trend, this time with a damping parameter added on. This damping parameter is added for forecasts of further periods into the future to prevent overforecasting with the standard linear trend. I don’t think this parameter is necessary for our forecast becasue we are only forecasting four months into the future. At a glance the linear trend and damped linear trend appear to produce identical forecasts, and I anticipate them to have identical RMSE and MAPE scores.

bacon_tsibble %>%
  model(
    additive = ETS(Price ~ error("A") + trend("A") + season("A"))) %>%
  forecast(h = 4) %>%
  autoplot(bacon_tsibble) +
  labs(title = "Four Month Forecast of Bacon Prices per Pound") +
  labs(subtitle = "Using Holt-Winters Additive Method")  

#I created a four period forecast of bacon prices using the Holt-Winters Additive Method. The Holt-Winters method adds a seasonal component onto Holt’s Linear Trend, with the additive method being preferrable for data with constant seasonality. I don’t think that there is any obvious seasonality, so I don’t expect this forecast to have incredible RMSE or MAPE scores.

bacon_tsibble %>%
  model(
    multiplicative = ETS(Price ~ error("M") + trend("A") + season("M"))) %>%
  forecast(h = 4) %>%
  autoplot(bacon_tsibble) +
  labs(title = "Four Month Forecast of Bacon Prices per Pound") +
  labs(subtitle = "Using Holt-Winters Multiplicative Method")  

#I created a four period forecast using the Holt-Winters Multiplicative method. Multiplicative is best when your data has non-constant seasonality. If there is any seasonality in this data, I would choose the multiplicative method over the additive, because any seasonality that is there is definitely not constant. We’ll have to see what the time series cross validation has to say about each of these models.

bacon_tsibble %>%
  stretch_tsibble(.init = 10) %>%
  model(
    SES = ETS(Price ~ error("A") + trend("N") + season("N")),
    Holt = ETS(Price ~ error("A") + trend("A") + season("N")),
    Damped = ETS(Price ~ error("A") + trend("Ad") + season("N")),
    multiplicative = ETS(Price ~ error("M") + trend("A") + season("M")),
    additive = ETS(Price ~ error("A") + trend("A") + season("A"))
  ) %>%
  forecast(h = 1) %>%
  accuracy(bacon_tsibble)
## Warning: 8 errors (2 unique) encountered for multiplicative
## [3] A seasonal ETS model cannot be used for this data.
## [5] Not enough data to estimate this ETS model.
## Warning: 8 errors (2 unique) encountered for additive
## [3] A seasonal ETS model cannot be used for this data.
## [5] Not enough data to estimate this ETS model.
## Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing. 
## 1 observation is missing at 2022 Oct
## # A tibble: 5 × 10
##   .model         .type       ME  RMSE    MAE     MPE  MAPE  MASE RMSSE  ACF1
##   <chr>          <chr>    <dbl> <dbl>  <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 additive       Test   0.00148 0.135 0.0997 -0.0440  2.16 0.289 0.272 0.320
## 2 Damped         Test   0.0132  0.129 0.0944  0.248   2.04 0.274 0.260 0.242
## 3 Holt           Test   0.00176 0.128 0.0949 -0.0315  2.06 0.275 0.259 0.255
## 4 multiplicative Test  -0.00120 0.142 0.107  -0.118   2.33 0.311 0.286 0.376
## 5 SES            Test   0.0166  0.128 0.0941  0.304   2.02 0.273 0.258 0.248

#I did a time-series cross validation of each of the models I have used so far, measuring the one-step forecast accuracy of each. According to our cross validation, the simple exponential smoothing model is the best, having the lowest RMSE, MAE, and MAPE scores. AS predicted, the additive and multiplicative Holt-Winters methods were the least accurate, with Holt’s Linear Trend and Holt’s Damped Linear Trend being close to each other and to SES in accuracy.

bacon_fc <- bacon_tsibble %>%
  model(ETS(Price ~ error("A") + trend("N") + season("N"))) %>%
  forecast(h=4)

bacon_fc
## # A fable: 4 x 4 [1M]
## # Key:     .model [1]
##   .model                                               Month         Price .mean
##   <chr>                                                <mth>        <dist> <dbl>
## 1 "ETS(Price ~ error(\"A\") + trend(\"N\") + seaso… 2022 Oct N(7.4, 0.016)  7.38
## 2 "ETS(Price ~ error(\"A\") + trend(\"N\") + seaso… 2022 Nov N(7.4, 0.032)  7.38
## 3 "ETS(Price ~ error(\"A\") + trend(\"N\") + seaso… 2022 Dec N(7.4, 0.048)  7.38
## 4 "ETS(Price ~ error(\"A\") + trend(\"N\") + seaso… 2023 Jan N(7.4, 0.064)  7.38

#Using the SES model, my point forecast for the price per pound of Bacon is $7.39, with an 80% prediction interval ranging from $7.10 to $7.60.