library("Quandl")
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library("vars")
## Loading required package: MASS
## Loading required package: strucchange
## Loading required package: sandwich
## Loading required package: urca
## Loading required package: lmtest
library("gdata")
## gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
## 
## gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
## 
## Attaching package: 'gdata'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following object is masked from 'package:stats':
## 
##     nobs
## The following object is masked from 'package:utils':
## 
##     object.size
library("stargazer")
## 
## Please cite as:
##  Hlavac, Marek (2015). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2. http://CRAN.R-project.org/package=stargazer
library("tseries")
library("urca")
library(zoo)
Quandl.auth(" 3HQPzReHC1WfzuSG41gy")
## Warning: 'Quandl.auth' is deprecated.
## Use 'Quandl.api_key' instead.
## See help("Deprecated")
#Problem 1
#Obtain monthly data for federal funds rate FRED/FEDFUNDS and for Moody’s AAA rated corporate bonds FED/RIMLPAAAR_N_M. Monetary transmission mechanism assumes that changes in Fed’s interest rate lead to changes in the interest rates at which the firms can borrow, which implies that these two time series should be cointegrated.
#(a) Plot the two time series, in the same graph. Test both series and their first differences for unit root (e.g. using Elliott, Rothenberg and Stock test, or Zivot and Andrews’ test) to verify that they are I(1).
#(b) Perform trace and max eigenvalue tests to determine whether the two series are cointegrated. Interpret
#the results.
#(c) Perform the test for the presence of a restricted constant rather than unrestricted constant in the model;
#based on the result of the test rerun the cointegration tests you performed in (b) if necessary.
#(d) Test the restriction β2 = −1, explain what the results of the test suggest.
#(e) Create a plot comparing the error term β′yt, showing the deviations from long run equilibrium, in the case where β is unrestricted and the case with restriction β2 = −1. Also add the mean values E(β′yt) into the graph.
#a)
f<-Quandl("FRED/FEDFUNDS",type="zoo")
m<-Quandl("FED/RIMLPAAAR_N_M",type="zoo")
a<-cbind(f,m)
A <-window(a,start="Jan 1960", end = "Jan 2016")
plot(A, xlab="Years", ylab="1960-2016")

df <- diff(f)
dm <- diff(m)
da <- cbind(df,dm)
A2 <- window(da, start="Jan 1960", end= "Jan 2016")
plot(A2, xlab="Years", ylab="1960-2016")

# Unit Root Tests-levels
summary( ur.ers(f, type="P-test", lag.max=8, model="trend") )
## 
## ############################################### 
## # Elliot, Rothenberg and Stock Unit Root Test # 
## ############################################### 
## 
## Test of type P-test 
## detrending of series with intercept and trend 
## 
## Value of test-statistic is: 11.765 
## 
## Critical values of P-test are:
##                 1pct 5pct 10pct
## critical values 3.96 5.62  6.89
summary( ur.ers(df, type="P-test", lag.max=8, model="trend") )
## 
## ############################################### 
## # Elliot, Rothenberg and Stock Unit Root Test # 
## ############################################### 
## 
## Test of type P-test 
## detrending of series with intercept and trend 
## 
## Value of test-statistic is: 0.4538 
## 
## Critical values of P-test are:
##                 1pct 5pct 10pct
## critical values 3.96 5.62  6.89
summary( ur.ers(m, type="P-test", lag.max=8, model="trend") )
## 
## ############################################### 
## # Elliot, Rothenberg and Stock Unit Root Test # 
## ############################################### 
## 
## Test of type P-test 
## detrending of series with intercept and trend 
## 
## Value of test-statistic is: 27.783 
## 
## Critical values of P-test are:
##                 1pct 5pct 10pct
## critical values 3.96 5.62  6.89
summary( ur.ers(dm, type="P-test", lag.max=8, model="trend") )
## 
## ############################################### 
## # Elliot, Rothenberg and Stock Unit Root Test # 
## ############################################### 
## 
## Test of type P-test 
## detrending of series with intercept and trend 
## 
## Value of test-statistic is: 0.1118 
## 
## Critical values of P-test are:
##                 1pct 5pct 10pct
## critical values 3.96 5.62  6.89
# both variables are non-stationary in levels. but the first differences of the variables are stationary.
#B)trace Test
fm <- ca.jo(A, ecdet="const", type="trace", K=2, spec="transitory")
summary(fm)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 5.918327e-02 5.939614e-03 1.138412e-18
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  4.00  7.52  9.24 12.97
## r = 0  | 44.93 17.85 19.96 24.60
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##               f.l1      m.l1   constant
## f.l1      1.000000   1.00000    1.00000
## m.l1     -1.208595 -73.82892    3.03463
## constant  3.582429 519.39052 -171.64079
## 
## Weights W:
## (This is the loading matrix)
## 
##            f.l1         m.l1      constant
## f.d -0.03729059 1.570362e-04  1.616259e-19
## m.d  0.01701971 6.886164e-05 -1.067351e-19
# max eigenvalue test
fm <- ca.jo(A, ecdet="const", type="eigen", K=2, spec="transitory")
summary(fm)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: maximal eigenvalue statistic (lambda max) , without linear trend and constant in cointegration 
## 
## Eigenvalues (lambda):
## [1] 5.918327e-02 5.939614e-03 1.138412e-18
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  4.00  7.52  9.24 12.97
## r = 0  | 40.94 13.75 15.67 20.20
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##               f.l1      m.l1   constant
## f.l1      1.000000   1.00000    1.00000
## m.l1     -1.208595 -73.82892    3.03463
## constant  3.582429 519.39052 -171.64079
## 
## Weights W:
## (This is the loading matrix)
## 
##            f.l1         m.l1      constant
## f.d -0.03729059 1.570362e-04  1.616259e-19
## m.d  0.01701971 6.886164e-05 -1.067351e-19
# C)
lttest(fm, r=1)
## LR-test for no linear trend
## 
## H0: H*2(r<=1)
## H1: H2(r<=1)
## 
## Test statistic is distributed as chi-square
## with 1 degress of freedom
##         test statistic p-value
## LR test           0.02    0.88
#d)
rest.betta <- matrix(c(0,-1,0,   0,0,1), c(3,2))
r.rb1 <- blrtest(fm, H=rest.betta, r=1)
summary(r.rb1)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Estimation and testing under linear restrictions on beta 
## 
## The VECM has been estimated subject to: 
## beta=H*phi and/or alpha=A*psi
## 
##      [,1] [,2]
## [1,]    0    0
## [2,]   -1    0
## [3,]    0    1
## 
## Eigenvalues of restricted VAR (lambda):
## [1] 0.0059 0.0000
## 
## The value of the likelihood ratio test statistic:
## 36.93 distributed as chi square with 1 df.
## The p-value of the test statistic is: 0 
## 
## Eigenvectors, normalised to first column
## of the restricted VAR:
## 
##      [,1] [,2]
## [1,]  NaN  NaN
## [2,] -Inf -Inf
## [3,]  Inf  Inf
## 
## Weights W of the restricted VAR:
## 
##     [,1] [,2]
## f.d  NaN  NaN
## m.d  NaN  NaN