1. Kết nối dữ liệu

library(readxl)
setwd("d:/DATA2021/ThuySan.VECM/data5.5.21")
solieu <-read_excel("data.ca.5.5.21.xlsx")

congthuc <-LnGDP ~ LnFEX  + LnOPEN + LnFDI

lm(data=solieu,congthuc) -> hoiquy1
summary(hoiquy1)
## 
## Call:
## lm(formula = congthuc, data = solieu)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.262300 -0.080561 -0.000358  0.062601  0.304111 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.955453   0.831754  -1.149    0.254    
## LnFEX        0.443029   0.078839   5.619 3.01e-07 ***
## LnOPEN       0.202684   0.035287   5.744 1.81e-07 ***
## LnFDI        0.003961   0.043662   0.091    0.928    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1184 on 76 degrees of freedom
## Multiple R-squared:  0.9139, Adjusted R-squared:  0.9105 
## F-statistic:   269 on 3 and 76 DF,  p-value: < 2.2e-16
library(dynlm)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
dynlm(data=solieu,congthuc) -> hoiquy2
summary(hoiquy2)
## 
## Time series regression with "numeric" data:
## Start = 1, End = 80
## 
## Call:
## dynlm(formula = congthuc, data = solieu)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.262300 -0.080561 -0.000358  0.062601  0.304111 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.955453   0.831754  -1.149    0.254    
## LnFEX        0.443029   0.078839   5.619 3.01e-07 ***
## LnOPEN       0.202684   0.035287   5.744 1.81e-07 ***
## LnFDI        0.003961   0.043662   0.091    0.928    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1184 on 76 degrees of freedom
## Multiple R-squared:  0.9139, Adjusted R-squared:  0.9105 
## F-statistic:   269 on 3 and 76 DF,  p-value: < 2.2e-16

2. Chạy lag tối ưu

library(ARDL)
## Warning: package 'ARDL' was built under R version 4.0.5
## To cite ARDL in publications use:
##   
## Kleanthis Natsiopoulos and Nickolaos Tzeremes (2021). ARDL: ARDL, ECM and Bounds-Test for Cointegration. R package version 0.1.1. https://CRAN.R-project.org/package=ARDL
chon.model <- auto_ardl(congthuc, data = solieu, max_order = 5)
## Warning: The `x` argument of `as_tibble.matrix()` must have column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
summary(chon.model)
##            Length Class      Mode   
## best_model 19     dynlm      list   
## best_order  4     -none-     numeric
## top_orders  5     data.frame list
chon.model$best_order
## [1] 4 0 5 1
chon.model$top_orders
##    LnGDP LnFEX LnOPEN LnFDI       AIC
## 1      4     0      5     1 -551.2352
## 2      4     0      5     2 -549.4331
## 3      4     1      5     1 -549.2492
## 4      4     1      5     2 -547.4349
## 5      4     2      5     2 -545.6707
## 6      5     5      5     5 -544.3025
## 7      4     2      5     3 -543.6859
## 8      4     3      5     3 -542.5112
## 9      4     3      5     4 -540.6391
## 10     4     3      4     4 -538.7661
## 11     4     4      4     4 -537.0791
## 12     3     4      4     5 -225.1021
## 13     3     5      4     5 -224.2462
## 14     3     4      3     5 -223.8054
## 15     3     4      4     4 -211.4600
## 16     3     4      3     4 -210.1093
## 17     2     4      0     1 -209.9050
## 18     2     4      1     1 -208.2009
## 19     2     4      0     2 -208.1295
## 20     3     5      4     4 -207.9789
chon.model$best_model -> best.roi
summary(chon.model$best_model)
## 
## Time series regression with "ts" data:
## Start = 6, End = 80
## 
## Call:
## dynlm::dynlm(formula = full_formula, data = data, start = start, 
##     end = end)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0095858 -0.0036292 -0.0006274  0.0041452  0.0104622 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.454655   0.141483  -3.214  0.00210 ** 
## L(LnGDP, 1)   0.060835   0.011741   5.182 2.62e-06 ***
## L(LnGDP, 2)   0.025079   0.008879   2.824  0.00639 ** 
## L(LnGDP, 3)   0.030230   0.009257   3.265  0.00179 ** 
## L(LnGDP, 4)   1.034307   0.010810  95.682  < 2e-16 ***
## LnFEX        -0.001741   0.005244  -0.332  0.74107    
## LnOPEN        0.020248   0.011355   1.783  0.07954 .  
## L(LnOPEN, 1)  0.001896   0.011182   0.170  0.86591    
## L(LnOPEN, 2) -0.035301   0.011223  -3.145  0.00256 ** 
## L(LnOPEN, 3) -0.015293   0.011311  -1.352  0.18134    
## L(LnOPEN, 4) -0.004152   0.011023  -0.377  0.70771    
## L(LnOPEN, 5) -0.024906   0.010668  -2.335  0.02286 *  
## LnFDI         0.015350   0.005956   2.577  0.01239 *  
## L(LnFDI, 1)  -0.018179   0.005758  -3.157  0.00248 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.005569 on 61 degrees of freedom
## Multiple R-squared:  0.9998, Adjusted R-squared:  0.9998 
## F-statistic: 2.511e+04 on 13 and 61 DF,  p-value: < 2.2e-16
toichon <- ardl(congthuc, data = solieu, order = c(4,1,5,1))

