library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(timeSeries)
## Loading required package: timeDate
##
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
##
## time<-
library(tseries)
library(xts)
library(forecast)
#Pull the data from Yahoo finance SPY- S&P500
library(readr)
SPY <- read_csv("SP500.csv")
##
## -- Column specification --------------------------------------------------------
## cols(
## Date = col_character(),
## Open = col_character(),
## High = col_character(),
## Low = col_character(),
## Close = col_character(),
## `Adj Close` = col_character(),
## Volume = col_character()
## )
class(SPY)
## [1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
SPY_Close_Prices= SPY[,6]
SPY_Close_Price.ts= ts(SPY_Close_Prices)
plot(SPY_Close_Price.ts)

class(SPY_Close_Price.ts)
## [1] "ts"
# graph of Acf and Pacf looking for identifiable lags
par(mfrow=c(1,2))
Acf(SPY_Close_Price.ts, main=" Acf for Differenced Series")
Pacf(SPY_Close_Price.ts, main= "Pacf for Diiferenced Series")

#adf test for p-value
print(adf.test(SPY_Close_Price.ts))
## Warning in adf.test(SPY_Close_Price.ts): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: SPY_Close_Price.ts
## Dickey-Fuller = -6.0774, Lag order = 13, p-value = 0.01
## alternative hypothesis: stationary
auto.arima(SPY_Close_Price.ts, seasonal =FALSE)
## Series: SPY_Close_Price.ts
## ARIMA(0,1,1) with drift
##
## Coefficients:
## ma1 drift
## -0.8528 0.7590
## s.e. 0.0109 0.3868
##
## sigma^2 estimated as 17314: log likelihood=-15853.15
## AIC=31712.31 AICc=31712.32 BIC=31729.8
fitA=auto.arima(SPY_Close_Price.ts, seasonal =FALSE)
tsdisplay(residuals(fitA), lag.max = 50, main = "(0,1,1) Model Residuals")

auto.arima(SPY_Close_Price.ts, seasonal =FALSE) # AIC=31712.31 AICc=31712.32 BIC=31729.8
## Series: SPY_Close_Price.ts
## ARIMA(0,1,1) with drift
##
## Coefficients:
## ma1 drift
## -0.8528 0.7590
## s.e. 0.0109 0.3868
##
## sigma^2 estimated as 17314: log likelihood=-15853.15
## AIC=31712.31 AICc=31712.32 BIC=31729.8
#plots of arima model
par(mfrow=c(2,2))
#auto arima 2,0,2
term<- 100
fcast<- forecast(fitA, h=term)
plot(fcast)
accuracy(fcast)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.04850582 131.5039 43.60241 -9.515368 13.87 1.227845 0.009300375
