1/43
Looks like no tags are added yet.
[consandip] Clear the workspace
rm(list=ls())
[consandip] Set the working directory
setwd("…")
[consandip] Read the data file
mydata<-read.table(file="consandip.txt",header=T)
[consandip] Use variable names directly
attach(mydata)
[consandip] Check the variable names
names(mydata)
[consandip] Data frame with log(CONS) and log(IP) in levels
logdata<-data.frame(log(CONS),log(IP))
[consandip] Name the columns
names(logdata)<-c("logCONS","logIP")
[consandip] Use column names directly
attach(logdata)
[consandip] Two plots above each other
par(mfrow=c(2,1))
[consandip] Plot both log series logCONS and logIP
ts.plot(logCONS);ts.plot(logIP)
[consandip] Load the unit root test package
library(CADFtest)
[consandip] Maximum lag = square root of sample size
max.lag<-round(sqrt((length(logCONS))))
[consandip] Unit root test with intercept and trend on log(CONS)
CADFtest(logCONS,type="trend",criterion="BIC",max.lag.y=max.lag)
[consandip] Unit root test with intercept and trend on log(IP)
CADFtest(logIP,type="trend",criterion="BIC",max.lag.y=max.lag)
[consandip] Log-differences of both series
dlogCONS<-diff(logCONS);dlogIP<-diff(logIP)
[consandip] Unit root test with intercept on dlogCONS
CADFtest(dlogCONS,type="drift",criterion="BIC",max.lag.y=max.lag)
[consandip] Unit root test with intercept on dlogIP
CADFtest(dlogIP,type="drift",criterion="BIC",max.lag.y=max.lag)
[consandip] Load the package for VAR models
library(vars)
[consandip] Lag selection for the VAR in levels (max 10 lags, with constant)
VARselect(logdata,lag.max=10,type="const")
[consandip] Load the package for cointegration tests
library(urca)
[consandip] Johansen trace test (K = 4 lags, constant in cointegration equation)
trace_test<-ca.jo(logdata,type="trace",K=4,ecdet="const",spec="transitory")
[consandip] Look at the trace test output
summary(trace_test)
[consandip] Johansen maximum eigenvalue test (same settings)
maxeigen_test<-ca.jo(logdata,type="eigen",K=4,ecdet="const",spec="transitory")
[consandip] Look at the maximum eigenvalue test output
summary(maxeigen_test)
[consandip] Estimate the VECM with 1 cointegration equation (from the trace test)
fit_vecm1<-cajorls(trace_test,r=1)
[consandip] Look at the VECM output
fit_vecm1
[consandip] Estimate the VECM from the maximum eigenvalue test
fit_vecm2<-cajorls(maxeigen_test,r=1)
[consandip] Look at the VECM output
fit_vecm2
[consandip] Transform the VECM to a VAR in levels
fit_var<-vec2var(trace_test,r=1)
[consandip] Forecast the next 6 quarters
myforecast<-predict(fit_var,n.ahead=6)
[consandip] Two plots above each other
par(mfrow=c(2,1))
[consandip] Plot the original log(CONS) series
ts.plot(log(CONS))
[consandip] log(CONS) point forecast as time series starting 2008 Q2
logCONS_forecast<-ts(myforecastfcstlogCONS[,1],frequency=4,start=c(2008,2))
[consandip] log(CONS) lower bound of the 95% prediction interval
logCONS_lower<-ts(myforecastfcstlogCONS[,2],frequency=4,start=c(2008,2))
[consandip] log(CONS) upper bound of the 95% prediction interval
logCONS_upper<-ts(myforecastfcstlogCONS[,3],frequency=4,start=c(2008,2))
[consandip] Plot forecast (black) with bounds (red)
ts.plot(logCONS_forecast,logCONS_lower,logCONS_upper,col=c("black","red","red"))
[consandip] Add a title to the plot
title(main = "6-step-ahead forecast of log(CONS)")
[consandip] Two plots above each other
par(mfrow=c(2,1))
[consandip] Plot the original log(IP) series
plot.ts(log(IP))
[consandip] log(IP) point forecast as time series starting 2008 Q2
logIP_forecast<-ts(myforecastfcstlogIP[,1],frequency=4,start=c(2008,2))
[consandip] log(IP) lower bound of the 95% prediction interval
logIP_lower<-ts(myforecastfcstlogIP[,2],frequency=4,start=c(2008,2))
[consandip] log(IP) upper bound of the 95% prediction interval
logIP_upper<-ts(myforecastfcstlogIP[,3],frequency=4,start=c(2008,2))
[consandip] Plot forecast (black) with bounds (red)
ts.plot(logIP_forecast,logIP_lower,logIP_upper,col=c("black","red","red"))
[consandip] Add a title to the plot
title(main = "6-step-ahead forecast of log(IP)")