# The two stock I choose are Facebook and Tesla. They are the world's most recognizable brands. It is believed that these two stock share some similar pattern.
library(fpp2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.2
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.6.2
## Registered S3 method overwritten by 'xts':
##   method     from
##   as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Loading required package: fma
## Loading required package: expsmooth
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: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
FB <- read.csv('/Users/jiahaolin/Downloads/Forecasting/week 6/FB.csv')
TSLA <- read.csv('/Users/jiahaolin/Downloads/Forecasting/week 6/TSLA.csv')
ts(FB['Adj.Close']) %>% autoplot(series='FB')+
   ts(TSLA['Adj.Close']) %>% autolayer(series='TSLA')

# Although two stocks differ from each other in the magnitudes, they fluctuate in the same direction. 
stock <- cbind(FB['Adj.Close'],TSLA['Adj.Close']) #Here, I build a data containing both stock prices.
names(stock) <- c('FB','TSLA')
stock.var1 <- VAR(stock, type='const', lag.max=1, ic='AIC') # First, let's try lag 1.
stock.var1
## 
## VAR Estimation Results:
## ======================= 
## 
## Estimated coefficients for equation FB: 
## ======================================= 
## Call:
## FB = FB.l1 + TSLA.l1 + const 
## 
##        FB.l1      TSLA.l1        const 
## 0.9947690854 0.0006786073 0.7171436931 
## 
## 
## Estimated coefficients for equation TSLA: 
## ========================================= 
## Call:
## TSLA = FB.l1 + TSLA.l1 + const 
## 
##     FB.l1   TSLA.l1     const 
##  0.022113  1.005170 -4.283316
ts(FB['Adj.Close']) %>% autoplot(series='FB')+
   ts(fitted(stock.var1)[,1]) %>% autolayer(series='FB.fit')

# The fitted value overlaps the actual one.
# We can check the result by using normal linear regression:
FB.lm <- lm(FB[2:nrow(stock)]~FB[1:(nrow(stock)-1)]+TSLA[1:(nrow(stock)-1)], data=stock)
TSLA.lm <- lm(TSLA[2:nrow(stock)]~TSLA[1:(nrow(stock)-1)]+FB[1:(nrow(stock)-1)], data=stock)
FB.lm
## 
## Call:
## lm(formula = FB[2:nrow(stock)] ~ FB[1:(nrow(stock) - 1)] + TSLA[1:(nrow(stock) - 
##     1)], data = stock)
## 
## Coefficients:
##               (Intercept)    FB[1:(nrow(stock) - 1)]  
##                 0.7171437                  0.9947691  
## TSLA[1:(nrow(stock) - 1)]  
##                 0.0006786
TSLA.lm # The results are similar with what we get from VAR model. It shows that both FB and TSLA stock price in lag 1 are positively affect the current FB and TSLA stock price.
## 
## Call:
## lm(formula = TSLA[2:nrow(stock)] ~ TSLA[1:(nrow(stock) - 1)] + 
##     FB[1:(nrow(stock) - 1)], data = stock)
## 
## Coefficients:
##               (Intercept)  TSLA[1:(nrow(stock) - 1)]  
##                  -4.28332                    1.00517  
##   FB[1:(nrow(stock) - 1)]  
##                   0.02211
# Now, let's try lag 10:
stock.var10 <- VAR(stock, type='const', lag.max=10, ic='AIC')
stock.var10
## 
## VAR Estimation Results:
## ======================= 
## 
## Estimated coefficients for equation FB: 
## ======================================= 
## Call:
## FB = FB.l1 + TSLA.l1 + FB.l2 + TSLA.l2 + FB.l3 + TSLA.l3 + FB.l4 + TSLA.l4 + FB.l5 + TSLA.l5 + FB.l6 + TSLA.l6 + FB.l7 + TSLA.l7 + FB.l8 + TSLA.l8 + FB.l9 + TSLA.l9 + FB.l10 + TSLA.l10 + const 
## 
##         FB.l1       TSLA.l1         FB.l2       TSLA.l2         FB.l3 
##  0.9482240030 -0.0128707513  0.0338896007  0.0307309716 -0.0219028839 
##       TSLA.l3         FB.l4       TSLA.l4         FB.l5       TSLA.l5 
## -0.0126474440  0.0251808795  0.0017821483 -0.0190418016 -0.0071458097 
##         FB.l6       TSLA.l6         FB.l7       TSLA.l7         FB.l8 
## -0.0114036876 -0.0054059346  0.0878136282  0.0013571989 -0.1122692777 
##       TSLA.l8         FB.l9       TSLA.l9        FB.l10      TSLA.l10 
## -0.0007821859  0.0918184608  0.0132098672 -0.0268071865 -0.0076371510 
##         const 
##  0.6492238172 
## 
## 
## Estimated coefficients for equation TSLA: 
## ========================================= 
## Call:
## TSLA = FB.l1 + TSLA.l1 + FB.l2 + TSLA.l2 + FB.l3 + TSLA.l3 + FB.l4 + TSLA.l4 + FB.l5 + TSLA.l5 + FB.l6 + TSLA.l6 + FB.l7 + TSLA.l7 + FB.l8 + TSLA.l8 + FB.l9 + TSLA.l9 + FB.l10 + TSLA.l10 + const 
## 
##        FB.l1      TSLA.l1        FB.l2      TSLA.l2        FB.l3 
##  0.171938837  0.988659082 -0.137407121  0.043093454 -0.022173757 
##      TSLA.l3        FB.l4      TSLA.l4        FB.l5      TSLA.l5 
##  0.097376761  0.449331720 -0.261108680 -1.147922661  0.248959947 
##        FB.l6      TSLA.l6        FB.l7      TSLA.l7        FB.l8 
##  0.460031851 -0.114685424  0.658925016  0.005859767 -0.449950884 
##      TSLA.l8        FB.l9      TSLA.l9       FB.l10     TSLA.l10 
##  0.004285374 -0.066824521 -0.075955484  0.104231917  0.068669270 
##        const 
## -4.004057727
ts(FB['Adj.Close']) %>% autoplot(series='FB')+
   ts(fitted(stock.var10)[,1]) %>% autolayer(series='FB.fit')

# The fiited value leads the actual one.