Libraries

require(fpp3)
## Loading required package: fpp3
## ── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
## ✔ tibble      3.2.1     ✔ tsibble     1.1.3
## ✔ dplyr       1.1.1     ✔ tsibbledata 0.4.1
## ✔ tidyr       1.3.0     ✔ feasts      0.3.1
## ✔ lubridate   1.9.2     ✔ fable       0.3.3
## ✔ ggplot2     3.4.1     ✔ fabletools  0.3.2
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date()    masks base::date()
## ✖ dplyr::filter()      masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval()  masks lubridate::interval()
## ✖ dplyr::lag()         masks stats::lag()
## ✖ tsibble::setdiff()   masks base::setdiff()
## ✖ tsibble::union()     masks base::union()
mydata=read.csv('C:/Users/lfult/Documents/_Courses/Boston College/Predictive analytics/energyconsumption.csv')

Time Series

myts=mydata %>%
  mutate(Month = yearmonth(Year)) %>%as_tsibble(index = Month)

myts$Year=NULL
autoplot(myts)
## Plot variable not specified, automatically selected `.vars =
## Energy_Consumption`

train=myts[1:481,]
test=myts[482:599,]

SES

fit=train%>%model(A=ETS(Energy_Consumption ~ error("A") + trend("N") + season("N")))

fc=fit %>%forecast(test)
report(fit)
## Series: Energy_Consumption 
## Model: ETS(A,N,N) 
##   Smoothing parameters:
##     alpha = 0.9154666 
## 
##   Initial states:
##      l[0]
##  7.173496
## 
##   sigma^2:  0.2466
## 
##      AIC     AICc      BIC 
## 2301.118 2301.168 2313.645
autoplot(fc)+
  geom_line(data=myts, aes(y=Energy_Consumption))

(tmp=accuracy(fc, test))
## # A tibble: 1 × 10
##   .model .type     ME  RMSE   MAE   MPE  MAPE  MASE RMSSE  ACF1
##   <chr>  <chr>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 A      Test  -0.758 0.942 0.826 -9.80  10.5   NaN   NaN 0.460

Error Trend

fit2=train%>%model(AA=ETS(Energy_Consumption ~ error("A") + trend("A") + season("N")))

fc2=fit2 %>%forecast(test)
report(fit2)
## Series: Energy_Consumption 
## Model: ETS(A,A,N) 
##   Smoothing parameters:
##     alpha = 0.9154921 
##     beta  = 0.0001001023 
## 
##   Initial states:
##      l[0]        b[0]
##  7.168793 0.003497177
## 
##   sigma^2:  0.2476
## 
##      AIC     AICc      BIC 
## 2305.139 2305.265 2326.019
autoplot(fc2)+
  geom_line(data=myts, aes(y=Energy_Consumption))

(tmp=rbind(tmp,accuracy(fc2, test)))
## # A tibble: 2 × 10
##   .model .type     ME  RMSE   MAE    MPE  MAPE  MASE RMSSE  ACF1
##   <chr>  <chr>  <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 A      Test  -0.758 0.942 0.826  -9.80  10.5   NaN   NaN 0.460
## 2 AA     Test  -0.967 1.12  1.00  -12.4   12.8   NaN   NaN 0.477

Error Trend Seasonality

fit3=train%>%model(AAA=ETS(Energy_Consumption ~ error("A") + trend("A") + season("A")))

fc3=fit3 %>%forecast(test)
report(fit3)
## Series: Energy_Consumption 
## Model: ETS(A,A,A) 
##   Smoothing parameters:
##     alpha = 0.2971645 
##     beta  = 0.000100026 
##     gamma = 0.2446957 
## 
##   Initial states:
##      l[0]        b[0]      s[0]     s[-1]      s[-2]      s[-3]      s[-4]
##  6.322777 0.003423837 0.9304058 0.1221934 -0.2244565 -0.5812579 -0.1398321
##       s[-5]      s[-6]      s[-7]      s[-8]     s[-9]    s[-10]   s[-11]
##  -0.3197637 -0.5367833 -0.3655611 -0.3851181 0.2049936 0.2662072 1.028973
## 
##   sigma^2:  0.0332
## 
##      AIC     AICc      BIC 
## 1350.639 1351.961 1421.629
autoplot(fc3)+
  geom_line(data=myts, aes(y=Energy_Consumption))

(tmp=rbind(tmp,accuracy(fc3, test)))
## # A tibble: 3 × 10
##   .model .type      ME  RMSE   MAE     MPE  MAPE  MASE RMSSE  ACF1
##   <chr>  <chr>   <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 A      Test  -0.758  0.942 0.826  -9.80  10.5    NaN   NaN 0.460
## 2 AA     Test  -0.967  1.12  1.00  -12.4   12.8    NaN   NaN 0.477
## 3 AAA    Test   0.0498 0.291 0.211   0.479  2.61   NaN   NaN 0.657

Error Trend Seasonality Automatic

fit4=train%>%model(Auto=ETS(Energy_Consumption))

fc4=fit4 %>%forecast(test)
report(fit4)
## Series: Energy_Consumption 
## Model: ETS(M,N,A) 
##   Smoothing parameters:
##     alpha = 0.3208371 
##     gamma = 0.2375125 
## 
##   Initial states:
##      l[0]      s[0]     s[-1]      s[-2]      s[-3]      s[-4]      s[-5]
##  6.267756 0.9294317 0.1158087 -0.1707518 -0.5531402 -0.2685045 -0.3336521
##       s[-6]      s[-7]      s[-8]     s[-9]    s[-10]  s[-11]
##  -0.5378114 -0.3829083 -0.3861742 0.2352485 0.2611836 1.09127
## 
##   sigma^2:  6e-04
## 
##      AIC     AICc      BIC 
## 1330.100 1331.132 1392.738
autoplot(fc4)+
  geom_line(data=myts, aes(y=Energy_Consumption))

(tmp=rbind(tmp,accuracy(fc4, test)))
## # 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 A      Test  -0.758  0.942 0.826  -9.80  10.5    NaN   NaN 0.460
## 2 AA     Test  -0.967  1.12  1.00  -12.4   12.8    NaN   NaN 0.477
## 3 AAA    Test   0.0498 0.291 0.211   0.479  2.61   NaN   NaN 0.657
## 4 Auto   Test   0.259  0.375 0.313   3.05   3.81   NaN   NaN 0.609