suppressMessages(library(vars))
library(urca) #ca.jo()
library(tsDyn) #rank.select VECM
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(thongke.club)
dulieu <-price_tom

1. Kiểm tra đồng liên kết johansen

# ca.jo(x, type = c("eigen", "trace"), ecdet = c("none", "const", "trend"), K = 2, spec=c("longrun", "transitory"), season = NULL, dumvar = NULL)

pp <- c("eigen", "trace")
ec <- c("none", "const", "trend")

summary(ca.jo (dulieu, type="eigen", ecdet="const"))
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: maximal eigenvalue statistic (lambda max) , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 4.941049e-01 4.059066e-01 3.397202e-01 2.987212e-01 1.110223e-16
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 3 | 41.87  7.52  9.24 12.97
## r <= 2 | 48.98 13.75 15.67 20.20
## r <= 1 | 61.44 19.77 22.00 26.81
## r = 0  | 80.41 25.56 28.14 33.24
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##               price_ao.l2  price_bs.l2 price_bl.l2  price_xk.l2      constant
## price_ao.l2   1.000000000   1.00000000   1.0000000    1.0000000     1.0000000
## price_bs.l2  -2.171621694  -0.03418338   0.3277819    0.6722894    -5.6188320
## price_bl.l2  -0.083469583  -3.45321394  -0.3226107   -3.0175846    -4.6929536
## price_xk.l2   0.001468523  -1.44235664  -0.4691159    3.0288594     0.1840072
## constant    153.373855785 619.14572698 -35.0462413 -279.1540712 -1565.3562799
## 
## Weights W:
## (This is the loading matrix)
## 
##            price_ao.l2 price_bs.l2 price_bl.l2 price_xk.l2      constant
## price_ao.d  -0.2860150   0.1540496 -0.89086839 -0.05564401 -1.368872e-18
## price_bs.d   0.5548829   0.1056625 -0.42821267 -0.02346870 -1.119193e-17
## price_bl.d  -0.1544955   0.2921216 -0.24827129  0.07652809  1.380346e-17
## price_xk.d  -0.1101736   0.2095363  0.07997943 -0.19963179  6.085855e-19
summary(ca.jo (dulieu, type="trace", ecdet="none"))
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , with linear trend 
## 
## Eigenvalues (lambda):
## [1] 0.4940565 0.4058060 0.3396911 0.2987173
## 
## Values of teststatistic and critical values of test:
## 
##            test 10pct  5pct  1pct
## r <= 3 |  41.87  6.50  8.18 11.65
## r <= 2 |  90.85 15.66 17.95 23.52
## r <= 1 | 152.27 28.71 31.52 37.22
## r = 0  | 232.67 45.23 48.28 55.43
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##              price_ao.l2 price_bs.l2 price_bl.l2 price_xk.l2
## price_ao.l2  1.000000000  1.00000000   1.0000000    1.000000
## price_bs.l2 -2.171568881 -0.03820047   0.3284309    0.671713
## price_bl.l2 -0.082565793 -3.43720294  -0.3220372   -3.020029
## price_xk.l2  0.001679455 -1.43705383  -0.4687727    3.030970
## 
## Weights W:
## (This is the loading matrix)
## 
##            price_ao.l2 price_bs.l2 price_bl.l2 price_xk.l2
## price_ao.d  -0.2865403   0.1545090 -0.89090486 -0.05554158
## price_bs.d   0.5545453   0.1062251 -0.42850130 -0.02340507
## price_bl.d  -0.1552394   0.2934133 -0.24882139  0.07653035
## price_xk.d  -0.1105804   0.2105927  0.07918119 -0.19948317
# include = c("const", "trend", "none", "both"),
summary(rank.select(dulieu, include="const"))
## Best AIC: rank= 4  lag= 1 
## Best BIC: rank= 4  lag= 1 
## Best HQ : rank= 4  lag= 1 
## 
## Best number of lags:
##     r=0 r=1 r=2 r=3 r=4
## AIC  10  10   8   2   1
## BIC   2   1   1   1   1
## HQ    2   2   2   1   1
summary(rank.select(dulieu, include="none"))
## Best AIC: rank= 3  lag= 2 
## Best BIC: rank= 3  lag= 1 
## Best HQ : rank= 3  lag= 1 
## 
## Best number of lags:
##     r=0 r=1 r=2 r=3 r=4
## AIC  10   8   8   2   2
## BIC   2   1   1   1   1
## HQ    2   2   2   1   1

Tìm lag

summary(lags.select(dulieu, include="both"))
## Best AIC:  lag= 1 
## Best BIC:  lag= 1 
## Best HQ :  lag= 1 
## 
## Best number of lags:
##     r=0
## AIC   1
## BIC   1
## HQ    1

Chạy VECM