3. Chạy UECM

summary(uecm(toichon))
## 
## Time series regression with "ts" data:
## Start = 6, End = 80
## 
## Call:
## dynlm::dynlm(formula = full_formula, data = data, start = start, 
##     end = end)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0096012 -0.0036108 -0.0007266  0.0041288  0.0104908 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -0.457000   0.144353  -3.166 0.002430 ** 
## L(LnGDP, 1)      0.150552   0.030077   5.006 5.18e-06 ***
## L(LnFEX, 1)     -0.001549   0.005588  -0.277 0.782533    
## L(LnOPEN, 1)    -0.057620   0.012601  -4.573 2.46e-05 ***
## L(LnFDI, 1)     -0.002848   0.003090  -0.922 0.360338    
## d(L(LnGDP, 1))  -1.090282   0.024249 -44.961  < 2e-16 ***
## d(L(LnGDP, 2))  -1.065162   0.018438 -57.770  < 2e-16 ***
## d(L(LnGDP, 3))  -1.034604   0.011253 -91.939  < 2e-16 ***
## d(LnFEX)        -0.002433   0.008408  -0.289 0.773315    
## d(LnOPEN)        0.020550   0.011799   1.742 0.086698 .  
## d(L(LnOPEN, 1))  0.079667   0.012169   6.547 1.48e-08 ***
## d(L(LnOPEN, 2))  0.044343   0.011952   3.710 0.000455 ***
## d(L(LnOPEN, 3))  0.028938   0.011806   2.451 0.017170 *  
## d(L(LnOPEN, 4))  0.024989   0.010783   2.317 0.023914 *  
## d(LnFDI)         0.015419   0.006040   2.553 0.013251 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.005615 on 60 degrees of freedom
## Multiple R-squared:  0.9996, Adjusted R-squared:  0.9996 
## F-statistic: 1.181e+04 on 14 and 60 DF,  p-value: < 2.2e-16

4. Chạy RECM

