library(forecast)
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff
library(fpp2)
## Loading required package: ggplot2
## Loading required package: fma
## Loading required package: expsmooth
library(vars)
## Warning: package 'vars' was built under R version 3.6.1
## 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 3.6.1
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.6.1
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
library(readr)
SPY <- read_csv("C:/Users/Joseph/Downloads/SPY (2).csv")
## Parsed with column specification:
## cols(
## Date = col_character(),
## `Adj Close` = col_double()
## )
DJI <- read_csv("C:/Users/Joseph/Downloads/^DJI.csv")
## Parsed with column specification:
## cols(
## Date = col_character(),
## `Adj Close` = col_double()
## )
usMarkets <- merge(
x=SPY,
y=DJI,
by='Date'
)
names(usMarkets)[2:3] <- c('SPY', 'DJI')
usMarkets <- ts(usMarkets[,2:3],start=c(2014,9),end=c(2019,9),frequency = 12)
VARselect(usMarkets[,1:2], lag.max=8)
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 7 7 5 7
##
## $criteria
## 1 2 3 4 5
## AIC(n) 1.917145e+01 1.784265e+01 1.741549e+01 1.729874e+01 1.624378e+01
## HQ(n) 1.925722e+01 1.798561e+01 1.761563e+01 1.755607e+01 1.655829e+01
## SC(n) 1.939450e+01 1.821440e+01 1.793595e+01 1.796790e+01 1.706164e+01
## FPE(n) 2.119140e+08 5.616328e+07 3.671130e+07 3.278189e+07 1.147888e+07
## 6 7 8
## AIC(n) 1.610500e+01 1.598794e+01 1.607409e+01
## HQ(n) 1.647669e+01 1.641682e+01 1.656014e+01
## SC(n) 1.707156e+01 1.710320e+01 1.733805e+01
## FPE(n) 1.007350e+07 9.062976e+06 1.002928e+07
var1 <- VAR(usMarkets[,1:2], p=5, type="const")
serial.test(var1, lags.pt=10, type="PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var1
## Chi-squared = 32.978, df = 20, p-value = 0.03393
var2 <- VAR(usMarkets[,1:2], p=7, type="const")
serial.test(var2, lags.pt=10, type="PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var2
## Chi-squared = 18.837, df = 12, p-value = 0.09253
forecast(var2) %>%
autoplot() + xlab("Year")
