I chose Microsoft and Apple stocks to compare because they are in the same sectore (tech) and have similar growth patterns. Below are the plots of their historical closing prices of the last five years.
# Clear the workspace
rm(list = ls()) # Clear environment
gc() # Clear unused memory
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 459936 24.6 994631 53.2 638942 34.2
## Vcells 825369 6.3 8388608 64.0 1633063 12.5
cat("\f") # Clear the console
library("feasts")
## Warning: package 'feasts' was built under R version 4.0.5
## Warning: package 'fabletools' was built under R version 4.0.5
library("seasonal")
## Warning: package 'seasonal' was built under R version 4.0.5
library("tsibble")
## Warning: package 'tsibble' was built under R version 4.0.5
library("MASS")
## Warning: package 'MASS' was built under R version 4.0.5
library("tsibbledata")
## Warning: package 'tsibbledata' was built under R version 4.0.5
library("dplyr")
## Warning: package 'dplyr' was built under R version 4.0.5
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 4.0.5
library("forecast")
## Warning: package 'forecast' was built under R version 4.0.5
library("fable")
## Warning: package 'fable' was built under R version 4.0.5
library("tseries")
## Warning: package 'tseries' was built under R version 4.0.5
library("weathermetrics")
## Warning: package 'weathermetrics' was built under R version 4.0.5
library("seasonalview")
## Warning: package 'seasonalview' was built under R version 4.0.5
library("tibbletime")
## Warning: package 'tibbletime' was built under R version 4.0.5
library("fpp3")
## Warning: package 'fpp3' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'lubridate' was built under R version 4.0.5
library("fma")
## Warning: package 'fma' was built under R version 4.0.5
library("vars")
## Warning: package 'vars' was built under R version 4.0.5
## Warning: package 'strucchange' was built under R version 4.0.5
## Warning: package 'zoo' was built under R version 4.0.5
## Warning: package 'sandwich' was built under R version 4.0.5
## Warning: package 'urca' was built under R version 4.0.5
## Warning: package 'lmtest' was built under R version 4.0.5
library("expsmooth")
## Warning: package 'expsmooth' was built under R version 4.0.5
setwd("/Users/spoll/OneDrive/Documents/Boston College/Predictive Analytics_Forecasting/Week 6")
# Load the datasets
msft <- read.csv("MSFT.csv"
, check.names = FALSE
, stringsAsFactors = FALSE
, na.strings = ""
)
aapl <- read.csv("AAPL.csv"
, check.names = FALSE
, stringsAsFactors = FALSE
, na.strings = ""
)
# Plot the time series of each stock
msft_ts <- ts(msft$Close, frequency = 365)
msft_ts %>% autoplot() + labs(title="MSFT Stock Close") +
ylab("Price ($)") +
theme(plot.title = element_text(hjust = 0.5))
aapl_ts <- ts(aapl$Close, frequency = 365)
aapl_ts %>% autoplot() + labs(title="AAPL Stock Close") +
ylab("Price ($)") +
theme(plot.title = element_text(hjust = 0.5))
Again here, both tech stocks have very similar differenced plots.
stock_close <- data.frame(msft$Close, aapl$Close)
stock_close_ts <- ts(stock_close, frequency = 365)
stock_close_ts %>% autoplot() + labs(title="MSFT/AAPL Stock Close") +
ylab("Price ($)") +
guides(colour=guide_legend(title="Closing Price")) +
theme(plot.title = element_text(hjust = 0.5))
stocks <-
#View(aapl)
#View(msft)
acf(stock_close_ts)
plot(diff(stock_close_ts))
acf(diff(stock_close_ts))
The ACF and PACF plots are similar but the ACF bars still exceed the bounds as the lag increases, compared to the PACF.
VARselect(stock_close_ts, lag.max = 8, type = "const")$selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 8 2 2 8
#VARselect(stock_close_ts, lag.max = 5, type = "const")$selection
#VARselect(stock_close_ts, lag.max = 3, type = "const")$selection
#VARselect(stock_close_ts, lag.max = 2, type = "const")$selection
#VARselect(stock_close_ts, lag.max = 1, type = "const")$selection
varfit <- VAR(stock_close_ts, p = 4, type = "const")
summary(varfit)
##
## VAR Estimation Results:
## =========================
## Endogenous variables: msft.Close, aapl.Close
## Deterministic variables: const
## Sample size: 1255
## Log Likelihood: -5264.09
## Roots of the characteristic polynomial:
## 0.9986 0.9937 0.4412 0.4412 0.4393 0.4393 0.3835 0.3835
## Call:
## VAR(y = stock_close_ts, p = 4, type = "const")
##
##
## Estimation results for equation msft.Close:
## ===========================================
## msft.Close = msft.Close.l1 + aapl.Close.l1 + msft.Close.l2 + aapl.Close.l2 + msft.Close.l3 + aapl.Close.l3 + msft.Close.l4 + aapl.Close.l4 + const
##
## Estimate Std. Error t value Pr(>|t|)
## msft.Close.l1 0.76758 0.04095 18.746 < 2e-16 ***
## aapl.Close.l1 0.12224 0.07358 1.661 0.096894 .
## msft.Close.l2 0.34592 0.05365 6.448 1.62e-10 ***
## aapl.Close.l2 -0.34831 0.10277 -3.389 0.000723 ***
## msft.Close.l3 0.01501 0.05390 0.278 0.780735
## aapl.Close.l3 -0.06151 0.10326 -0.596 0.551478
## msft.Close.l4 -0.12210 0.04106 -2.974 0.003000 **
## aapl.Close.l4 0.27544 0.07377 3.734 0.000197 ***
## const 0.13197 0.23284 0.567 0.570962
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 3.19 on 1246 degrees of freedom
## Multiple R-Squared: 0.9984, Adjusted R-squared: 0.9984
## F-statistic: 9.694e+04 on 8 and 1246 DF, p-value: < 2.2e-16
##
##
## Estimation results for equation aapl.Close:
## ===========================================
## aapl.Close = msft.Close.l1 + aapl.Close.l1 + msft.Close.l2 + aapl.Close.l2 + msft.Close.l3 + aapl.Close.l3 + msft.Close.l4 + aapl.Close.l4 + const
##
## Estimate Std. Error t value Pr(>|t|)
## msft.Close.l1 -0.05810 0.02278 -2.550 0.010891 *
## aapl.Close.l1 1.02285 0.04094 24.983 < 2e-16 ***
## msft.Close.l2 0.11348 0.02985 3.801 0.000151 ***
## aapl.Close.l2 -0.10901 0.05719 -1.906 0.056864 .
## msft.Close.l3 0.03495 0.02999 1.165 0.244117
## aapl.Close.l3 -0.06822 0.05746 -1.187 0.235334
## msft.Close.l4 -0.08252 0.02285 -3.612 0.000316 ***
## aapl.Close.l4 0.14124 0.04105 3.441 0.000599 ***
## const -0.11922 0.12956 -0.920 0.357656
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.775 on 1246 degrees of freedom
## Multiple R-Squared: 0.9984, Adjusted R-squared: 0.9984
## F-statistic: 9.958e+04 on 8 and 1246 DF, p-value: < 2.2e-16
##
##
##
## Covariance matrix of residuals:
## msft.Close aapl.Close
## msft.Close 10.178 4.096
## aapl.Close 4.096 3.151
##
## Correlation matrix of residuals:
## msft.Close aapl.Close
## msft.Close 1.0000 0.7232
## aapl.Close 0.7232 1.0000
stock_var_fc <- forecast(varfit, h = 12)
stock_var_fc %>% plot()
tsdisplay(residuals(varfit))
Even after differencing, the VAR forecast for both stocks are quite similar. It was interesting that MSFT’s estimate was negative while the AAPL estimate was positive.
stock_diff <- diff(stock_close_ts)
VARselect(stock_diff, lag.max = 8, type = "const")$selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 8 1 1 8
var_diff <- VAR(stock_diff, p = 1, type = "const")
summary(var_diff)
##
## VAR Estimation Results:
## =========================
## Endogenous variables: msft.Close, aapl.Close
## Deterministic variables: const
## Sample size: 1257
## Log Likelihood: -5290.453
## Roots of the characteristic polynomial:
## 0.1987 0.0005393
## Call:
## VAR(y = stock_diff, p = 1, type = "const")
##
##
## Estimation results for equation msft.Close:
## ===========================================
## msft.Close = msft.Close.l1 + aapl.Close.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## msft.Close.l1 -0.23258 0.04043 -5.753 1.1e-08 ***
## aapl.Close.l1 0.13099 0.07363 1.779 0.0755 .
## const 0.19724 0.09077 2.173 0.0300 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 3.213 on 1254 degrees of freedom
## Multiple R-Squared: 0.03493, Adjusted R-squared: 0.03339
## F-statistic: 22.7 on 2 and 1254 DF, p-value: 2.076e-10
##
##
## Estimation results for equation aapl.Close:
## ===========================================
## aapl.Close = msft.Close.l1 + aapl.Close.l1 + const
##
## Estimate Std. Error t value Pr(>|t|)
## msft.Close.l1 -0.06033 0.02250 -2.681 0.00744 **
## aapl.Close.l1 0.03444 0.04099 0.840 0.40094
## const 0.10974 0.05053 2.172 0.03007 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 1.788 on 1254 degrees of freedom
## Multiple R-Squared: 0.007757, Adjusted R-squared: 0.006175
## F-statistic: 4.902 on 2 and 1254 DF, p-value: 0.007575
##
##
##
## Covariance matrix of residuals:
## msft.Close aapl.Close
## msft.Close 10.321 4.174
## aapl.Close 4.174 3.198
##
## Correlation matrix of residuals:
## msft.Close aapl.Close
## msft.Close 1.0000 0.7264
## aapl.Close 0.7264 1.0000
var_diff_fc <- forecast(var_diff, h = 12)
var_diff_fc %>% autoplot() + labs(title="Differenced MSFT/AAPL Stock Close Forecast") +
ylab("Price ($)") +
guides(colour=guide_legend(title="Closing Price")) +
theme(plot.title = element_text(hjust = 0.5))