summary(recm(toichon, case=2)) #short-run
## 
## Time series regression with "zooreg" data:
## Start = 6, End = 80
## 
## Call:
## dynlm::dynlm(formula = full_formula, data = data, start = start, 
##     end = end)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0096012 -0.0036108 -0.0007266  0.0041288  0.0104908 
## 
## Coefficients:
##                  Estimate Std. Error  t value Pr(>|t|)    
## d(L(LnGDP, 1))  -1.090282   0.010784 -101.097  < 2e-16 ***
## d(L(LnGDP, 2))  -1.065162   0.010999  -96.840  < 2e-16 ***
## d(L(LnGDP, 3))  -1.034604   0.008559 -120.878  < 2e-16 ***
## d(LnFEX)        -0.002433   0.007638   -0.318  0.75114    
## d(LnOPEN)        0.020550   0.010481    1.961  0.05426 .  
## d(L(LnOPEN, 1))  0.079667   0.009520    8.368 7.24e-12 ***
## d(L(LnOPEN, 2))  0.044343   0.009643    4.599 2.06e-05 ***
## d(L(LnOPEN, 3))  0.028938   0.009740    2.971  0.00418 ** 
## d(L(LnOPEN, 4))  0.024989   0.009256    2.700  0.00887 ** 
## d(LnFDI)         0.015419   0.005383    2.864  0.00565 ** 
## ect              0.150552   0.003364   44.748  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.005436 on 64 degrees of freedom
##   (0 observations deleted due to missingness)
## Multiple R-squared:  0.9996, Adjusted R-squared:  0.9996 
## F-statistic: 1.614e+04 on 11 and 64 DF,  p-value: < 2.2e-16
summary(recm(toichon, case=3)) #long-run
## 
## Time series regression with "zooreg" data:
## Start = 6, End = 80
## 
## Call:
## dynlm::dynlm(formula = full_formula, data = data, start = start, 
##     end = end)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0096012 -0.0036108 -0.0007266  0.0041288  0.0104908 
## 
## Coefficients:
##                  Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)     -0.457000   0.059422   -7.691 1.24e-10 ***
## d(L(LnGDP, 1))  -1.090282   0.016209  -67.266  < 2e-16 ***
## d(L(LnGDP, 2))  -1.065162   0.013975  -76.218  < 2e-16 ***
## d(L(LnGDP, 3))  -1.034604   0.009685 -106.830  < 2e-16 ***
## d(LnFEX)        -0.002433   0.007705   -0.316  0.75326    
## d(LnOPEN)        0.020550   0.010721    1.917  0.05979 .  
## d(L(LnOPEN, 1))  0.079667   0.010443    7.629 1.59e-10 ***
## d(L(LnOPEN, 2))  0.044343   0.010465    4.237 7.52e-05 ***
## d(L(LnOPEN, 3))  0.028938   0.010421    2.777  0.00722 ** 
## d(L(LnOPEN, 4))  0.024989   0.009771    2.558  0.01296 *  
## d(LnFDI)         0.015419   0.005435    2.837  0.00612 ** 
## ect              0.150552   0.017395    8.655 2.57e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.005479 on 63 degrees of freedom
##   (0 observations deleted due to missingness)
## Multiple R-squared:  0.9996, Adjusted R-squared:  0.9996 
## F-statistic: 1.578e+04 on 11 and 63 DF,  p-value: < 2.2e-16

5. kiểm tra mối quan hệ long-run

bounds_f_test(toichon, case=2)
## 
##  Bounds F-test (Wald) for no cointegration
## 
## data:  d(LnGDP) ~ L(LnGDP, 1) + L(LnFEX, 1) + L(LnOPEN, 1) + L(LnFDI,     1) + d(L(LnGDP, 1)) + d(L(LnGDP, 2)) + d(L(LnGDP, 3)) + d(LnFEX) +     d(LnOPEN) + d(L(LnOPEN, 1)) + d(L(LnOPEN, 2)) + d(L(LnOPEN,     3)) + d(L(LnOPEN, 4)) + d(LnFDI)
## F = 375.44, p-value = 1e-06
## alternative hypothesis: Possible cointegration
## null values:
##    k    T 
##    3 1000
bounds_f_test(toichon, case=2)$tab
##   statistic p.value
## F  375.4449   1e-06
bounds_t_test(toichon, case=3)
## 
##  Bounds t-test for no cointegration
## 
## data:  d(LnGDP) ~ L(LnGDP, 1) + L(LnFEX, 1) + L(LnOPEN, 1) + L(LnFDI,     1) + d(L(LnGDP, 1)) + d(L(LnGDP, 2)) + d(L(LnGDP, 3)) + d(LnFEX) +     d(LnOPEN) + d(L(LnOPEN, 1)) + d(L(LnOPEN, 2)) + d(L(LnOPEN,     3)) + d(L(LnOPEN, 4)) + d(LnFDI)
## t = 5.0056, p-value = 1
## alternative hypothesis: Possible cointegration
## null values:
##    k    T 
##    3 1000
bounds_t_test(toichon, case=3)$tab
##   statistic  p.value
## t  5.005631 0.999999

