What is a VECM Model? A VECM is a special case of VAR that is used when variables are non-stationary but cointegrated.

Cointegration means variables share a long-run equilibrium even if each one individually trends over time. VECM captures both: Short-term dynamics (like VAR) Long-term equilibrium (error correction term) Error Correction Term: ΔYt=α(Yt−1−βXt−1)+short-term terms+ϵtY_t = ({t-1} - X{t-1}) + + _tΔYt​=α(Yt−1​−βXt−1​)+short-term terms+ϵt​

Set and confirm the working directory

getwd()
## [1] "C:/Users/Hp/Documents/ARDL"

Load required packages

Load the data into the R environment

data <- read.csv("C:\\Users\\Hp\\Documents\\ARDL\\ECON DEVELOPMENT DATA.csv")

Select the Variables needed

data_var <- data[, c("ECNDEV","INF", "EDU", "K", "HEA", "UNEMP")]

Do some data exploration

head(df)
##                                               
## 1 function (x, df1, df2, ncp, log = FALSE)    
## 2 {                                           
## 3     if (missing(ncp))                       
## 4         .Call(C_df, x, df1, df2, log)       
## 5     else .Call(C_dnf, x, df1, df2, ncp, log)
## 6 }
tail(df)
##                                               
## 1 function (x, df1, df2, ncp, log = FALSE)    
## 2 {                                           
## 3     if (missing(ncp))                       
## 4         .Call(C_df, x, df1, df2, log)       
## 5     else .Call(C_dnf, x, df1, df2, ncp, log)
## 6 }
glimpse(df)
## function (x, df1, df2, ncp, log = FALSE)
dim(df)
## NULL

Convert to time series object

data_var <- ts(data_var)

Select optimal lag length

lag_select <- VARselect(data_var, lag.max = 10, type = "const")
lag_select$selection
## AIC(n)  HQ(n)  SC(n) FPE(n) 
##      4      4      4      4

Perform Johansen Cointegration Test

johansen_test <- ca.jo(data_var,
                       type = "trace",   # or "eigen"
                       ecdet = "const",  # deterministic trend
                       K = 2)            # lag = p

summary(johansen_test)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 9.418956e-01 8.373230e-01 6.054274e-01 4.617373e-01 2.754849e-01
## [6] 2.063891e-01 7.771561e-16
## 
## Values of teststatistic and critical values of test:
## 
##            test 10pct   5pct   1pct
## r <= 5 |   6.47  7.52   9.24  12.97
## r <= 4 |  15.50 17.85  19.96  24.60
## r <= 3 |  32.84 32.00  34.91  41.07
## r <= 2 |  58.88 49.65  53.12  60.16
## r <= 1 | 109.73 71.86  76.07  84.45
## r = 0  | 189.40 97.18 102.14 111.01
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##               ECNDEV.l2        INF.l2        EDU.l2          K.l2        HEA.l2
## ECNDEV.l2  1.000000e+00  1.000000e+00  1.000000e+00  1.000000e+00  1.000000e+00
## INF.l2     2.384042e-02 -6.533930e-04  1.909929e-03  2.035639e-03 -3.739982e-04
## EDU.l2    -3.258790e-02  4.734721e-03  3.298484e-02 -2.230834e-02  5.325055e-03
## K.l2      -1.549129e-05 -6.649319e-07  8.761501e-07 -8.013002e-06 -2.299670e-06
## HEA.l2     7.653749e-02 -2.805510e-02 -8.155767e-02  1.247992e-01 -1.325824e-02
## UNEMP.l2  -2.726798e-01 -1.019339e-01 -3.541020e-02  1.142133e-01  1.132103e-02
## constant   1.399781e+00 -1.010100e-01 -1.382035e+00 -9.552031e-01 -6.489344e-01
##                UNEMP.l2      constant
## ECNDEV.l2  1.000000e+00  1.000000e+00
## INF.l2     3.334640e-03  3.799670e-04
## EDU.l2     1.001677e-01 -9.457907e-03
## K.l2      -6.380153e-06  2.623403e-06
## HEA.l2    -4.258175e-02  1.760955e-03
## UNEMP.l2  -2.521259e-01 -8.258689e-02
## constant  -3.997089e+00  2.409528e-01
## 
## Weights W:
## (This is the loading matrix)
## 
##              ECNDEV.l2        INF.l2        EDU.l2         K.l2        HEA.l2
## ECNDEV.d -7.121601e-04 -8.768154e-02    -0.1095208  -0.03499773    -0.1809679
## INF.d    -2.508586e+01  1.016641e+02   -27.0498918  12.79407731   -12.7552889
## EDU.d    -5.281632e-02 -9.429869e-01    -1.2112211  -1.09774652     1.2936498
## K.d      -3.540611e+03 -1.215327e+05 16982.5081788 142.50761496 19290.3602536
## HEA.d     5.880631e-02  1.161045e+00     3.7810039  -3.60644498     5.4506441
## UNEMP.d   1.380530e-01 -5.146009e-01    -0.9514587   2.24010606     5.1919656
##              UNEMP.l2      constant
## ECNDEV.d   0.01818742 -3.585833e-14
## INF.d     -1.32622944 -2.794687e-12
## EDU.d     -0.24557811  9.466956e-13
## K.d      534.89303335 -4.532365e-08
## HEA.d      0.25770717 -1.057715e-11
## UNEMP.d    0.50879884  1.894373e-12

