#TSLA vs GM
tsla<-getSymbols("TSLA",auto.assign =FALSE,from = "2011-09-01", to = "2021-09-01") #as my train
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
head(tsla)
##            TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume TSLA.Adjusted
## 2011-09-01     4.932     4.974    4.768      4.800     4240500         4.800
## 2011-09-02     4.732     4.798    4.536      4.614     3849500         4.614
## 2011-09-06     4.500     4.640    4.458      4.588     4049000         4.588
## 2011-09-07     4.678     4.800    4.656      4.768     2296000         4.768
## 2011-09-08     4.716     4.806    4.656      4.722     2528500         4.722
## 2011-09-09     4.674     4.714    4.510      4.594     3346500         4.594
plot(tsla)

tslaClose<-tsla$TSLA.Close
plot(tslaClose)

gm<-getSymbols("GM",auto.assign =FALSE,from = "2011-09-01", to = "2021-09-01") #as my train
head(gm)
##            GM.Open GM.High GM.Low GM.Close GM.Volume GM.Adjusted
## 2011-09-01   24.09   24.25  22.91    23.03  16926200    17.79974
## 2011-09-02   22.41   22.55  21.73    22.07  14086700    17.05777
## 2011-09-06   21.36   21.58  20.88    21.44  14282500    16.57084
## 2011-09-07   21.82   23.04  21.82    22.86  13412200    17.66835
## 2011-09-08   22.79   23.13  22.24    22.48  11782500    17.37465
## 2011-09-09   22.36   22.45  21.47    21.76  11920600    16.81817
plot(gm)

gmClose<-gm$GM.Close
plot(gmClose)

#time series
tsla.ts<-ts(tsla$TSLA.Close,frequency=365)
autoplot(tsla.ts)+
  labs(title="TSLA daily stock price 2011-09-01 to 2021-09-01")

gm.ts<-ts(gm$GM.Close,frequency=365)
autoplot(gm.ts)+
  labs(title="General Motors daily stock price 2011-09-01 to 2021-09-01")

md<-data.frame(tsla$TSLA.Close,gm$GM.Close)
md.ts<-ts(md,frequency=365)
md.ts%>%
  autoplot()+
  labs(title="TSLA vs GM 2011-09-01 to 2021-09-01",ylab="Price in $")

#Differencing
acf(tsla.ts)

acf(gm.ts)

unitroot_ndiffs(tsla.ts)
## ndiffs 
##      2
#ndiffs=2
unitroot_ndiffs(gm.ts)
## ndiffs 
##      1
#ndiffs=1
acf(diff(tsla.ts,differences = 2))

acf(diff(gm.ts,differences=1))

VARselect(md.ts,lag.max=10,type="const")$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##     10     10      1     10
#AIC(n)  HQ(n)  SC(n) FPE(n) 
#10     10      1     10 
#it indicates choosing 10 as the lag number