ve <- VECM(dulieu, lag=1 ,estim="ML")
summary(ve)
## #############
## ###Model VECM 
## #############
## Full sample size: 120    End sample size: 118
## Number of variables: 4   Number of estimated slope parameters 24
## AIC 2726.332     BIC 2801.14     SSR 185416.2
## Cointegrating vector (estimated by ML):
##    price_ao  price_bs    price_bl    price_xk
## r1        1 -2.171569 -0.08256579 0.001679455
## 
## 
##                   ECT                 Intercept             price_ao -1       
## Equation price_ao -0.2865(0.1144)*    -43.6645(17.6129)*    -0.3428(0.1204)** 
## Equation price_bs 0.5545(0.0798)***   85.0986(12.2893)***   -0.2919(0.0840)***
## Equation price_bl -0.1552(0.1148)     -23.4854(17.6781)     0.0338(0.1208)    
## Equation price_xk -0.1106(0.1190)     -16.8105(18.3195)     0.1237(0.1252)    
##                   price_bs -1         price_bl -1        price_xk -1        
## Equation price_ao -0.2692(0.1592).    0.1559(0.1005)     -0.0844(0.0887)    
## Equation price_bs 0.0641(0.1111)      0.1526(0.0701)*    -0.0867(0.0619)    
## Equation price_bl -0.1523(0.1598)     -0.4415(0.1009)*** 0.0341(0.0890)     
## Equation price_xk 0.0012(0.1656)      0.0450(0.1046)     -0.5015(0.0922)***
rank.test(ve)
## Rank selected: 4 (first eigen test with pval above 5 %: 0 %)
vecm <- ca.jo (dulieu, spec=c("longrun"))
summary(vecm)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: maximal eigenvalue statistic (lambda max) , with linear trend 
## 
## Eigenvalues (lambda):
## [1] 0.4940565 0.4058060 0.3396911 0.2987173
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 3 | 41.87  6.50  8.18 11.65
## r <= 2 | 48.98 12.91 14.90 19.19
## r <= 1 | 61.42 18.90 21.07 25.75
## r = 0  | 80.40 24.78 27.14 32.14
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##              price_ao.l2 price_bs.l2 price_bl.l2 price_xk.l2
## price_ao.l2  1.000000000  1.00000000   1.0000000    1.000000
## price_bs.l2 -2.171568881 -0.03820047   0.3284309    0.671713
## price_bl.l2 -0.082565793 -3.43720294  -0.3220372   -3.020029
## price_xk.l2  0.001679455 -1.43705383  -0.4687727    3.030970
## 
## Weights W:
## (This is the loading matrix)
## 
##            price_ao.l2 price_bs.l2 price_bl.l2 price_xk.l2
## price_ao.d  -0.2865403   0.1545090 -0.89090486 -0.05554158
## price_bs.d   0.5545453   0.1062251 -0.42850130 -0.02340507
## price_bl.d  -0.1552394   0.2934133 -0.24882139  0.07653035
## price_xk.d  -0.1105804   0.2105927  0.07918119 -0.19948317
cajorls(vecm)
## $rlm
## 
## Call:
## lm(formula = substitute(form1), data = data.mat)
## 
## Coefficients:
##               price_ao.d  price_bs.d  price_bl.d  price_xk.d
## ect1           -0.28654     0.55455    -0.15524    -0.11058 
## constant      -43.66453    85.09864   -23.48540   -16.81048 
## price_ao.dl1   -0.62938     0.26263    -0.12139     0.01313 
## price_bs.dl1    0.35301    -1.14011     0.18482     0.24138 
## price_bl.dl1    0.17961     0.10683    -0.42868     0.05414 
## price_xk.dl1   -0.08488    -0.08578     0.03381    -0.50171 
## 
## 
## $beta
##                     ect1
## price_ao.l2  1.000000000
## price_bs.l2 -2.171568881
## price_bl.l2 -0.082565793
## price_xk.l2  0.001679455

Chuyển VECM thành VAR

Để chạy cứ shock, kiểm định, dự báo như trong VARS

# Để chạy cứ shock, kiểm định, dự bán như trong VARS
vec2var(vecm) ->ve2va
ve2va
## 
## Coefficient matrix of lagged endogenous variables:
## 
## A1:
##          price_ao.l1 price_bs.l1 price_bl.l1 price_xk.l1
## price_ao  0.37062419   0.3530060  0.17960840 -0.08487808
## price_bs  0.26262986  -0.1401082  0.10683293 -0.08577742
## price_bl -0.12139316   0.1848165  0.57132042  0.03380618
## price_xk  0.01312994   0.2413775  0.05413641  0.49829361
## 
## 
## A2:
##          price_ao.l2  price_bs.l2 price_bl.l2 price_xk.l2
## price_ao   0.3428355  0.269235934 -0.15594998  0.08439685
## price_bs   0.2919154 -0.064125058 -0.15261941  0.08670876
## price_bl  -0.0338462  0.152296466  0.44149704 -0.03406689
## price_xk  -0.1237103 -0.001244635 -0.04500625  0.50152068
## 
## 
## Coefficient matrix of deterministic regressor(s).
## 
##           constant
## price_ao -43.66453
## price_bs  85.09864
## price_bl -23.48540
## price_xk -16.81048

Nên chạy trên Eviews hay Stata sẽ tốt hơn.