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(tseries)
library(timeSeries)
## Loading required package: timeDate
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
## 
##     time<-
library(forecast)
library(xts)

getSymbols('CHTR', from='1995-01-01', to='2021-11-11')
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## [1] "CHTR"
class(CHTR)
## [1] "xts" "zoo"
CHTR_Close_Prices = CHTR[,4]

plot(CHTR_Close_Prices)

par(mfrow=c(1,1))

par(mfrow=c(1,2))
Acf(CHTR_Close_Prices, main='ACF for Different Series')
Pacf(CHTR_Close_Prices, main='PACF for Different Series')

print(adf.test(CHTR_Close_Prices))
## 
##  Augmented Dickey-Fuller Test
## 
## data:  CHTR_Close_Prices
## Dickey-Fuller = -1.8976, Lag order = 14, p-value = 0.6217
## alternative hypothesis: stationary
auto.arima(CHTR_Close_Prices, seasonal = FALSE)
## Series: CHTR_Close_Prices 
## ARIMA(0,1,2) with drift 
## 
## Coefficients:
##           ma1     ma2   drift
##       -0.0870  0.0405  0.2233
## s.e.   0.0183  0.0180  0.0931
## 
## sigma^2 estimated as 28.45:  log likelihood=-9228.23
## AIC=18464.46   AICc=18464.48   BIC=18488.47
fitA = auto.arima(CHTR_Close_Prices, seasonal = FALSE)
tsdisplay(residuals(fitA), lag.max=40, main='(1,1,3) Model Residuals')

auto.arima(CHTR_Close_Prices, seasonal=FALSE)
## Series: CHTR_Close_Prices 
## ARIMA(0,1,2) with drift 
## 
## Coefficients:
##           ma1     ma2   drift
##       -0.0870  0.0405  0.2233
## s.e.   0.0183  0.0180  0.0931
## 
## sigma^2 estimated as 28.45:  log likelihood=-9228.23
## AIC=18464.46   AICc=18464.48   BIC=18488.47
fitB = arima(CHTR_Close_Prices, order=c(1,1,18))
tsdisplay(residuals(fitB), lag.max=40, main='(1,1,18) Model Residuals')

fitC = arima(CHTR_Close_Prices, order=c(1,1,38))
tsdisplay(residuals(fitC), lag.max=40, main='(1,1,38) Model Residuals')

fitD = arima(CHTR_Close_Prices, order=c(1,1,1))
tsdisplay(residuals(fitD), lag.max=40, main='(1,1,1) Model Residuals')

par(mfrow=c(2,2))

term<-100
fcast1 <- forecast(fitA, h=term)
plot(fcast1)

# Custom Arima
fcast2 <- forecast(fitB, h=term)
plot(fcast2)
fcast3 <- forecast(fitC, h=term)
plot(fcast3)
fcast4 <- forecast(fitD, h=term)
plot(fcast4)

#Mape accuracy subtract from 100.
accuracy(fcast1)
##                         ME     RMSE      MAE        MPE     MAPE     MASE
## Training set -3.566426e-05 5.330595 2.999576 -0.1104135 1.213128 1.002772
##                       ACF1
## Training set -0.0004222097
accuracy(fcast2)
##                     ME     RMSE      MAE        MPE     MAPE      MASE
## Training set 0.2222817 5.282925 2.987921 0.08559274 1.194499 0.9988757
##                      ACF1
## Training set -0.002128292
accuracy(fcast3)
##                     ME     RMSE      MAE        MPE     MAPE     MASE
## Training set 0.2458619 5.240203 3.003843 0.09499509 1.207272 1.004199
##                     ACF1
## Training set -0.00358169
accuracy(fcast4)
##                     ME    RMSE      MAE        MPE     MAPE     MASE
## Training set 0.2384474 5.33617 2.992056 0.09137677 1.187475 1.000258
##                      ACF1
## Training set 0.0006713685
#Plot of Arima Models term = 45 days
term<-45
fcast1 <- forecast(fitA, h=term)
plot(fcast1)

#custom arima
fcast2 <- forecast(fitB, h=term)
plot(fcast2)
fcast3 <- forecast(fitC, h=term)
plot(fcast3)
fcast4 <- forecast(fitD, h=term)
plot(fcast4)

#Plot of Arima Models term = 365 days
term<-365
fcast1 <- forecast(fitA, h=term)
plot(fcast1)

#custom arima
fcast2 <- forecast(fitB, h=term)
plot(fcast2)
fcast3 <- forecast(fitC, h=term)
plot(fcast3)
fcast4 <- forecast(fitD, h=term)
plot(fcast4)