-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVECM.R
149 lines (109 loc) · 3.08 KB
/
VECM.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
### Co-integration test
library(urca)
library(forecast)
install.packages("readxl")
require(readxl)
install.packages("MTS")
require(MTS)
install.packages("vars")
library(vars)
install.packages("fUnitRoots")
library(fUnitRoots)
da=read_excel("FANG.xlsx")
amzn_ = da[,2]
fb_ = da[,3]
googl_ = da[,4]
nflx_ = da[,5]
amzn = ts(log(na.omit(as.numeric(unlist(amzn_[[1]])))))
fb = ts(log(na.omit(as.numeric(unlist(fb_[[1]])))))
googl = ts(log(na.omit(as.numeric(unlist(googl_[[1]])))))
nflx = ts(log(na.omit(as.numeric(unlist(nflx_[[1]])))))
z = cbind(amzn,fb,googl,nflx)
a = cbind(fb,googl)
zt = diffM(z,1)
acf(z[,1])
acf(z[,2])
acf(z[,3])
acf(z[,4])
acf(zt[,1])
acf(zt[,2])
acf(zt[,3])
acf(zt[,4])
adfTest(z[,1],lags=1,type="c")
adfTest(z[,2],lags=1,type="c")
adfTest(z[,3],lags=1,type="c")
adfTest(z[,4],lags=1,type="c")
adfTest(zt[,1],lags=1,type="c")
adfTest(zt[,2],lags=1,type="c")
adfTest(zt[,3],lags=1,type="c")
adfTest(zt[,4],lags=1,type="c")
# plot FANG
plot(merge(as.zoo(fb), as.zoo(amzn), as.zoo(googl), as.zoo(nflx)),
plot.type = "single",
lty = c(2, 1),
lwd = 2,
xlab = "Date",
ylab = "Price",
ylim = c(0, 15),
main = "FANG")
lines(as.zoo(fb),
col = "black",
lwd = 1,)
lines(as.zoo(amzn),
col = "grey",
lwd = 2,)
lines(as.zoo(nflx),
col = "steelblue",
lwd = 2,)
lines(as.zoo(googl),
col = "red",
lwd = 1,)
# add horizontal line add 0
abline(0, 0)
# add a legend
legend("topright",
legend = c("FB", "AMZN", "NFLX", "GOOGL"),
col = c("black", "grey", "steelblue", "red"),
lwd = c(2, 2, 2),
lty = c(1, 1, 1))
VARselect(a, lag.max = 10, type = "const")
VARselect(a, lag.max = 10, type = "const")$selection
cointest = ca.jo(a, K = 2, type= "eigen", ecdet = c("const"), spec = c("transitory"))
cointest@teststat[2]
cointest@teststat[1]
cointest@cval
summary(cointest)
ac = ca.jo(z, K = 5, ecdet = c("none"), spec = c("transitory"))
summary(ac)
ad = ca.jo(z, K = 5, ecdet = c("none"), type = c("trace"), spec = c("transitory"))
summary(ad)
m2=ca.jo(bnd,K=2,ecdet=c("none"))
summary(m2)
m3=ca.jo(bnd,K=2,ecdet=c("none"),spec=c("transitory"))
summary(m3)
m4=ca.jo(bnd,K=2,ecdet=c("none"),type=c("trace"),spec=c("transitory"))
summary(m4)
wt=z[,1]-0.002*z[,2]
adfTest(wt,lags=3,type="c")
############### Estimation of ECM model
m1=ECMvar1(z,3,wt) ## Given the co-integrated vector
m2=refECMvar1(m1) ####### Refine the model fit
beta=c(1,-0.002) ### Initial value of co-integration
m3=ECMvar(z,3,beta,include.const=F) #### Joint estimation
#### 4-Macro Example
da=read.table("q-4macro.txt",header=T)
zt=cbind(log(da$rgnp),da$tb3m,log(da$m1sk),da$gs10)
colnames(zt) <- c("rgnp","tb3m","lnm1","gs10")
m1=VARorderI(zt)
summary(m2)
require(fUnitRoots)
require(urca)
m2=ca.jo(zt,K=5,ecdet=c("const"),spec=c("transitory"))
summary(m2)
m3=ca.jo(zt,K=5,ecdet=c("const"),spec=c("transitory"),type=c("trace"))
summary(m3)
w1t=zt[,1]-0.282*zt[,2]-0.792*zt[,3]+0.313*zt[,4]
w2t=zt[,1]-0.78*zt[,2]-0.673*zt[,3]+0.773*zt[,4]
adfTest(w1t,lags=6,type="c")
adfTest(w2t,lags=6,type="c")
MTSdiag(m3) ### Model checking