#Libraries
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.0.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.0.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.0.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.0.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(fpp2)
## Warning: package 'fpp2' was built under R version 4.0.3
## -- Attaching packages --------------------------------------------------------------------------------- fpp2 2.4 --
## v ggplot2 3.3.2 v fma 2.4
## v forecast 8.13 v expsmooth 2.3
## Warning: package 'forecast' was built under R version 4.0.3
## Warning: package 'fma' was built under R version 4.0.3
## Warning: package 'expsmooth' was built under R version 4.0.3
##
library(vars)
## Warning: package 'vars' was built under R version 4.0.3
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following objects are masked from 'package:fma':
##
## cement, housing, petrol
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.0.3
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.0.3
## Loading required package: urca
## Warning: package 'urca' was built under R version 4.0.3
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.0.3
#Download data #Using JP Morgan and Bank Of America, as they should be relatively correlated. They are in the same industry and subject to roughly similar regulatory changes
tickers=c("BAC","JPM")
getSymbols(tickers,return.class="ts",from="2015-01-01",to="2020-11-01",auto.assign = TRUE, periodicity="monthly")
## '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] "BAC" "JPM"
#Plot time series
#Bank variables
BAC_ts=BAC[,6]
JPM_ts=JPM[,6]
autoplot(BAC_ts)+
autolayer(JPM_ts)
#Data isnt stationary so we should take differences
#differences
BAC_ts.diff = diff(BAC_ts)
JPM_ts.diff = diff(JPM_ts)
#these look reasonably correlated
autoplot(BAC_ts.diff)
autoplot(JPM_ts.diff)
plot(BAC_ts, JPM_ts)
#Check Autocorrelations #Autocorrelations look good to me, we are set to run VAR model
ggAcf(BAC_ts.diff)
ggPacf(BAC_ts.diff)
ggAcf(JPM_ts.diff)
ggPacf(JPM_ts.diff)
#Selecting a model
combined.data=cbind(BAC=BAC_ts.diff,JPM=JPM_ts.diff)
VARselect(combined.data[,1:2], lag.max=8,
type="const")[["selection"]]
## AIC(n) HQ(n) SC(n) FPE(n)
## 1 1 1 1
var1 <- VAR(combined.data, p=1, type="const")
serial.test(var1, lags.pt=8, type="PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var1
## Chi-squared = 21.862, df = 28, p-value = 0.7876
fcst.1 = forecast(var1)
autoplot(fcst.1)