#packages needed
library(readr)
library(ggplot2)
library(ggfortify)
library(vars)
library(forecast)
library(psych)
# I used walmart and target since they target the same consumers and should be correlated

wmt <- read_csv("WMT.csv")
Parsed with column specification:
cols(
  Date = col_character(),
  Open = col_double(),
  High = col_double(),
  Low = col_double(),
  Close = col_double(),
  `Adj Close` = col_double(),
  Volume = col_double()
)
tgt <- read_csv("TGT.csv")
Parsed with column specification:
cols(
  Date = col_character(),
  Open = col_double(),
  High = col_double(),
  Low = col_double(),
  Close = col_double(),
  `Adj Close` = col_double(),
  Volume = col_double()
)
summary(wmt)
     Date                Open             High             Low            Close          Adj Close          Volume         
 Length:72          Min.   : 57.29   Min.   : 61.47   Min.   :56.30   Min.   : 57.24   Min.   : 51.26   Min.   :113025400  
 Class :character   1st Qu.: 71.42   1st Qu.: 73.19   1st Qu.:69.94   1st Qu.: 71.31   1st Qu.: 63.60   1st Qu.:141117400  
 Mode  :character   Median : 75.34   Median : 78.84   Median :73.59   Median : 75.59   Median : 66.38   Median :164333800  
                    Mean   : 77.40   Mean   : 80.58   Mean   :74.74   Mean   : 77.76   Mean   : 70.39   Mean   :175626632  
                    3rd Qu.: 82.47   3rd Qu.: 87.45   3rd Qu.:78.43   3rd Qu.: 82.89   3rd Qu.: 74.75   3rd Qu.:204103475  
                    Max.   :105.96   Max.   :109.98   Max.   :98.52   Max.   :106.60   Max.   :101.61   Max.   :347169000  
summary(tgt)
     Date                Open            High            Low            Close         Adj Close         Volume         
 Length:72          Min.   :52.53   Min.   :56.29   Min.   :48.56   Min.   :52.29   Min.   :46.21   Min.   : 65991000  
 Class :character   1st Qu.:61.60   1st Qu.:63.90   1st Qu.:58.59   1st Qu.:61.80   1st Qu.:51.70   1st Qu.: 96417900  
 Mode  :character   Median :69.34   Median :73.14   Median :66.06   Median :69.47   Median :60.95   Median :114381400  
                    Mean   :68.92   Mean   :72.44   Mean   :65.39   Mean   :69.07   Mean   :60.21   Mean   :117846012  
                    3rd Qu.:75.84   3rd Qu.:79.21   3rd Qu.:71.89   3rd Qu.:75.96   3rd Qu.:67.33   3rd Qu.:134160100  
                    Max.   :88.33   Max.   :90.39   Max.   :86.38   Max.   :88.21   Max.   :84.30   Max.   :206344800  
#Explore both data sets

#walmart
wmtTS <- ts(wmt$Close, frequency=12, start=c(2013,1))
autoplot(wmtTS) + xlab("Month") + ylab("Adjusted Monthly Close Price")


acf(wmtTS)

pacf(wmtTS)

checkresiduals(wmtTS)

#target
tgtTS <- ts(tgt$Close, frequency=12, start=c(2013,1))
autoplot(tgtTS) + xlab("Month") + ylab("Adjusted Monthly Close Price")


acf(tgtTS)

pacf(tgtTS)

checkresiduals(tgtTS)

#Using the VAR Function

v1 <- VAR(cbind(wmtTS,tgtTS), p = 1, type = "both")
summary(v1)

VAR Estimation Results:
========================= 
Endogenous variables: wmtTS, tgtTS 
Deterministic variables: both 
Sample size: 71 
Log Likelihood: -398.206 
Roots of the characteristic polynomial:
0.8826 0.8826
Call:
VAR(y = cbind(wmtTS, tgtTS), p = 1, type = "both")


Estimation results for equation wmtTS: 
====================================== 
wmtTS = wmtTS.l1 + tgtTS.l1 + const + trend 

         Estimate Std. Error t value Pr(>|t|)    
wmtTS.l1  0.89340    0.05455  16.377  < 2e-16 ***
tgtTS.l1 -0.14136    0.05562  -2.541  0.01336 *  
const    16.69971    5.30306   3.149  0.00245 ** 
trend     0.04494    0.02663   1.687  0.09619 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Residual standard error: 4.053 on 67 degrees of freedom
Multiple R-Squared: 0.8439, Adjusted R-squared: 0.8369 
F-statistic: 120.7 on 3 and 67 DF,  p-value: < 2.2e-16 


Estimation results for equation tgtTS: 
====================================== 
tgtTS = wmtTS.l1 + tgtTS.l1 + const + trend 

         Estimate Std. Error t value Pr(>|t|)    
wmtTS.l1  0.04461    0.05979   0.746    0.458    
tgtTS.l1  0.86494    0.06096  14.188   <2e-16 ***
const     6.36185    5.81229   1.095    0.278    
trend    -0.01099    0.02919  -0.377    0.708    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Residual standard error: 4.442 on 67 degrees of freedom
Multiple R-Squared: 0.7597, Adjusted R-squared: 0.749 
F-statistic: 70.61 on 3 and 67 DF,  p-value: < 2.2e-16 



Covariance matrix of residuals:
       wmtTS  tgtTS
wmtTS 16.427  6.147
tgtTS  6.147 19.733

Correlation matrix of residuals:
       wmtTS  tgtTS
wmtTS 1.0000 0.3414
tgtTS 0.3414 1.0000
LS0tCnRpdGxlOiAiV2VlayA2IFZhciBEaXNjdXNzaW9uIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgpgYGB7cn0KI3BhY2thZ2VzIG5lZWRlZApsaWJyYXJ5KHJlYWRyKQpsaWJyYXJ5KGdncGxvdDIpCmxpYnJhcnkoZ2dmb3J0aWZ5KQpsaWJyYXJ5KHZhcnMpCmxpYnJhcnkoZm9yZWNhc3QpCmxpYnJhcnkocHN5Y2gpCmBgYAoKCmBgYHtyfQojIEkgdXNlZCB3YWxtYXJ0IGFuZCB0YXJnZXQgc2luY2UgdGhleSB0YXJnZXQgdGhlIHNhbWUgY29uc3VtZXJzIGFuZCBzaG91bGQgYmUgY29ycmVsYXRlZAoKd210IDwtIHJlYWRfY3N2KCJXTVQuY3N2IikKdGd0IDwtIHJlYWRfY3N2KCJUR1QuY3N2IikKCnN1bW1hcnkod210KQpzdW1tYXJ5KHRndCkKYGBgCgoKYGBge3J9CiNFeHBsb3JlIGJvdGggZGF0YSBzZXRzCgojd2FsbWFydAp3bXRUUyA8LSB0cyh3bXQkQ2xvc2UsIGZyZXF1ZW5jeT0xMiwgc3RhcnQ9YygyMDEzLDEpKQphdXRvcGxvdCh3bXRUUykgKyB4bGFiKCJNb250aCIpICsgeWxhYigiQWRqdXN0ZWQgTW9udGhseSBDbG9zZSBQcmljZSIpCgphY2Yod210VFMpCnBhY2Yod210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