library(forecast)
library(vars)
## Loading required package: MASS
## Loading required package: strucchange
## Loading required package: zoo
## 
## 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(MASS)
library(strucchange)
library(zoo)
library(sandwich)
library(urca)
library(lmtest)

HD <- read.csv("~/Downloads/HD.csv")
WMT <- read.csv("~/Downloads/WMT.csv")

hd.ts=ts(HD[,6], frequency=12, start=c(2013,8))
wmt.ts=ts(WMT[,6],frequency=12,star=c(2013,8))
data=data.frame(HD[,6],WMT[,6])
data.ts=ts(data,frequency=12,start=c(2013,8))
plot(hd.ts,xlab="Time",ylab="Daily Adjusted Closing Price",main="Daily Adjusted Closing Price of Home Depot in past 5 years")

plot(wmt.ts,xlab="Time",ylab="Daily Adjusted Closing Price",main="Daily Adjusted Closing Price of Walmart in past 5 years")

plot(data.ts)

VARselect(data.ts, lag.max=8, type="const")$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      1      1      1      1
var=VAR(data.ts, p=1, type="const")
serial.test(var, lags.pt=10, type="PT.asymptotic")
## 
##  Portmanteau Test (asymptotic)
## 
## data:  Residuals of VAR object var
## Chi-squared = 41.817, df = 36, p-value = 0.2329
summary(var)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: HD...6., WMT...6. 
## Deterministic variables: const 
## Sample size: 60 
## Log Likelihood: -353.42 
## Roots of the characteristic polynomial:
## 1.006  0.88
## Call:
## VAR(y = data.ts, p = 1, type = "const")
## 
## 
## Estimation results for equation HD...6.: 
## ======================================== 
## HD...6. = HD...6..l1 + WMT...6..l1 + const 
## 
##             Estimate Std. Error t value Pr(>|t|)    
## HD...6..l1   1.01053    0.02717  37.192   <2e-16 ***
## WMT...6..l1 -0.02609    0.10059  -0.259    0.796    
## const        2.83186    5.71092   0.496    0.622    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 6.004 on 57 degrees of freedom
## Multiple R-Squared: 0.9767,  Adjusted R-squared: 0.9759 
## F-statistic:  1196 on 2 and 57 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation WMT...6.: 
## ========================================= 
## WMT...6. = HD...6..l1 + WMT...6..l1 + const 
## 
##             Estimate Std. Error t value Pr(>|t|)    
## HD...6..l1   0.02466    0.01843   1.338    0.186    
## WMT...6..l1  0.87508    0.06824  12.823   <2e-16 ***
## const        6.38905    3.87424   1.649    0.105    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 4.073 on 57 degrees of freedom
## Multiple R-Squared: 0.8524,  Adjusted R-squared: 0.8473 
## F-statistic: 164.6 on 2 and 57 DF,  p-value: < 2.2e-16 
## 
## 
## 
## Covariance matrix of residuals:
##          HD...6. WMT...6.
## HD...6.    36.05    10.08
## WMT...6.   10.08    16.59
## 
## Correlation matrix of residuals:
##          HD...6. WMT...6.
## HD...6.   1.0000   0.4122
## WMT...6.  0.4122   1.0000