AMZN <- read.csv("~/Documents/bc/forecasting/dis4/AMZN.csv")
EBAY <- read.csv("~/Documents/bc/forecasting/dis4/EBAY.csv")
library(fpp)
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.4.4
## Loading required package: fma
## Loading required package: expsmooth
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 3.4.4
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 3.4.4
library(vars)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following objects are masked from 'package:fma':
##
## cement, housing, petrol
## Loading required package: strucchange
## Loading required package: sandwich
## Loading required package: urca
mydata=data.frame(AMZN$Adj.Close, EBAY$Adj.Close)
tsdata=ts(mydata, frequency = 252, start=c(2013,07,18))
plot(tsdata)
By looking at the graph, my guess that Amazon and Ebay are not very correlated
var1=VARselect(tsdata, lag.max=8, type="const")$selection
summary(var1)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.0 1.0 2.5 2.5 4.0 4.0
var2=VAR(tsdata, lag.max=8, type="const")
summary(var2)
##
## VAR Estimation Results:
## =========================
## Endogenous variables: AMZN.Adj.Close, EBAY.Adj.Close
## Deterministic variables: const
## Sample size: 1255
## Log Likelihood: -5756.885
## Roots of the characteristic polynomial:
## 1.003 0.9888 0.4464 0.4464 0.377 0.3493 0.3493 0.2955
## Call:
## VAR(y = tsdata, type = "const", lag.max = 8)
##
##
## Estimation results for equation AMZN.Adj.Close:
## ===============================================
## AMZN.Adj.Close = AMZN.Adj.Close.l1 + EBAY.Adj.Close.l1 + AMZN.Adj.Close.l2 + EBAY.Adj.Close.l2 + AMZN.Adj.Close.l3 + EBAY.Adj.Close.l3 + AMZN.Adj.Close.l4 + EBAY.Adj.Close.l4 + const
##
## Estimate Std. Error t value Pr(>|t|)
## AMZN.Adj.Close.l1 0.99240 0.02908 34.127 < 2e-16 ***
## EBAY.Adj.Close.l1 1.49744 0.80644 1.857 0.06357 .
## AMZN.Adj.Close.l2 0.01740 0.04091 0.425 0.67061
## EBAY.Adj.Close.l2 -2.45100 1.10879 -2.211 0.02725 *
## AMZN.Adj.Close.l3 -0.10032 0.04093 -2.451 0.01440 *
## EBAY.Adj.Close.l3 3.23680 1.10839 2.920 0.00356 **
## AMZN.Adj.Close.l4 0.09350 0.02927 3.195 0.00144 **
## EBAY.Adj.Close.l4 -2.28436 0.80911 -2.823 0.00483 **
## const -0.72106 2.94737 -0.245 0.80677
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 12.84 on 1246 degrees of freedom
## Multiple R-Squared: 0.9989, Adjusted R-squared: 0.9989
## F-statistic: 1.397e+05 on 8 and 1246 DF, p-value: < 2.2e-16
##
##
## Estimation results for equation EBAY.Adj.Close:
## ===============================================
## EBAY.Adj.Close = AMZN.Adj.Close.l1 + EBAY.Adj.Close.l1 + AMZN.Adj.Close.l2 + EBAY.Adj.Close.l2 + AMZN.Adj.Close.l3 + EBAY.Adj.Close.l3 + AMZN.Adj.Close.l4 + EBAY.Adj.Close.l4 + const
##
## Estimate Std. Error t value Pr(>|t|)
## AMZN.Adj.Close.l1 0.0002828 0.0010525 0.269 0.78824
## EBAY.Adj.Close.l1 0.9626417 0.0291891 32.980 < 2e-16 ***
## AMZN.Adj.Close.l2 0.0030517 0.0014807 2.061 0.03952 *
## EBAY.Adj.Close.l2 -0.0527675 0.0401326 -1.315 0.18881
## AMZN.Adj.Close.l3 -0.0039375 0.0014816 -2.658 0.00797 **
## EBAY.Adj.Close.l3 0.1255594 0.0401180 3.130 0.00179 **
## AMZN.Adj.Close.l4 0.0007729 0.0010594 0.730 0.46580
## EBAY.Adj.Close.l4 -0.0476015 0.0292857 -1.625 0.10433
## const 0.2391766 0.1066795 2.242 0.02514 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
##
## Residual standard error: 0.4649 on 1246 degrees of freedom
## Multiple R-Squared: 0.9946, Adjusted R-squared: 0.9946
## F-statistic: 2.881e+04 on 8 and 1246 DF, p-value: < 2.2e-16
##
##
##
## Covariance matrix of residuals:
## AMZN.Adj.Close EBAY.Adj.Close
## AMZN.Adj.Close 164.966 1.4507
## EBAY.Adj.Close 1.451 0.2161
##
## Correlation matrix of residuals:
## AMZN.Adj.Close EBAY.Adj.Close
## AMZN.Adj.Close 1.000 0.243
## EBAY.Adj.Close 0.243 1.000
serial.test(var2, lags.pt=8, type="PT.asymptotic")
##
## Portmanteau Test (asymptotic)
##
## data: Residuals of VAR object var2
## Chi-squared = 26.817, df = 16, p-value = 0.04357
fcast=forecast(var2,h=252)
plot(fcast)
The result proves my guess at first, they are weakly correlated at 0.243.
And for the forecast for the next year, both stcoks are going upwards slowly.