R Packages Utilized
library(readr)
library(ggplot2)
library(ggfortify)
library(vars)
library(forecast)
library(psych)
Importing 5 years of Monthly Adjusted Closing Data for two stocks
## I chose a monthly frequency for 1/1/2013 through 12/31/2018
## Walmart Stock (NYSE:WMT)
wmt <- read_csv("C:/Users/bryce_anderson/Desktop/Boston College/Predictive Analytics and Forecasting/Week 6 (PA&F)/WMT.csv")
## Source: https://finance.yahoo.com/quote/WMT/history?period1=1357016400&period2=1546232400&interval=1mo&filter=history&frequency=1mo
## Target Stock (NYSE:TGT)
tgt <- read_csv("C:/Users/bryce_anderson/Desktop/Boston College/Predictive Analytics and Forecasting/Week 6 (PA&F)/TGT.csv")
## Source: https://finance.yahoo.com/quote/TGT/history?period1=1357016400&period2=1546232400&interval=1mo&filter=history&frequency=1mo
Walmart (NYSE:WMT)
## Walmart (NYSE:WMT)
wmtTS <- ts(wmt$close, frequency=12, start=c(2013,1)) ## Creating time series for close variable
autoplot(wmtTS) + xlab("Month") + ylab("Adjusted Monthly Close Price")
acf(wmtTS)
pacf(wmtTS)
checkresiduals(wmtTS)
Target (NYSE:TGT)
## Target (NYSE:TGT)
tgtTS <- ts(tgt$close, frequency=12, start=c(2013,1)) ## Creating time series for close variable
autoplot(tgtTS) + xlab("Month") + ylab("Adjusted Monthly Close Price")
acf(tgtTS)
pacf(tgtTS)
checkresiduals(tgtTS)
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: -386.377
## Roots of the characteristic polynomial:
## 0.8855 0.8855
## 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.90127 0.05456 16.519 <2e-16 ***
## tgtTS.l1 -0.13622 0.05858 -2.325 0.0231 *
## const 12.77208 4.29701 2.972 0.0041 **
## trend 0.07853 0.03217 2.441 0.0173 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 3.811 on 67 degrees of freedom
## Multiple R-Squared: 0.892, Adjusted R-squared: 0.8872
## F-statistic: 184.5 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.046776 0.056965 0.821 0.414
## tgtTS.l1 0.862912 0.061162 14.109 <2e-16 ***
## const 4.977569 4.486441 1.109 0.271
## trend 0.006157 0.033589 0.183 0.855
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 3.979 on 67 degrees of freedom
## Multiple R-Squared: 0.8197, Adjusted R-squared: 0.8117
## F-statistic: 101.6 on 3 and 67 DF, p-value: < 2.2e-16
##
##
##
## Covariance matrix of residuals:
## wmtTS tgtTS
## wmtTS 14.53 4.98
## tgtTS 4.98 15.84
##
## Correlation matrix of residuals:
## wmtTS tgtTS
## wmtTS 1.0000 0.3284
## tgtTS 0.3284 1.0000
## Diagram of fit and residuals for both time series
plot(v1)