## Example 1. Comparing the Engle -- Granger procedure carried oud by two procedures.
## ECM.EngleGran() makes easier the fitting process.
## Here, we will use:
## A) The R code 4.2, in Chapter 4, Pfaff (2011).
## This code 1) generates artificial data and 2) fits an ECM, following
## the Engle --Granger procedure.
## B) The ECM.EngleGran() family function to fit the same model assuming
## bivariate normal innovations.
## The downside in the R code 4.2 is the assumption of no--correlation among
## the errors. These are generated indenpendently.
## A)
## STEP 1. Set up the data (R code as in Pfaff (2011)).
nn <- 100
set.seed(123456)
e1 <- rnorm(nn) # Independent of e2
e2 <- rnorm(nn)
y1 <- cumsum(e1)
y2 <- 0.6 * y1 + e2
lr.reg <- lm(y2 ~ y1)
error <- residuals(lr.reg)
error.lagged <- error[-c(nn - 1, nn)]
dy1 <- diff(y1)
dy2 <- diff(y2)
diff.dat <- data.frame(embed(cbind(dy1, dy2), 2))
colnames(diff.dat) <- c('dy1', 'dy2', 'dy1.1', 'dy2.1')
## STEP 2. Fit the ECM model, using lm(), R code as in Pfaff (2011).
ecm.reg <- lm(dy2 ~ error.lagged + dy1.1 + dy2.1, data = diff.dat)
summary(ecm.reg)
## B) Now, using ECM.EngleGran() and VGLMs, the steps at A) can be skipped.
## Enter the I(1)--variables in the response vector only, putting down the
## the dependent variable from the I(1) set, i.e. y2, in the second column.
coint.data <- data.frame(y1 = y1, y2 = y2)
fit.ECM <- vglm(cbind(y1, y2) ~ 1, ECM.EngleGran, data = coint.data, trace = TRUE)
## Check coefficients ##
coef(fit.ECM, matrix = TRUE) ## Compare 'Diff2' with summary(ecm.reg)
coef(summary(ecm.reg))
head(depvar(fit.ECM)) # The estimated differences (first order)
vcov(fit.ECM)
constraints(fit.ECM, matrix = TRUE)
if (FALSE) {
### Example 2. Here, we compare ECM.EngleGran() from VGAMextra with VECM() from
## package "tsDyn" when fitting an ECM(1, 1). We will make use of
## the argument 'ordtsDyn' so that the outcomes can be compared.
library("tsDyn") # Need to be installed first.
fit.tsDyn1 <- with(coint.data, VECM(cbind(y2, y1), lag = 1, estim = "2OLS")) # MODEL 1
summary(fit.tsDyn1)
### Fit same model using ECM.EngleGran(). NOTE: Set ordtsDyn = 1 !! # MODEL 2
fit.ECM.2 <- vglm(cbind(y1, y2) ~ 1, ECM.EngleGran(ecm.order = c(1, 1),
resids.pattern = "neither", ordtsDyn = 1),
data = coint.data, trace = TRUE)
coef.ECM.2 <- coef(fit.ECM.2, matrix = TRUE)
fit.tsDyn1$coefficients ## From pakage 'tsDyn'.
t(coef.ECM.2[, 1:2][c(2, 1, 4, 3), ][, 2:1]) ## FROM VGAMextra
### Example 3. An ECM(2, 2), with residuals estimated by OLS, with NO intercept
### and NO trend term. The data set is 'zeroyld', from package tsDyn.
### ECM.EngleGran() and with VECM() willbe compared again.
data(zeroyld, package = "tsDyn")
# Fit a VECM with Engle-Granger 2OLS estimator:
vecm.eg <- VECM(zeroyld, lag=2, estim = "2OLS")
summary(vecm.eg)
# For the same data, fit a VECM with ECM.EngleGran(), from VGAMextra.
# Set ordtsDyn = 1 for compatibility!
fit.ECM.3 <- vglm(cbind(long.run, short.run) ~ 1, ECM.EngleGran(ecm.order = c(2, 2),
resids.pattern = "neither", ordtsDyn = 1),
data = zeroyld, trace = TRUE)
coef.ECM.3 <- coef(fit.ECM.3, matrix = TRUE)
#### Compare results
vecm.eg$coefficients # From tsDyn
t(coef.ECM.3[, 1:2][c(2, 1, 5, 3, 6, 4 ),][, 2:1]) # FROM VGAMextra
}
Run the code above in your browser using DataLab