Discussion #4

Libraries

library(rmarkdown)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tseries)
library(timeSeries)
## Loading required package: timeDate
library(xts)
## Loading required package: zoo

## 
## Attaching package: 'zoo'

## The following object is masked from 'package:timeSeries':
## 
##     time<-

## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(quantmod)
## Loading required package: TTR
library(readr)
library(ggplot2)

Data Import and Time Series Creation

setwd("~/Desktop/Grad School/Predictive")
shooting <- read.csv("shooting.csv")
shooting <- shooting[ -c(3) ]
shooting <- na.omit(shooting)
shooting.month <- ts(data = shooting$Shootings, start = 2015, end = 2021, frequency = 12)
autoplot(shooting.month)

shoot <- window(shooting.month, start=2015)
train <- window(shoot, end=c(2020))

Functions

f.ets <- function(x, h) {
  forecast(ets(x), h = h)
}
f.arima <- function(x, h) {
  forecast(auto.arima(x), h=h)
}

Comparing Models

ets <- tsCV(shoot, f.ets, h=1)
auto <- tsCV(shoot, f.arima, h=1)

mean(ets^2, na.rm=TRUE)
## [1] 74.63301
mean(auto^2, na.rm=TRUE)
## [1] 85.02418

Testing ETS

train.ets <- ets(train)

test.ets <- train.ets %>% 
  forecast(h = 6) %>%
  accuracy(shoot)

test.ets[,c("RMSE","MAE","MAPE","MASE")]
##                   RMSE      MAE     MAPE      MASE
## Training set  5.645678 4.509681 31.85152 0.7105286
## Test set     11.285645 9.994815 40.91740 1.5747458

Testing auto.arima

train.arima <- auto.arima(train)

test.arima <- train.arima %>% 
  forecast(h = 6) %>%
  accuracy(shoot)

test.arima[,c("RMSE","MAE","MAPE","MASE")]
##                   RMSE       MAE     MAPE     MASE
## Training set  5.653538  4.254442 26.54793 0.670314
## Test set     11.436363 10.014514 39.85224 1.577849

Forecast

shoot %>% 
  ets() %>% 
  forecast(h=12) %>% 
  autoplot()