Autocorrelation (Tự tương quan): Kiểm tra và khắc phục

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")

congthuc1 <-LnGDP ~ LnFEX + LnLAB + REER + LnOPEN + LnFDI
congthuc2 <-LnFGDP ~ LnFEX + LnLAB + REER + LnOPEN + LnFDI
lm(data=solieu,congthuc1) -> hoiquy1
lm(data=solieu,congthuc2) -> hoiquy2
summary(hoiquy1)
## 
## Call:
## lm(formula = congthuc1, data = solieu)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.210940 -0.058072  0.003733  0.039472  0.269841 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 23.35330    4.89950   4.766 9.14e-06 ***
## LnFEX        0.40039    0.06716   5.962 7.86e-08 ***
## LnLAB       -2.96920    0.63490  -4.677 1.28e-05 ***
## REER        -1.00061    0.53801  -1.860  0.06688 .  
## LnOPEN       0.53708    0.08649   6.210 2.81e-08 ***
## LnFDI        0.13661    0.04067   3.359  0.00124 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09556 on 74 degrees of freedom
## Multiple R-squared:  0.9455, Adjusted R-squared:  0.9418 
## F-statistic: 256.6 on 5 and 74 DF,  p-value: < 2.2e-16
summary(hoiquy2)
## 
## Call:
## lm(formula = congthuc2, data = solieu)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.8529 -0.1832  0.0617  0.2316  1.0591 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -0.3754    21.8932  -0.017 0.986364    
## LnFEX        -0.5312     0.3001  -1.770 0.080796 .  
## LnLAB        -1.4912     2.8370  -0.526 0.600712    
## REER         -2.4703     2.4041  -1.028 0.307505    
## LnOPEN        1.3723     0.3865   3.551 0.000672 ***
## LnFDI         0.4846     0.1817   2.667 0.009406 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.427 on 74 degrees of freedom
## Multiple R-squared:   0.87,  Adjusted R-squared:  0.8612 
## F-statistic: 99.03 on 5 and 74 DF,  p-value: < 2.2e-16

A Kiểm tra lỗi tự tương quan

1. Durbin - Waston test

library(dynlm)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
hoiquy3 <-dynlm(data=solieu,congthuc1)
summary(hoiquy3)
## 
## Time series regression with "numeric" data:
## Start = 1, End = 80
## 
## Call:
## dynlm(formula = congthuc1, data = solieu)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.210940 -0.058072  0.003733  0.039472  0.269841 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 23.35330    4.89950   4.766 9.14e-06 ***
## LnFEX        0.40039    0.06716   5.962 7.86e-08 ***
## LnLAB       -2.96920    0.63490  -4.677 1.28e-05 ***
## REER        -1.00061    0.53801  -1.860  0.06688 .  
## LnOPEN       0.53708    0.08649   6.210 2.81e-08 ***
## LnFDI        0.13661    0.04067   3.359  0.00124 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09556 on 74 degrees of freedom
## Multiple R-squared:  0.9455, Adjusted R-squared:  0.9418 
## F-statistic: 256.6 on 5 and 74 DF,  p-value: < 2.2e-16
library(AER)
## Loading required package: car
## Loading required package: carData
## Loading required package: lmtest
## Loading required package: sandwich
## Loading required package: survival
durbinWatsonTest(hoiquy3)
##  lag Autocorrelation D-W Statistic p-value
##    1       0.2013972      1.481443   0.008
##  Alternative hypothesis: rho != 0
durbinWatsonTest(hoiquy1)
##  lag Autocorrelation D-W Statistic p-value
##    1       0.2013972      1.481443   0.004
##  Alternative hypothesis: rho != 0
durbinWatsonTest(hoiquy3,max.lag = 4)
##  lag Autocorrelation D-W Statistic p-value
##    1       0.2013972     1.4814433   0.004
##    2       0.2333388     1.4167328   0.000
##    3       0.0380467     1.8058170   0.372
##    4       0.5300794     0.8149223   0.000
##  Alternative hypothesis: rho[lag] != 0
library(lmtest)
dwtest(hoiquy3)
## 
##  Durbin-Watson test
## 
## data:  hoiquy3
## DW = 1.4814, p-value = 0.00283
## alternative hypothesis: true autocorrelation is greater than 0
dwtest(hoiquy1)
## 
##  Durbin-Watson test
## 
## data:  hoiquy1
## DW = 1.4814, p-value = 0.00283
## alternative hypothesis: true autocorrelation is greater than 0

2. Breusch Godfrey Test

library(lmtest)
bgtest(hoiquy1)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  hoiquy1
## LM test = 5.0263, df = 1, p-value = 0.02497
bgtest(hoiquy3)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  hoiquy3
## LM test = 5.0263, df = 1, p-value = 0.02497

3. Berenblutt-Webb test

#Chưa kiếm được gói chạy

#B Khắc phục ## 1. White HC Robust Standard Errors

library(sandwich)
coeftest(hoiquy1, vcov = vcovHC(hoiquy1, type = "HC1"))
## 
## t test of coefficients:
## 
##              Estimate Std. Error t value  Pr(>|t|)    
## (Intercept) 23.353300   3.713228  6.2892 2.013e-08 ***
## LnFEX        0.400395   0.075047  5.3352 1.001e-06 ***
## LnLAB       -2.969203   0.476508 -6.2312 2.567e-08 ***
## REER        -1.000614   0.500219 -2.0004  0.049132 *  
## LnOPEN       0.537083   0.067648  7.9394 1.686e-11 ***
## LnFDI        0.136611   0.046514  2.9370  0.004416 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2. Newey West HAC Robust Standard Errors