varfit<-VAR(md.ts,p=10,type="const")
summary(varfit)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: TSLA.Close, GM.Close 
## Deterministic variables: const 
## Sample size: 2506 
## Log Likelihood: -11350.364 
## Roots of the characteristic polynomial:
## 1.001 0.9936 0.8131 0.8131 0.7985 0.7886 0.7886 0.7848 0.7848 0.7801 0.7801 0.7396 0.7396 0.7029 0.7029 0.6621 0.6524 0.6524 0.6131 0.6131
## Call:
## VAR(y = md.ts, p = 10, type = "const")
## 
## 
## Estimation results for equation TSLA.Close: 
## =========================================== 
## TSLA.Close = TSLA.Close.l1 + GM.Close.l1 + TSLA.Close.l2 + GM.Close.l2 + TSLA.Close.l3 + GM.Close.l3 + TSLA.Close.l4 + GM.Close.l4 + TSLA.Close.l5 + GM.Close.l5 + TSLA.Close.l6 + GM.Close.l6 + TSLA.Close.l7 + GM.Close.l7 + TSLA.Close.l8 + GM.Close.l8 + TSLA.Close.l9 + GM.Close.l9 + TSLA.Close.l10 + GM.Close.l10 + const 
## 
##                Estimate Std. Error t value Pr(>|t|)    
## TSLA.Close.l1   0.92642    0.02006  46.191  < 2e-16 ***
## GM.Close.l1     0.49108    0.23024   2.133 0.033032 *  
## TSLA.Close.l2   0.04910    0.02735   1.796 0.072693 .  
## GM.Close.l2    -0.14034    0.32459  -0.432 0.665530    
## TSLA.Close.l3   0.09816    0.02731   3.594 0.000332 ***
## GM.Close.l3    -0.12427    0.32411  -0.383 0.701444    
## TSLA.Close.l4  -0.04044    0.02738  -1.477 0.139792    
## GM.Close.l4    -0.76954    0.32363  -2.378 0.017488 *  
## TSLA.Close.l5  -0.13992    0.02726  -5.133 3.06e-07 ***
## GM.Close.l5     0.83360    0.32397   2.573 0.010138 *  
## TSLA.Close.l6   0.12743    0.02723   4.679 3.03e-06 ***
## GM.Close.l6    -0.58861    0.32443  -1.814 0.069749 .  
## TSLA.Close.l7  -0.02486    0.02736  -0.908 0.363743    
## GM.Close.l7     0.70442    0.32477   2.169 0.030178 *  
## TSLA.Close.l8   0.07090    0.02735   2.593 0.009575 ** 
## GM.Close.l8    -0.96801    0.32519  -2.977 0.002941 ** 
## TSLA.Close.l9   0.03411    0.02738   1.246 0.212889    
## GM.Close.l9     1.17025    0.32640   3.585 0.000343 ***
## TSLA.Close.l10 -0.09861    0.02019  -4.884 1.11e-06 ***
## GM.Close.l10   -0.65441    0.23115  -2.831 0.004677 ** 
## const           1.59109    0.85187   1.868 0.061912 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 7.928 on 2485 degrees of freedom
## Multiple R-Squared: 0.9982,  Adjusted R-squared: 0.9981 
## F-statistic: 6.721e+04 on 20 and 2485 DF,  p-value: < 2.2e-16 
## 
## 
## Estimation results for equation GM.Close: 
## ========================================= 
## GM.Close = TSLA.Close.l1 + GM.Close.l1 + TSLA.Close.l2 + GM.Close.l2 + TSLA.Close.l3 + GM.Close.l3 + TSLA.Close.l4 + GM.Close.l4 + TSLA.Close.l5 + GM.Close.l5 + TSLA.Close.l6 + GM.Close.l6 + TSLA.Close.l7 + GM.Close.l7 + TSLA.Close.l8 + GM.Close.l8 + TSLA.Close.l9 + GM.Close.l9 + TSLA.Close.l10 + GM.Close.l10 + const 
## 
##                  Estimate Std. Error t value Pr(>|t|)    
## TSLA.Close.l1   0.0013976  0.0017581   0.795 0.426717    
## GM.Close.l1     0.9993499  0.0201832  49.514  < 2e-16 ***
## TSLA.Close.l2   0.0005292  0.0023973   0.221 0.825300    
## GM.Close.l2    -0.0186527  0.0284540  -0.656 0.512181    
## TSLA.Close.l3   0.0008139  0.0023940   0.340 0.733897    
## GM.Close.l3     0.0181460  0.0284115   0.639 0.523088    
## TSLA.Close.l4  -0.0008107  0.0024002  -0.338 0.735571    
## GM.Close.l4    -0.0215986  0.0283692  -0.761 0.446527    
## TSLA.Close.l5  -0.0041278  0.0023894  -1.728 0.084191 .  
## GM.Close.l5     0.0137470  0.0283993   0.484 0.628385    
## TSLA.Close.l6  -0.0004049  0.0023873  -0.170 0.865331    
## GM.Close.l6    -0.0229991  0.0284392  -0.809 0.418760    
## TSLA.Close.l7   0.0049535  0.0023985   2.065 0.039005 *  
## GM.Close.l7     0.0837202  0.0284693   2.941 0.003305 ** 
## TSLA.Close.l8  -0.0006813  0.0023971  -0.284 0.776269    
## GM.Close.l8    -0.0968468  0.0285060  -3.397 0.000691 ***
## TSLA.Close.l9  -0.0010076  0.0023999  -0.420 0.674629    
## GM.Close.l9     0.0751603  0.0286122   2.627 0.008670 ** 
## TSLA.Close.l10 -0.0003697  0.0017700  -0.209 0.834561    
## GM.Close.l10   -0.0379867  0.0202630  -1.875 0.060953 .  
## const           0.2516635  0.0746750   3.370 0.000763 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.695 on 2485 degrees of freedom
## Multiple R-Squared: 0.9926,  Adjusted R-squared: 0.9925 
## F-statistic: 1.662e+04 on 20 and 2485 DF,  p-value: < 2.2e-16 
## 
## 
## 
## Covariance matrix of residuals:
##            TSLA.Close GM.Close
## TSLA.Close    62.8512   0.6342
## GM.Close       0.6342   0.4830
## 
## Correlation matrix of residuals:
##            TSLA.Close GM.Close
## TSLA.Close     1.0000   0.1151
## GM.Close       0.1151   1.0000