title: “fc 4” author: “Matt Mullis” date: “11/3/22” output: html_document —

Load packages and data

library(fpp3)
library(readxl)
Baconprice <- read_excel("//Users//mattmullis//Downloads//Baconprice.xls")
view(Baconprice)
# install and load any package necessary

Questions

Exercise 1

head(Baconprice)
## # A tibble: 6 × 2
##   Date                Price
##   <dttm>              <dbl>
## 1 2000-01-01 00:00:00  2.75
## 2 2000-02-01 00:00:00  2.87
## 3 2000-03-01 00:00:00  2.93
## 4 2000-04-01 00:00:00  2.95
## 5 2000-05-01 00:00:00  3.01
## 6 2000-06-01 00:00:00  3.13
plot(Baconprice)

bb <- Baconprice %>% 
  mutate(Date = yearmonth(Date))
bpts <- bb %>% as_tsibble(index = "Date")
bpts %>%
  model(
    STL(Price ~ trend(window = 7) +
                   season(window = "periodic"),
    robust = TRUE)) %>%
  components() %>%
  autoplot()

#this is clealy data with an upward increasing trend. maybe a bit of seasonality?
fit <- bpts %>%
    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")),
    Multiplicitive = ETS(Price ~ error("M") + trend("Ad") +
                   season("M"))
  ) %>% 
  forecast(h=4)
## Warning: 9 errors (2 unique) encountered for Multiplicitive
## [3] A seasonal ETS model cannot be used for this data.
## [6] Not enough data to estimate this ETS model.
hilo(fit)
## # A tsibble: 4,224 x 7 [1M]
## # Key:       .id, .model [1,056]
##      .id .model     Date          Price .mean                  `80%`
##    <int> <chr>     <mth>         <dist> <dbl>                 <hilo>
##  1     1 SES    2000 Nov N(3.1, 0.0074)  3.07 [2.958766, 3.179262]80
##  2     1 SES    2000 Dec  N(3.1, 0.015)  3.07 [2.913107, 3.224921]80
##  3     1 SES    2001 Jan  N(3.1, 0.022)  3.07 [2.878071, 3.259957]80
##  4     1 SES    2001 Feb   N(3.1, 0.03)  3.07 [2.848534, 3.289494]80
##  5     1 Holt   2000 Nov N(3.3, 0.0081)  3.27 [3.151517, 3.381505]80
##  6     1 Holt   2000 Dec N(3.3, 0.0081)  3.31 [3.194372, 3.424360]80
##  7     1 Holt   2001 Jan N(3.4, 0.0081)  3.35 [3.237227, 3.467215]80
##  8     1 Holt   2001 Feb N(3.4, 0.0081)  3.40 [3.280081, 3.510069]80
##  9     1 Damped 2000 Nov N(3.1, 0.0071)  3.07 [2.960637, 3.176961]80
## 10     1 Damped 2000 Dec  N(3.1, 0.016)  3.07 [2.908302, 3.228875]80
## # … with 4,214 more rows, and 1 more variable: `95%` <hilo>
view(fit)
accuracy(fit,bpts)
## Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing. 
## 4 observations are missing between 2022 Oct and 2023 Jan
## # A tibble: 4 × 10
##   .model         .type      ME  RMSE   MAE     MPE  MAPE  MASE RMSSE  ACF1
##   <chr>          <chr>   <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Damped         Test  0.0344  0.239 0.166  0.619   3.46 0.482 0.481 0.755
## 2 Holt           Test  0.00617 0.232 0.167 -0.0486  3.53 0.484 0.468 0.759
## 3 Multiplicitive Test  0.00432 0.245 0.181  0.0294  3.79 0.525 0.493 0.772
## 4 SES            Test  0.0417  0.230 0.161  0.747   3.36 0.468 0.463 0.748
#it looks like the Simple Exponential smoothing Method is the best. The number it gives me for h=4 is 7.382999. 

#im going to do a bit more work showing the SES model. 
ses <- bpts %>% 
  model(SES = ETS(Price ~ error("A") + trend("N") + season("N")) )

ses %>%
  forecast(h = 4) %>%
  autoplot(bpts) +
  geom_line(aes(y = .fitted), color = "green", data = augment(ses)) +
  labs(y = "Price") +
  guides(color = "none")

#this looks so pretty. 
tidy(ses)
## # A tibble: 2 × 3
##   .model term  estimate
##   <chr>  <chr>    <dbl>
## 1 SES    alpha     1.00
## 2 SES    l[0]      2.75