Estimate VECM

vecm_model <- cajorls(johansen_test, r = 1)  # r = number of cointegrating equations
summary(vecm_model$rlm)
## Response ECNDEV.d :
## 
## Call:
## lm(formula = ECNDEV.d ~ ect1 + ECNDEV.dl1 + INF.dl1 + EDU.dl1 + 
##     K.dl1 + HEA.dl1 + UNEMP.dl1 - 1, data = data.mat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.044843  0.000052  0.002060  0.005358  0.009370 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## ect1       -7.122e-04  5.823e-03  -0.122    0.904
## ECNDEV.dl1 -1.121e-01  2.161e-01  -0.518    0.610
## INF.dl1     6.444e-05  2.509e-04   0.257    0.800
## EDU.dl1     4.738e-03  7.709e-03   0.615    0.545
## K.dl1       4.339e-07  6.471e-07   0.671    0.510
## HEA.dl1    -3.089e-03  6.343e-03  -0.487    0.631
## UNEMP.dl1   1.652e-03  6.151e-03   0.269    0.791
## 
## Residual standard error: 0.011 on 21 degrees of freedom
## Multiple R-squared:  0.1069, Adjusted R-squared:  -0.1908 
## F-statistic: 0.359 on 7 and 21 DF,  p-value: 0.9158
## 
## 
## Response INF.d :
## 
## Call:
## lm(formula = INF.d ~ ect1 + ECNDEV.dl1 + INF.dl1 + EDU.dl1 + 
##     K.dl1 + HEA.dl1 + UNEMP.dl1 - 1, data = data.mat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.5092 -2.7816 -0.5785  1.5357  7.8756 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## ect1       -2.509e+01  2.421e+00 -10.361 1.04e-09 ***
## ECNDEV.dl1 -5.553e+01  8.987e+01  -0.618   0.5433    
## INF.dl1    -5.559e-01  1.043e-01  -5.329 2.77e-05 ***
## EDU.dl1     7.391e+00  3.206e+00   2.306   0.0314 *  
## K.dl1      -1.345e-03  2.691e-04  -4.997 6.03e-05 ***
## HEA.dl1     1.608e+00  2.637e+00   0.610   0.5487    
## UNEMP.dl1   5.367e+00  2.557e+00   2.098   0.0481 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.573 on 21 degrees of freedom
## Multiple R-squared:  0.8382, Adjusted R-squared:  0.7843 
## F-statistic: 15.54 on 7 and 21 DF,  p-value: 5.228e-07
## 
## 
## Response EDU.d :
## 
## Call:
## lm(formula = EDU.d ~ ect1 + ECNDEV.dl1 + INF.dl1 + EDU.dl1 + 
##     K.dl1 + HEA.dl1 + UNEMP.dl1 - 1, data = data.mat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.36276 -0.05292 -0.01031  0.09743  0.26650 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## ect1       -5.282e-02  7.871e-02  -0.671   0.5095    
## ECNDEV.dl1 -4.330e-01  2.922e+00  -0.148   0.8836    
## INF.dl1    -4.994e-03  3.391e-03  -1.473   0.1556    
## EDU.dl1     9.419e-01  1.042e-01   9.039  1.1e-08 ***
## K.dl1       4.821e-06  8.747e-06   0.551   0.5873    
## HEA.dl1    -1.199e-02  8.574e-02  -0.140   0.8901    
## UNEMP.dl1   1.525e-01  8.314e-02   1.834   0.0809 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1487 on 21 degrees of freedom
## Multiple R-squared:  0.8666, Adjusted R-squared:  0.8221 
## F-statistic: 19.48 on 7 and 21 DF,  p-value: 7.438e-08
## 
## 
## Response K.d :
## 
## Call:
## lm(formula = K.d ~ ect1 + ECNDEV.dl1 + INF.dl1 + EDU.dl1 + K.dl1 + 
##     HEA.dl1 + UNEMP.dl1 - 1, data = data.mat)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -6750  -1080   -143    978  12012 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## ect1       -3540.6110  2227.6405  -1.589   0.1269  
## ECNDEV.dl1 22634.4567 82685.9137   0.274   0.7870  
## INF.dl1      -74.7834    95.9723  -0.779   0.4445  
## EDU.dl1     3776.1077  2949.3676   1.280   0.2144  
## K.dl1          0.5227     0.2476   2.112   0.0469 *
## HEA.dl1     -618.6025  2426.4558  -0.255   0.8012  
## UNEMP.dl1   -954.3335  2353.0815  -0.406   0.6892  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4207 on 21 degrees of freedom
## Multiple R-squared:  0.5817, Adjusted R-squared:  0.4423 
## F-statistic: 4.172 on 7 and 21 DF,  p-value: 0.005044
## 
## 
## Response HEA.d :
## 
## Call:
## lm(formula = HEA.d ~ ect1 + ECNDEV.dl1 + INF.dl1 + EDU.dl1 + 
##     K.dl1 + HEA.dl1 + UNEMP.dl1 - 1, data = data.mat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.17514 -0.02036  0.02056  0.08065  1.08749 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## ect1        5.881e-02  2.032e-01   0.289    0.775
## ECNDEV.dl1 -5.247e+00  7.541e+00  -0.696    0.494
## INF.dl1    -8.194e-04  8.752e-03  -0.094    0.926
## EDU.dl1     3.423e-01  2.690e-01   1.273    0.217
## K.dl1      -6.151e-06  2.258e-05  -0.272    0.788
## HEA.dl1    -4.701e-02  2.213e-01  -0.212    0.834
## UNEMP.dl1   3.586e-02  2.146e-01   0.167    0.869
## 
## Residual standard error: 0.3837 on 21 degrees of freedom
## Multiple R-squared:  0.1116, Adjusted R-squared:  -0.1846 
## F-statistic: 0.3768 on 7 and 21 DF,  p-value: 0.9056
## 
## 
## Response UNEMP.d :
## 
## Call:
## lm(formula = UNEMP.d ~ ect1 + ECNDEV.dl1 + INF.dl1 + EDU.dl1 + 
##     K.dl1 + HEA.dl1 + UNEMP.dl1 - 1, data = data.mat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.81262 -0.11473 -0.00509  0.14447  0.88711 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## ect1        1.381e-01  1.688e-01   0.818   0.4227  
## ECNDEV.dl1  6.453e+00  6.266e+00   1.030   0.3148  
## INF.dl1     5.722e-03  7.273e-03   0.787   0.4403  
## EDU.dl1     1.319e-01  2.235e-01   0.590   0.5613  
## K.dl1      -3.853e-05  1.876e-05  -2.054   0.0527 .
## HEA.dl1     2.044e-02  1.839e-01   0.111   0.9125  
## UNEMP.dl1   4.369e-01  1.783e-01   2.450   0.0231 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3188 on 21 degrees of freedom
## Multiple R-squared:  0.4884, Adjusted R-squared:  0.3178 
## F-statistic: 2.863 on 7 and 21 DF,  p-value: 0.02902