coeftest(hoiquy1, vcov = NeweyWest(hoiquy1))
## 
## t test of coefficients:
## 
##              Estimate Std. Error t value  Pr(>|t|)    
## (Intercept) 23.353300   3.180194  7.3434 2.240e-10 ***
## LnFEX        0.400395   0.086639  4.6214 1.579e-05 ***
## LnLAB       -2.969203   0.447932 -6.6287 4.800e-09 ***
## REER        -1.000614   0.707773 -1.4137 0.1616286    
## LnOPEN       0.537083   0.068042  7.8935 2.059e-11 ***
## LnFDI        0.136611   0.036895  3.7026 0.0004083 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3.Phương pháp Cochrane Orcutt

library(orcutt)
## Warning: package 'orcutt' was built under R version 4.0.5
cochrane.orcutt(hoiquy1) -> khacphuc3
khacphuc3
## Cochrane-orcutt estimation for first order autocorrelation 
##  
## Call:
## lm(formula = congthuc1, data = solieu)
## 
##  number of interaction: 20
##  rho 0.927267
## 
## Durbin-Watson statistic 
## (original):    1.48144 , p-value: 2.83e-03
## (transformed): 2.33073 , p-value: 9.337e-01
##  
##  coefficients: 
## (Intercept)       LnFEX       LnLAB        REER      LnOPEN       LnFDI 
##    8.417480    0.339173   -1.504980    0.898420    0.305399    0.495587
summary(khacphuc3)
## Call:
## lm(formula = congthuc1, data = solieu)
## 
##              Estimate Std. Error t value  Pr(>|t|)    
## (Intercept)  8.417480   7.778457   1.082   0.28275    
## LnFEX        0.339173   0.081144   4.180 7.998e-05 ***
## LnLAB       -1.504980   0.692349  -2.174   0.03297 *  
## REER         0.898420   1.195323   0.752   0.45470    
## LnOPEN       0.305399   0.101249   3.016   0.00352 ** 
## LnFDI        0.495587   0.060966   8.129 8.061e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0825 on 73 degrees of freedom
## Multiple R-squared:  0.9024 ,  Adjusted R-squared:  0.8957
## F-statistic: 135 on 5 and 73 DF,  p-value: < 1.94e-35
## 
## Durbin-Watson statistic 
## (original):    1.48144 , p-value: 2.83e-03
## (transformed): 2.33073 , p-value: 9.337e-01

4. Phương pháp Prais Winsten

library(prais)
## Warning: package 'prais' was built under R version 4.0.5
prais_winsten(hoiquy1, data = solieu) -> khacphuc4
## Iteration 0: rho = 0
## Iteration 1: rho = 0.2257
## Iteration 2: rho = 0.3798
## Iteration 3: rho = 0.493
## Iteration 4: rho = 0.5885
## Iteration 5: rho = 0.6782
## Iteration 6: rho = 0.7656
## Iteration 7: rho = 0.8448
## Iteration 8: rho = 0.9006
## Iteration 9: rho = 0.9283
## Iteration 10: rho = 0.9399
## Iteration 11: rho = 0.9449
## Iteration 12: rho = 0.9472
## Iteration 13: rho = 0.9482
## Iteration 14: rho = 0.9487
## Iteration 15: rho = 0.9489
## Iteration 16: rho = 0.949
## Iteration 17: rho = 0.9491
## Iteration 18: rho = 0.9491
## Iteration 19: rho = 0.9491
## Iteration 20: rho = 0.9491
## Iteration 21: rho = 0.9491
## Iteration 22: rho = 0.9491
## Iteration 23: rho = 0.9491
khacphuc4
## 
## Call:
## prais_winsten(formula = hoiquy1, data = solieu)
## 
## Coefficients:
## (Intercept)        LnFEX        LnLAB         REER       LnOPEN        LnFDI  
##     11.7417       0.3395      -1.7494       1.0689       0.2693       0.5035  
## 
## AR(1) coefficient rho: 0.9491
summary(khacphuc4)
## 
## Call:
## prais_winsten(formula = hoiquy1, data = solieu)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.41998 -0.22901 -0.14571  0.03467  0.39260 
## 
## AR(1) coefficient rho after 23 Iterations: 0.9491
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 11.74169    6.38098   1.840  0.06976 .  
## LnFEX        0.33952    0.08018   4.235 6.50e-05 ***
## LnLAB       -1.74943    0.60317  -2.900  0.00490 ** 
## REER         1.06895    1.19783   0.892  0.37507    
## LnOPEN       0.26928    0.09718   2.771  0.00706 ** 
## LnFDI        0.50354    0.06043   8.333 3.03e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.08211 on 74 degrees of freedom
## Multiple R-squared:  0.9672, Adjusted R-squared:  0.965 
## F-statistic:   437 on 5 and 74 DF,  p-value: < 2.2e-16
## 
## Durbin-Watson statistic (original): 1.481 
## Durbin-Watson statistic (transformed): 2.336

Dùng cách 3 nó đã có sẵn pvalue không cần phải tra pvalue trong bảng thống kê t