library(tseries) # for `adf.test()`
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(dynlm) #for function `dynlm()`
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(vars) # for function `VAR()`
## Loading required package: MASS
## Loading required package: strucchange
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
library(nlWaldTest) # for the `nlWaldtest()` function
library(lmtest) #for `coeftest()` and `bptest()`.
library(broom) #for `glance(`) and `tidy()`
library(PoEdata) #for PoE4 datasets
library(PoEdata)
library(sandwich)
library(knitr) #for `kable()`
library(forecast)
data("gdp", package="PoEdata")
gdp <- ts(gdp, start=c(1970,1), end=c(2000,4), frequency=4)

ts.plot(gdp[,"usa"],gdp[,"aus"], type="l", 
        lty=c(1,2), col=c(1,2))
legend("topleft", border=NULL, legend=c("USA","AUS"), 
       lty=c(1,2), col=c(1,2))

adf.test(gdp[,"usa"])
## 
##  Augmented Dickey-Fuller Test
## 
## data:  gdp[, "usa"]
## Dickey-Fuller = -0.90827, Lag order = 4, p-value = 0.9492
## alternative hypothesis: stationary
adf.test(gdp[,"aus"])
## 
##  Augmented Dickey-Fuller Test
## 
## data:  gdp[, "aus"]
## Dickey-Fuller = -0.61238, Lag order = 4, p-value = 0.9755
## alternative hypothesis: stationary
adf.test(diff(gdp[,"usa"]))
## Warning in adf.test(diff(gdp[, "usa"])): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(gdp[, "usa"])
## Dickey-Fuller = -4.2929, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
adf.test(diff(gdp[,"aus"]))
## Warning in adf.test(diff(gdp[, "aus"])): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(gdp[, "aus"])
## Dickey-Fuller = -4.4168, Lag order = 4, p-value = 0.01
## alternative hypothesis: stationary
cint1.dyn <- dynlm(aus~usa-1, data=gdp)
kable(tidy(cint1.dyn), digits=3,
      caption="The results of the cointegration equation 'cint1.dyn'")
## Warning: Tidiers for objects of class dynlm are not maintained by the broom
## team, and are only supported through the lm tidier method. Please be cautious in
## interpreting and reporting broom output.
The results of the cointegration equation ‘cint1.dyn’
term estimate std.error statistic p.value
usa 0.985 0.002 594.787 0
ehat <- resid(cint1.dyn)
cint2.dyn <- dynlm(d(ehat)~L(ehat)-1)
summary(cint2.dyn)
## 
## Time series regression with "ts" data:
## Start = 1970(2), End = 2000(4)
## 
## Call:
## dynlm(formula = d(ehat) ~ L(ehat) - 1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.48488 -0.33704 -0.00385  0.46559  1.35065 
## 
## Coefficients:
##         Estimate Std. Error t value Pr(>|t|)   
## L(ehat) -0.12794    0.04428  -2.889  0.00457 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5985 on 122 degrees of freedom
## Multiple R-squared:  0.06405,    Adjusted R-squared:  0.05637 
## F-statistic: 8.348 on 1 and 122 DF,  p-value: 0.00457
vecaus<- dynlm(d(aus)~L(ehat), data=gdp)
vecusa <- dynlm(d(usa)~L(ehat), data=gdp)
tidy(vecaus)
## Warning: Tidiers for objects of class dynlm are not maintained by the broom
## team, and are only supported through the lm tidier method. Please be cautious in
## interpreting and reporting broom output.
## # A tibble: 2 x 5
##   term        estimate std.error statistic  p.value
##   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)   0.492     0.0579      8.49 6.12e-14
## 2 L(ehat)      -0.0987    0.0475     -2.08 3.99e- 2