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 2
#Obtain quarterly time series for Disposable Personal Income FRED/DPI and for Personal Consumption Expenditures FRED/PCEC.
#(a) Let y1,t = log P CEt and y2,t = log P DIt. Test these log transformed time series and their first differences for unit root to verify that y1,t and y2,t are I(1).
#(b) The disposable income hypothesis suggest that PCEt = φDPIt where φ is the marginal propensity to consume. Consumption and disposable income should thus be growing at the same rate, and log P CEt −log P DIt −log φ should be I(0). Perform the trace and max eigenvalue tests for cointegration of y1,t and y2,t. Interpret the results.
#(c) Perform the test for the presence of a restricted constant rather than unrestricted constant, interpret the result. If necessary rerun the cointegration test from (b).
#(d) Estimate the unrestricted VEC model, examine significance of variables in the two equations.
#(e) Perform following three tests: (1) β2 = −1, (2) α2 = 0, (3) joint hypothesis β2 = −1 and α2 = 0.
#Interpret the results.
#(f) Convert the VEC model into a VAR model in levels. Create and plot eight quarter ahead forecast.


#a)

D<-Quandl("FRED/DPI",type="zoo")
P<-Quandl("FRED/PCEC",type="zoo")
lD=log(D)
lP=log(P)

a<-cbind(lD,lP)

plot(a,xlab="year", main="1960-Q1-2016-Q1")

dlD<-diff(lD)
dlP<-diff(lP)

par(mfrow=c(1,2))

plot(dlD,xlab = "year",main="1960-2016")

plot(dlP,xlab = "year",main="1960-2016")

#the Unit-Root tests
summary( ur.ers(lD, 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: 83.7733 
## 
## Critical values of P-test are:
##                 1pct 5pct 10pct
## critical values 3.96 5.62  6.89
summary( ur.ers(dlD, 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: 2.2919 
## 
## Critical values of P-test are:
##                 1pct 5pct 10pct
## critical values 3.96 5.62  6.89
summary( ur.ers(lP, 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: 63.1974 
## 
## Critical values of P-test are:
##                 1pct 5pct 10pct
## critical values 3.96 5.62  6.89
summary( ur.ers(dlP, 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: 1.7594 
## 
## 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
DP <- ca.jo(a, ecdet="trend", type="trace", K=2, spec="transitory")

summary(DP)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: trace statistic , with linear trend in cointegration 
## 
## Eigenvalues (lambda):
## [1] 1.169941e-01 3.065947e-02 9.504492e-19
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  8.56 10.49 12.25 16.26
## r = 0  | 42.78 22.76 25.32 30.45
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##                  lD.l1        lP.l1    trend.l1
## lD.l1     1.0000000000  1.000000000  1.00000000
## lP.l1    -0.9467561512 -1.120563179 -1.80151062
## trend.l1 -0.0008356929  0.003153558  0.01371773
## 
## Weights W:
## (This is the loading matrix)
## 
##           lD.l1        lP.l1     trend.l1
## lD.d 0.01960847 -0.024034218 3.575926e-15
## lP.d 0.12974403 -0.007937361 7.403759e-15
DP <- ca.jo(a, ecdet="trend", type="eigen", K=2, spec="transitory")

summary(DP)
## 
## ###################### 
## # Johansen-Procedure # 
## ###################### 
## 
## Test type: maximal eigenvalue statistic (lambda max) , with linear trend in cointegration 
## 
## Eigenvalues (lambda):
## [1] 1.169941e-01 3.065947e-02 9.504492e-19
## 
## Values of teststatistic and critical values of test:
## 
##           test 10pct  5pct  1pct
## r <= 1 |  8.56 10.49 12.25 16.26
## r = 0  | 34.22 16.85 18.96 23.65
## 
## Eigenvectors, normalised to first column:
## (These are the cointegration relations)
## 
##                  lD.l1        lP.l1    trend.l1
## lD.l1     1.0000000000  1.000000000  1.00000000
## lP.l1    -0.9467561512 -1.120563179 -1.80151062
## trend.l1 -0.0008356929  0.003153558  0.01371773
## 
## Weights W:
## (This is the loading matrix)
## 
##           lD.l1        lP.l1     trend.l1
## lD.d 0.01960847 -0.024034218 3.575926e-15
## lP.d 0.12974403 -0.007937361 7.403759e-15
#Part C

lttest(DP, 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            9.9       0
#Part D


DP.VEC <- cajorls(DP, r=1)
DP.VEC
## $rlm
## 
## Call:
## lm(formula = substitute(form1), data = data.mat)
## 
## Coefficients:
##           lD.d       lP.d     
## ect1       0.019608   0.129744
## constant   0.001245  -0.039420
## lD.dl1    -0.029967   0.175551
## lP.dl1     0.461941   0.042985
## 
## 
## $beta
##                   ect1
## lD.l1     1.0000000000
## lP.l1    -0.9467561512
## trend.l1 -0.0008356929
summary(DP.VEC$rlm)
## Response lD.d :
## 
## Call:
## lm(formula = lD.d ~ ect1 + constant + lD.dl1 + lP.dl1 - 1, data = data.mat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.051913 -0.004746 -0.000611  0.004612  0.058281 
## 
## Coefficients:
##           Estimate Std. Error t value Pr(>|t|)    
## ect1      0.019608   0.027290   0.719    0.473    
## constant  0.001245   0.010533   0.118    0.906    
## lD.dl1   -0.029967   0.067041  -0.447    0.655    
## lP.dl1    0.461941   0.071736   6.439 5.44e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.01011 on 271 degrees of freedom
## Multiple R-squared:  0.7314, Adjusted R-squared:  0.7274 
## F-statistic: 184.5 on 4 and 271 DF,  p-value: < 2.2e-16
## 
## 
## Response lP.d :
## 
## Call:
## lm(formula = lP.d ~ ect1 + constant + lD.dl1 + lP.dl1 - 1, data = data.mat)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.036571 -0.004066 -0.000287  0.004549  0.059174 
## 
## Coefficients:
##           Estimate Std. Error t value Pr(>|t|)    
## ect1      0.129744   0.023428   5.538 7.23e-08 ***
## constant -0.039420   0.009042  -4.360 1.85e-05 ***
## lD.dl1    0.175551   0.057555   3.050  0.00251 ** 
## lP.dl1    0.042985   0.061585   0.698  0.48579    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.008681 on 271 degrees of freedom
## Multiple R-squared:  0.7853, Adjusted R-squared:  0.7821 
## F-statistic: 247.8 on 4 and 271 DF,  p-value: < 2.2e-16
#Part E



rest.betta  <- matrix(c(0,-1,0,0,0,1), c(3,2))

r.rb1 <- blrtest(DP, 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.0385 0.0024
## 
## The value of the likelihood ratio test statistic:
## 23.41 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]
## lD.d  NaN  NaN
## lP.d  NaN  NaN
#Part F

DP.VAR <- vec2var(DP, r=1)
DP.VAR.fcst <- predict(DP.VAR, n.ahead=8)

par( mar=c(3,3,1,1), cex=0.75)

plot(DP.VAR.fcst)