6. Ước lượng long-run relationship (cointegrating equation)

ce <-coint_eq(toichon, case = 2)
dulieu <-cbind(solieu,ce)
head(dulieu)
##   STT    QUY    LnGDP   LnFGDP    LnFEX    LnLAB      REER   LnOPEN    LnFDI
## 1   1 2000q1 12.32272 7.204922 19.51234 10.55482 0.9962832 22.62871 10.34393
## 2   2 2000q2 12.57529 7.457496 19.76492 10.56007 0.9997200 22.73124 10.59650
## 3   3 2000q3 12.50762 7.389821 19.69724 10.56939 1.0071360 22.71304 10.52882
## 4   4 2000q4 12.70953 7.591730 19.89915 10.55959 1.0072360 22.87906 10.73074
## 5   5 2001q1 12.39648 7.320969 19.72526 10.58206 0.9931406 22.57954 10.45481
## 6   6 2001q2 12.63004 7.554526 19.95881 10.58733 0.9989467 22.81070 10.68836
##         ce
## 1 12.09252
## 2 12.13914
## 3 12.13020
## 4 12.19963
## 5 12.07799
## 6 12.17328

7. ước lượng short-run and the long-run multipliers

multipliers(toichon, type = "sr")
##          term   estimate  std.error t.statistic      p.value
## 1 (Intercept)  3.0354942 0.49040313   6.1897937 5.908161e-08
## 2       LnFEX  0.0161587 0.03735827   0.4325335 6.669044e-01
## 3      LnOPEN -0.1364989 0.01699815  -8.0302177 4.351891e-11
## 4       LnFDI -0.1024166 0.02330366  -4.3948727 4.592822e-05
multipliers(toichon, type = "lr")
##          term   estimate  std.error t.statistic      p.value
## 1 (Intercept) 3.03549421 0.49040313   6.1897937 5.908161e-08
## 2       LnFEX 0.01029125 0.03735827   0.2754746 7.838982e-01
## 3      LnOPEN 0.38272420 0.01699815  22.5156330 5.924181e-31
## 4       LnFDI 0.01891612 0.02330366   0.8117232 4.201591e-01

Các kiểm định mô hình

Phương sai thay đổi

library(lmtest)
bptest(toichon)
## 
##  studentized Breusch-Pagan test
## 
## data:  toichon
## BP = 9.3587, df = 14, p-value = 0.8074

Tự tương quan

bgtest(toichon)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  toichon
## LM test = 8.4716, df = 1, p-value = 0.003607

Phân phối chuẩn phần dư

phandu <-residuals(toichon)
shapiro.test(phandu)
## 
##  Shapiro-Wilk normality test
## 
## data:  phandu
## W = 0.97334, p-value = 0.1143

Ví dụ

(Chạy Lag và Diff nhớ sét thời gian or remove dữ liệu rỗng)

solieu2 <-ts(solieu, star=c(2000,1),frequency = 4)
congthuc2 <-d(LnGDP) ~ L(LnFEX)  + L(LnOPEN,1) + L(LnFDI,0:3)  + d(LnFEX) + d(LnOPEN,2) + d(LnFDI)
dynlm(congthuc2, data=solieu2)
## 
## Time series regression with "ts" data:
## Start = 2000(4), End = 2019(4)
## 
## Call:
## dynlm(formula = congthuc2, data = solieu2)
## 
## Coefficients:
##    (Intercept)        L(LnFEX)    L(LnOPEN, 1)  L(LnFDI, 0:3)0  L(LnFDI, 0:3)1  
##      -1.379042        0.081588       -0.007025        0.449326       -0.487835  
## L(LnFDI, 0:3)2  L(LnFDI, 0:3)3        d(LnFEX)    d(LnOPEN, 2)        d(LnFDI)  
##       0.150117       -0.124356        0.493584        0.190099              NA