Questions
Exercise 1
Gas_data <- read_excel("~/Gas Data.xlsx")
head(Gas_data)
## # A tibble: 6 x 2
## `40182` `2.718`
## <dttm> <dbl>
## 1 2010-01-11 00:00:00 2.80
## 2 2010-01-18 00:00:00 2.79
## 3 2010-01-25 00:00:00 2.76
## 4 2010-02-01 00:00:00 2.72
## 5 2010-02-08 00:00:00 2.71
## 6 2010-02-15 00:00:00 2.66
gasdata <- Gas_data %>%
rename(date = "40182") %>%
rename(Price = "2.718")
gasdata2 <- gasdata %>%
mutate(Week = yearweek(date)) %>%
select(-date) %>%
as_tsibble(index = Week)
autoplot(gasdata2) + labs(title = "Gasoline Prices")
## Plot variable not specified, automatically selected `.vars = Price`

Exercise 2
seasgas <- gasdata2 %>%
model(stl = STL(Price)) %>%
components() %>%
autoplot()
seasgas

gasdata2 %>%
features(Price, features = guerrero)
## # A tibble: 1 x 1
## lambda_guerrero
## <dbl>
## 1 -0.342
gasdata2 %>%
autoplot(box_cox(Price, -.342)) + labs(y= "Box Cox Transformation")

#box cox did not really do much.
Exercise 3
gasdatazoom <- gasdata2 %>%
filter(year(Week) > 2021)
gasdata2 %>%
model(MEAN(Price)) %>%
forecast(h=2) %>%
autoplot(gasdatazoom) + labs(title= "MEAN Forecast of Gasoline Prices")

NAIVEfc <- gasdata2 %>%
model(NAIVE(Price)) %>%
forecast(h = 2) %>%
autoplot(gasdatazoom) + labs(title = "NAIVE Forecast of Gasoline Prices")
NAIVEfc

gasdata2 %>%
model(SNAIVE(Price ~ lag(2))) %>%
forecast(h = 2) %>%
autoplot(gasdatazoom) + labs(title = "Seasonal NAIVE Forecast of Gasoline Prices")

gasdata2 %>%
model(RW(Price ~ drift())) %>%
forecast(h = 2) %>%
autoplot(gasdatazoom) + geom_line(data = slice(gasdatazoom, range(cumsum(!is.na(Price)))),
aes(y=Price), linetype = "dashed", colour = "#0072B2") + labs(title = "RW Drift Forecast of Gasoline Prices")

Exercise 4
# The drift appears to be the most visually accurate.
Exercise 5
gastrain <- gasdata2 %>%
stretch_tsibble(.init = 5, .step=1)
RWfit <- gastrain %>%
model(RW(Price ~ drift())) %>%
forecast(h=1) %>%
accuracy(gasdata2)
## Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing.
## 1 observation is missing at 2022 W41
RWfit
## # A tibble: 1 x 10
## .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 RW(Price ~ dri~ Test 6.54e-5 0.0559 0.0397 -0.00161 1.32 0.0839 0.0909 0.592
Meanfit <- gastrain %>%
model(MEAN(Price)) %>%
forecast(h=1) %>%
accuracy(gasdata2)
## Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing.
## 1 observation is missing at 2022 W41
Meanfit
## # A tibble: 1 x 10
## .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MEAN(Price) Test -0.108 0.619 0.492 -7.67 17.7 1.04 1.01 0.994
SNAIVEfit <- gastrain %>%
model(SNAIVE(Price ~ lag(2))) %>%
forecast(h=1) %>%
accuracy(gasdata2)
## Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing.
## 1 observation is missing at 2022 W41
SNAIVEfit
## # A tibble: 1 x 10
## .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 SNAIVE(Price ~ lag~ Test 0.00351 0.0995 0.0721 0.0562 2.40 0.152 0.162 0.786
NAIVEfit <- gastrain %>%
model(NAIVE(Price)) %>%
forecast(h=1) %>%
accuracy(gasdata2)
## Warning: The future dataset is incomplete, incomplete out-of-sample data will be treated as missing.
## 1 observation is missing at 2022 W41
NAIVEfit
## # A tibble: 1 x 10
## .model .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NAIVE(Price) Test 0.00182 0.0558 0.0395 0.0395 1.32 0.0834 0.0906 0.591
Exercise 6
gasdata2 %>%
model(NAIVE(Price)) %>%
forecast(h=10) %>%
hilo()
## # A tsibble: 10 x 6 [1W]
## # Key: .model [1]
## .model Week Price .mean `80%`
## <chr> <week> <dist> <dbl> <hilo>
## 1 NAIVE(Price) 2022 W41 N(3.9, 0.0031) 3.91 [3.837683, 3.980317]80
## 2 NAIVE(Price) 2022 W42 N(3.9, 0.0062) 3.91 [3.808143, 4.009857]80
## 3 NAIVE(Price) 2022 W43 N(3.9, 0.0093) 3.91 [3.785476, 4.032524]80
## 4 NAIVE(Price) 2022 W44 N(3.9, 0.012) 3.91 [3.766367, 4.051633]80
## 5 NAIVE(Price) 2022 W45 N(3.9, 0.015) 3.91 [3.749531, 4.068469]80
## 6 NAIVE(Price) 2022 W46 N(3.9, 0.019) 3.91 [3.734311, 4.083689]80
## 7 NAIVE(Price) 2022 W47 N(3.9, 0.022) 3.91 [3.720314, 4.097686]80
## 8 NAIVE(Price) 2022 W48 N(3.9, 0.025) 3.91 [3.707286, 4.110714]80
## 9 NAIVE(Price) 2022 W49 N(3.9, 0.028) 3.91 [3.695050, 4.122950]80
## 10 NAIVE(Price) 2022 W50 N(3.9, 0.031) 3.91 [3.683477, 4.134523]80
## # ... with 1 more variable: `95%` <hilo>
## # i Use `colnames()` to see all variable names