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("fred", package="PoEdata")
fred <- ts(fred, start=c(1960,1),end=c(2009,4),frequency=4)
ts.plot(fred[,"c"],fred[,"y"], type="l", 
        lty=c(1,2), col=c(1,2))
legend("topleft", border=NULL, legend=c("c","y"), 
       lty=c(1,2), col=c(1,2))

Acf(fred[,"c"])

Acf(fred[,"y"])

adf.test(fred[,"c"])
## 
##  Augmented Dickey-Fuller Test
## 
## data:  fred[, "c"]
## Dickey-Fuller = -2.6202, Lag order = 5, p-value = 0.3163
## alternative hypothesis: stationary
adf.test(fred[,"y"])
## 
##  Augmented Dickey-Fuller Test
## 
## data:  fred[, "y"]
## Dickey-Fuller = -2.2905, Lag order = 5, p-value = 0.4544
## alternative hypothesis: stationary
adf.test(diff(fred[,"c"]))
## Warning in adf.test(diff(fred[, "c"])): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(fred[, "c"])
## Dickey-Fuller = -4.713, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
adf.test(diff(fred[,"y"]))
## Warning in adf.test(diff(fred[, "y"])): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  diff(fred[, "y"])
## Dickey-Fuller = -5.7751, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
cointcy <- dynlm(c~y, data=fred)
ehat <- resid(cointcy)
adf.test(ehat)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  ehat
## Dickey-Fuller = -2.5617, Lag order = 5, p-value = 0.3409
## alternative hypothesis: stationary
library(vars)
Dc <- diff(fred[,"c"])
Dy <- diff(fred[,"y"])
varmat <- as.matrix(cbind(Dc,Dy))
varfit <- VAR(varmat) # `VAR()` from package `vars`
summary(varfit)
## 
## VAR Estimation Results:
## ========================= 
## Endogenous variables: Dc, Dy 
## Deterministic variables: const 
## Sample size: 198 
## Log Likelihood: 1400.444 
## Roots of the characteristic polynomial:
## 0.3441 0.3425
## Call:
## VAR(y = varmat)
## 
## 
## Estimation results for equation Dc: 
## =================================== 
## Dc = Dc.l1 + Dy.l1 + const 
## 
##        Estimate Std. Error t value Pr(>|t|)    
## Dc.l1 0.2156068  0.0747486   2.884  0.00436 ** 
## Dy.l1 0.1493798  0.0577343   2.587  0.01040 *  
## const 0.0052776  0.0007573   6.969 4.81e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.006575 on 195 degrees of freedom
## Multiple R-Squared: 0.1205,  Adjusted R-squared: 0.1115 
## F-statistic: 13.36 on 2 and 195 DF,  p-value: 3.661e-06 
## 
## 
## Estimation results for equation Dy: 
## =================================== 
## Dy = Dc.l1 + Dy.l1 + const 
## 
##         Estimate Std. Error t value Pr(>|t|)    
## Dc.l1  0.4754276  0.0973264   4.885 2.15e-06 ***
## Dy.l1 -0.2171679  0.0751730  -2.889   0.0043 ** 
## const  0.0060367  0.0009861   6.122 4.99e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## 
## Residual standard error: 0.008562 on 195 degrees of freedom
## Multiple R-Squared: 0.1118,  Adjusted R-squared: 0.1027 
## F-statistic: 12.27 on 2 and 195 DF,  p-value: 9.53e-06 
## 
## 
## 
## Covariance matrix of residuals:
##           Dc        Dy
## Dc 4.324e-05 2.508e-05
## Dy 2.508e-05 7.330e-05
## 
## Correlation matrix of residuals:
##        Dc     Dy
## Dc 1.0000 0.4456
## Dy 0.4456 1.0000
impresp <- irf(varfit)
plot(impresp)

plot(fevd(varfit)) # `fevd()` is in package `vars`