Walmart and Target stocks were chosen for potential use in a VAR model. The assumption in choosing these stocks was that, as competitors, an increase in one’s stock price might be correlated with a decrease in the other’s. Another thought was that an increase in one’s price might translate to an increase in the others as a signal of people’s move toward in-store vs. online shopping. Either way, the assumed relationship would fit into the VAR model construct.

Plots of 5 years of adjusted close prices for Walmart and Target are below. Of note, the date was changed to an index to avoid needing to fill gaps in the code.

## # A tibble: 1 × 1
##   ndiffs
##    <int>
## 1      1
## # A tibble: 1 × 1
##   ndiffs
##    <int>
## 1      1

Vector autoregressions were modeled for the first-differenced adjusting closing prices for Walmart and Target with one optimizing \(AIC_c\) and the other optimizing \(BIC\). The model optimizing \(BIC\) was VAR(0) suggesting no relationship between the two stocks. However, the \(AIC_c\) optimized model was a VAR(4) with mean. The VAR(4) model’s report is below, along with a 14 day forecast of changes in the stocks’ prices over the next 14 days.

Essentially, the VAR(4) model suggests that changes in either stock’s price can be calculated from prior price changes in its own price and the paired price out to a lag of 4 days. It’s unclear the strength of the model’s fit as residuals show a significant amount of autocorrelation.

## Series: Adj_Close_T, Adj_Close_W 
## Model: VAR(4) w/ mean 
## 
## Coefficients for Adj_Close_T:
##       lag(Adj_Close_T,1)  lag(Adj_Close_W,1)  lag(Adj_Close_T,2)
##                   0.0338             -0.1288              0.0108
## s.e.              0.0307              0.0489              0.0306
##       lag(Adj_Close_W,2)  lag(Adj_Close_T,3)  lag(Adj_Close_W,3)
##                   0.0349              0.0543             -0.0049
## s.e.              0.0491              0.0307              0.0491
##       lag(Adj_Close_T,4)  lag(Adj_Close_W,4)  constant
##                  -0.0795              0.0757    0.1557
## s.e.              0.0307              0.0490    0.0692
## 
## Coefficients for Adj_Close_W:
##       lag(Adj_Close_T,1)  lag(Adj_Close_W,1)  lag(Adj_Close_T,2)
##                   0.0012             -0.0996              0.0284
## s.e.              0.0192              0.0306              0.0192
##       lag(Adj_Close_W,2)  lag(Adj_Close_T,3)  lag(Adj_Close_W,3)
##                  -0.0057             -0.0171              0.0165
## s.e.              0.0308              0.0192              0.0307
##       lag(Adj_Close_T,4)  lag(Adj_Close_W,4)  constant
##                   0.0146             -0.0654    0.0778
## s.e.              0.0192              0.0307    0.0434
## 
## Residual covariance matrix:
##             Adj_Close_T Adj_Close_W
## Adj_Close_T      5.8932      1.4285
## Adj_Close_W      1.4285      2.3147
## 
## log likelihood = -5089.5
## AIC = 10223  AICc = 10223.82 BIC = 10335.97

R Code

knitr::opts_chunk$set(echo = TRUE)
local({
  hook_source <- knitr::knit_hooks$get('source')
  knitr::knit_hooks$set(source = function(x, options) {
    x <- x[!grepl('# SECRET!!$', x)]
    hook_source(x, options)
  })
})

#INVOKE APPROPRIATE LIBRARIES

library("feasts")
library("seasonal")
library("tsibble")
library("tsibbledata")
library("dplyr")
library("ggplot2")
library("forecast")
library("fable")
library("fpp3")
library("sqldf")
library("psych")
library("PerformanceAnalytics")
library("car")
library("kableExtra")
library("glmnet")
library("ISLR")
library("leaps")


#csv path for the data
csv_path <- paste0("/Users/timothywhite/Library/CloudStorage/",
                   "OneDrive-Personal/Documents/PROFESSIONAL/GRAD SCHOOL/",
                   "BOSTON COLLEGE/SPRING 2022/",

#retrieve Walmart data
WMT_data <- read.csv(paste0(csv_path, "WMT5.csv"),
                       stringsAsFactors = TRUE)

WMT_data2 <-
  WMT_data %>%
  mutate(Adj_Close_W = Adj.Close,
         Date = as.Date(Date),
         IDX = 1:length(rownames(WMT_data))) %>%
  select(IDX,
         Adj_Close_W)

#retrieve Amazon data
AMZN_data <- read.csv(paste0(csv_path, "AMZN5.csv"),
                     stringsAsFactors = TRUE)

AMZN_data2 <-
  AMZN_data %>%
  mutate(Adj_Close_A = Adj.Close,
         Date = as.Date(Date),
         IDX = 1:length(rownames(AMZN_data))) %>%
  select(IDX,
         Adj_Close_A)

#retrieve Target data
TGT_data <- read.csv(paste0(csv_path, "TGT5.csv"),
                     stringsAsFactors = TRUE)

TGT_data2 <-
  TGT_data %>%
  mutate(Adj_Close_T = Adj.Close,
         Date = as.Date(Date),
         IDX = 1:length(rownames(TGT_data))) %>%
  select(IDX,
         Adj_Close_T)

#Join Target and Walmart into a tsibble
WMT_TGT_ts <- 
  merge(WMT_data2, TGT_data2, by = "IDX") %>%
  as_tsibble(index = IDX)

#Plot of Walmart prices
WMT_TGT_ts %>%
  autoplot(Adj_Close_W)

#Plot of Target prices
WMT_TGT_ts %>%
  autoplot(Adj_Close_T)

WMT_TGT_ts %>%
  features(Adj_Close_W, unitroot_ndiffs)

WMT_TGT_ts %>%
  features(Adj_Close_T, unitroot_ndiffs)

#Differenced tsibble of Walmart and Target
WMT_TGT_ts2 <-
  WMT_TGT_ts %>%
  mutate(Adj_Close_W = difference(Adj_Close_W),
         Adj_Close_T = difference(Adj_Close_T))

WMT_TGT_VAR <-
  WMT_TGT_ts2 %>%
  model(aicc = VAR(vars(Adj_Close_T, Adj_Close_W), ic = "aicc"),
        bic = VAR(vars(Adj_Close_T, Adj_Close_W), ic = "bic"))


WMT_TGT_VAR %>%
  select(aicc) %>%
  forecast(h = 14) %>%
  autoplot(WMT_TGT_ts2 %>% filter(IDX > 1200))

WMT_TGT_VAR %>%
  select(aicc) %>%
  report()

WMT_TGT_VAR %>%
  select(aicc) %>%
  augment() %>%
  ACF(.innov) %>%
  autoplot()