Convert to VAR (for IRF, Forecast, etc.)

vecm_var <- vec2var(johansen_test, r = 1)

Impulse Response Function (IRF)

irf_result <- irf(vecm_var, n.ahead = 10, boot = TRUE)
## Warning in chol.default(SKK, pivot = TRUE): the matrix is either rank-deficient
## or not positive definite
## Warning in log(1 - lambda[x + 1]): NaNs produced
## Warning in chol.default(SKK, pivot = TRUE): the matrix is either rank-deficient
## or not positive definite
## Warning in log(1 - lambda[x + 1]): NaNs produced
## Warning in chol.default(SKK, pivot = TRUE): the matrix is either rank-deficient
## or not positive definite
## Warning in log(1 - lambda[x + 1]): NaNs produced
## Warning in chol.default(SKK, pivot = TRUE): the matrix is either rank-deficient
## or not positive definite
## Warning in log(1 - lambda[x + 1]): NaNs produced
## Warning in chol.default(SKK, pivot = TRUE): the matrix is either rank-deficient
## or not positive definite
## Warning in log(1 - lambda[x + 1]): NaNs produced
## Warning in chol.default(SKK, pivot = TRUE): the matrix is either rank-deficient
## or not positive definite
## Warning in log(1 - lambda[x + 1]): NaNs produced
## Warning in chol.default(SKK, pivot = TRUE): the matrix is either rank-deficient
## or not positive definite
## Warning in log(1 - lambda[x + 1]): NaNs produced
plot(irf_result)

Forecast

forecast_vecm <- predict(vecm_var, n.ahead = 10)

plot(forecast_vecm)