PC session 6

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/43

encourage image

There's no tags or description

Looks like no tags are added yet.

44 Terms

1
New cards

[consandip] Clear the workspace

rm(list=ls())

2
New cards

[consandip] Set the working directory

setwd("…")

3
New cards

[consandip] Read the data file

mydata<-read.table(file="consandip.txt",header=T)

4
New cards

[consandip] Use variable names directly

attach(mydata)

5
New cards

[consandip] Check the variable names

names(mydata)

6
New cards

[consandip] Data frame with log(CONS) and log(IP) in levels

logdata<-data.frame(log(CONS),log(IP))

7
New cards

[consandip] Name the columns

names(logdata)<-c("logCONS","logIP")

8
New cards

[consandip] Use column names directly

attach(logdata)

9
New cards

[consandip] Two plots above each other

par(mfrow=c(2,1))

10
New cards

[consandip] Plot both log series logCONS and logIP

ts.plot(logCONS);ts.plot(logIP)

11
New cards

[consandip] Load the unit root test package

library(CADFtest)

12
New cards

[consandip] Maximum lag = square root of sample size

max.lag<-round(sqrt((length(logCONS))))

13
New cards

[consandip] Unit root test with intercept and trend on log(CONS)

CADFtest(logCONS,type="trend",criterion="BIC",max.lag.y=max.lag)

14
New cards

[consandip] Unit root test with intercept and trend on log(IP)

CADFtest(logIP,type="trend",criterion="BIC",max.lag.y=max.lag)

15
New cards

[consandip] Log-differences of both series

dlogCONS<-diff(logCONS);dlogIP<-diff(logIP)

16
New cards

[consandip] Unit root test with intercept on dlogCONS

CADFtest(dlogCONS,type="drift",criterion="BIC",max.lag.y=max.lag)

17
New cards

[consandip] Unit root test with intercept on dlogIP

CADFtest(dlogIP,type="drift",criterion="BIC",max.lag.y=max.lag)

18
New cards

[consandip] Load the package for VAR models

library(vars)

19
New cards

[consandip] Lag selection for the VAR in levels (max 10 lags, with constant)

VARselect(logdata,lag.max=10,type="const")

20
New cards

[consandip] Load the package for cointegration tests

library(urca)

21
New cards

[consandip] Johansen trace test (K = 4 lags, constant in cointegration equation)

trace_test<-ca.jo(logdata,type="trace",K=4,ecdet="const",spec="transitory")

22
New cards

[consandip] Look at the trace test output

summary(trace_test)

23
New cards

[consandip] Johansen maximum eigenvalue test (same settings)

maxeigen_test<-ca.jo(logdata,type="eigen",K=4,ecdet="const",spec="transitory")

24
New cards

[consandip] Look at the maximum eigenvalue test output

summary(maxeigen_test)

25
New cards

[consandip] Estimate the VECM with 1 cointegration equation (from the trace test)

fit_vecm1<-cajorls(trace_test,r=1)

26
New cards

[consandip] Look at the VECM output

fit_vecm1

27
New cards

[consandip] Estimate the VECM from the maximum eigenvalue test

fit_vecm2<-cajorls(maxeigen_test,r=1)

28
New cards

[consandip] Look at the VECM output

fit_vecm2

29
New cards

[consandip] Transform the VECM to a VAR in levels

fit_var<-vec2var(trace_test,r=1)

30
New cards

[consandip] Forecast the next 6 quarters

myforecast<-predict(fit_var,n.ahead=6)

31
New cards

[consandip] Two plots above each other

par(mfrow=c(2,1))

32
New cards

[consandip] Plot the original log(CONS) series

ts.plot(log(CONS))

33
New cards

[consandip] log(CONS) point forecast as time series starting 2008 Q2

logCONS_forecast<-ts(myforecastfcstfcstlogCONS[,1],frequency=4,start=c(2008,2))

34
New cards

[consandip] log(CONS) lower bound of the 95% prediction interval

logCONS_lower<-ts(myforecastfcstfcstlogCONS[,2],frequency=4,start=c(2008,2))

35
New cards

[consandip] log(CONS) upper bound of the 95% prediction interval

logCONS_upper<-ts(myforecastfcstfcstlogCONS[,3],frequency=4,start=c(2008,2))

36
New cards

[consandip] Plot forecast (black) with bounds (red)

ts.plot(logCONS_forecast,logCONS_lower,logCONS_upper,col=c("black","red","red"))

37
New cards

[consandip] Add a title to the plot

title(main = "6-step-ahead forecast of log(CONS)")

38
New cards

[consandip] Two plots above each other

par(mfrow=c(2,1))

39
New cards

[consandip] Plot the original log(IP) series

plot.ts(log(IP))

40
New cards

[consandip] log(IP) point forecast as time series starting 2008 Q2

logIP_forecast<-ts(myforecastfcstfcstlogIP[,1],frequency=4,start=c(2008,2))

41
New cards

[consandip] log(IP) lower bound of the 95% prediction interval

logIP_lower<-ts(myforecastfcstfcstlogIP[,2],frequency=4,start=c(2008,2))

42
New cards

[consandip] log(IP) upper bound of the 95% prediction interval

logIP_upper<-ts(myforecastfcstfcstlogIP[,3],frequency=4,start=c(2008,2))

43
New cards

[consandip] Plot forecast (black) with bounds (red)

ts.plot(logIP_forecast,logIP_lower,logIP_upper,col=c("black","red","red"))

44
New cards

[consandip] Add a title to the plot

title(main = "6-step-ahead forecast of log(IP)")