-
Notifications
You must be signed in to change notification settings - Fork 2
/
531 final.R
364 lines (323 loc) · 12.4 KB
/
531 final.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
setwd("/Users/shutingliao/Documents/2017\ Winter/stats\ 531/data")
name_col=c("YR","MN","IP","MFG","MFGD","MFGN","MIN","UTIL","P","MAT")
M = read.table('USA industrial production.txt', col.names = name_col)
P = ts(M[,c(3:10)],start=c(1947, 1),frequency = 12)
head(P)
plot(P)
P1= diff(P,1)
plot(P1)
par(mfrow=c(2,2))
acf(P[,1], lag.max = 1000)
acf(P[,2], lag.max = 1000)
acf(P[,3], lag.max = 1000)
acf(P[,4], lag.max = 1000)
acf(P[,5], lag.max = 1000)
acf(P[,6], lag.max = 1000)
acf(P[,7], lag.max = 1000)
acf(P[,8], lag.max = 1000)
pacf(P[,1], lag.max = 1000)
pacf(P[,2], lag.max = 1000)
pacf(P[,3], lag.max = 1000)
pacf(P[,4], lag.max = 1000)
pacf(P[,5], lag.max = 1000)
pacf(P[,6], lag.max = 1000)
pacf(P[,7], lag.max = 1000)
pacf(P[,8], lag.max = 1000)
#Test
library(tseries)
library(urca)
library(forecast)
library(portes)
library(stats)
#stationary check
print(adf.test(P[,1], alt='stationary'))
print(kpss.test(P[,1], null='Level'))
print(kpss.test(P[,1], null='Trend'))
print(adf.test(P[,2], alt='stationary'))
print(kpss.test(P[,2], null='Level'))
print(kpss.test(P[,2], null='Trend'))
print(adf.test(P[,3], alt='stationary'))
print(kpss.test(P[,3], null='Level'))
print(kpss.test(P[,3], null='Trend'))
print(adf.test(P[,4], alt='stationary'))
print(kpss.test(P[,4], null='Level'))
print(kpss.test(P[,4], null='Trend'))
print(adf.test(P[,5], alt='stationary'))
print(kpss.test(P[,5], null='Level'))
print(kpss.test(P[,5], null='Trend'))
print(adf.test(P[,6], alt='stationary'))
print(kpss.test(P[,6], null='Level'))
print(kpss.test(P[,6], null='Trend'))
print(adf.test(P[,7], alt='stationary'))
print(kpss.test(P[,7], null='Level'))
print(kpss.test(P[,7], null='Trend'))
print(adf.test(P[,8], alt='stationary'))
print(kpss.test(P[,8], null='Level'))
print(kpss.test(P[,8], null='Trend'))
P1=diff(P,1)
#take difference
print(adf.test(P1[,1], alt='stationary'))
print(kpss.test(P1[,1], null='Level'))
print(kpss.test(P1[,1], null='Trend'))
print(adf.test(P1[,2], alt='stationary'))
print(kpss.test(P1[,2], null='Level'))
print(kpss.test(P1[,2], null='Trend'))
print(adf.test(P1[,3], alt='stationary'))
print(kpss.test(P1[,3], null='Level'))
print(kpss.test(P1[,3], null='Trend'))
print(adf.test(P1[,4], alt='stationary'))
print(kpss.test(P1[,4], null='Level'))
print(kpss.test(P1[,4], null='Trend'))
print(adf.test(P1[,5], alt='stationary'))
print(kpss.test(P1[,5], null='Level'))
print(kpss.test(P1[,5], null='Trend'))
print(adf.test(P1[,6], alt='stationary'))
print(kpss.test(P1[,6], null='Level'))
print(kpss.test(P1[,6], null='Trend'))
print(adf.test(P1[,7], alt='stationary'))
print(kpss.test(P1[,7], null='Level'))
print(kpss.test(P1[,7], null='Trend'))
print(adf.test(P1[,8], alt='stationary'))
print(kpss.test(P1[,8], null='Level'))
print(kpss.test(P1[,8], null='Trend'))
#2. Cointergration tests
#Engle-Granger conintergration test
z=list()
z1=list()
for(i in 1:8) z[[i]]= lm(P1[,i]~., P1[,-i])
for(i in 1:8) print(summary(z[[i]]))
for(i in 1:8) {z1[[i]]=ur.df(z[[i]]$res);
if (i ==1) print(z1[[i]]@cval);
print(z1[[i]]@teststat)}
#Pillips-Ouliaris conintergration test
zz=ca.po(P1[,1:6], demean="constant", type = "Pu")
print(zz@cval)
print(zz@teststat)
zz1=ca.po(P1[,1:6], demean="constant", type = "Pz")
print(zz1@cval)
print(zz1@teststat)
#Johansen procedure
at=ca.jo(P, type = "trace", ecdet = "const")
#summary(at)
at@cval
at@teststat#r=6
#granger test
library(MSBVAR)
g=granger.test(P1, p=8)
round(g, 4)
#library(MTS)
library(vars)
(m1=VAR(P1, p=4, type = "both"))
m2 = VAR(P, p=4, type = "both")
stability(m1)
e1 = residuals(m1)
par(mfrow=c(2,2)) ##line 28
plot(gvtest(e1[,1],1:60)[,4], main="Generalized variance tests for IP (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,1],1:60)[,4], main="Ljung-Box tests for IP (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,1], main="Acf of residuals for IP (VAR)", lag.max = 60) ##line 36
plot(e1[,1],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e1[,2],1:60)[,4], main="Generalized variance tests for MFG (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,2],1:60)[,4], main="Ljung-Box tests for MFG (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,2], main="Acf of residuals for MFG (VAR)", lag.max = 60) ##line 36
plot(e1[,2],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e1[,3],1:60)[,4], main="Generalized variance tests for MFGD (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,3],1:60)[,4], main="Ljung-Box tests for MFGD (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,3], main="Acf of residuals for MFGD (VAR)", lag.max = 60) ##line 36
plot(e1[,3],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e1[,4],1:60)[,4], main="Generalized variance tests for MFGN (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,4],1:60)[,4], main="Ljung-Box tests for MFGN (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,4], main="Acf of residuals for MFGN (VAR)", lag.max = 60) ##line 36
plot(e1[,4],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e1[,5],1:60)[,4], main="Generalized variance tests for MIN (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,5],1:60)[,4], main="Ljung-Box tests for MIN (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,5], main="Acf of residuals for MIN (VAR)", lag.max = 60) ##line 36
plot(e1[,5],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e1[,6],1:60)[,4], main="Generalized variance tests for UTIL (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,6],1:60)[,4], main="Ljung-Box tests for UTIL (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,6], main="Acf of residuals for UTIL (VAR)", lag.max = 60) ##line 36
plot(e1[,6],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e1[,7],1:60)[,4], main="Generalized variance tests for P (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,7],1:60)[,4], main="Ljung-Box tests for P (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,7], main="Acf of residuals for P (VAR)", lag.max = 60) ##line 36
plot(e1[,7],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e1[,8],1:60)[,4], main="Generalized variance tests for MAT (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e1[,8],1:60)[,4], main="Ljung-Box tests for MAT (VAR)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e1[,8], main="Acf of residuals for MAT (VAR)", lag.max = 60) ##line 36
plot(e1[,8],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
#prediction
z_pred=predict(m1, n.ahead=72, ci=0.95)
par(mar= c(1,1,1,1))
plot(z_pred)
m2_pred = predict(m2, n.ahead = 72, ci = 0.95)
plot(m2_pred)
#VARX
library(dse)
CNA = TSdata(input = P1[,c(1:4)], output=P1[,5:8])
CNA = tframed(CNA, list(start=c(1947,1), frequency=12))
CNA.ls = estVARXls(CNA, max.lag = 2)
print(CNA.ls)
#detach(package:vars)
stability(CNA.ls)
#residuals analysis
par(mar = c(1,1,1,1))
rr = checkResiduals(CNA.ls, plot=T)
plot(rr$residuals, type='p')
e2 = residuals(CNA.ls)
#par('mar')
par(mfrow=c(2,2))
par(mar=c(1,1,1,1))
plot(gvtest(e2[,1],1:60)[,4], main="Generalized variance tests for MIN (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e2[,1],1:60)[,4], main="Ljung-Box tests for MIN (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e2[,1], main="Acf of residuals for MIN (VARX)", lag.max = 60) ##line 36
plot(e2[,1],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e2[,2],1:60)[,4], main="Generalized variance tests for UTIL (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e2[,2],1:60)[,4], main="Ljung-Box tests for UTIL (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e2[,2], main="Acf of residuals for UTIL (VARX)", lag.max = 60) ##line 36
plot(e2[,2],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e2[,3],1:60)[,4], main="Generalized variance tests for P (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e2[,3],1:60)[,4], main="Ljung-Box tests for P (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e2[,3], main="Acf of residuals for P (VARX)", lag.max = 60) ##line 36
plot(e2[,3],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e2[,4],1:60)[,4], main="Generalized variance tests for MAT (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e2[,4],1:60)[,4], main="Ljung-Box tests for MAT (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e2[,4], main="Acf of residuals for MAT (VARX)", lag.max = 60) ##line 36
plot(e2[,4],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
#prediction
detach(package:forecast)
CNA.ls2=estVARXls(window(CNA, end = c(1985,12)),max.lag = 2)
S.p=forecast(CNA.ls2, conditioning.inputs=CNA$input)
S.p$forecast
par(mar=c(1,1,1,1))
tfplot(S.p)
#two outputs(MIN UTIL)
CNA_2 = TSdata(input = P1[,c(1:4, 7,8)], output=P1[,5:6])
CNA_2 = tframed(CNA_2, list(start=c(1947,1), frequency=12))
CNA.ls_2 = estVARXls(CNA_2, max.lag = 2)
print(CNA.ls_2)
#detach(package:vars)
stability(CNA.ls_2)
#residuals analysis
#par(mar = c(1,1,1,1))
rr = checkResiduals(CNA.ls_2, plot=T)
plot(rr$residuals, type='p')
e3 = residuals(CNA.ls_2)
#par('mar')
par(mfrow=c(2,2))
par(mar=c(1,1,1,1))
plot(gvtest(e3[,1],1:60)[,4], main="Generalized variance tests for MIN (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e3[,1],1:60)[,4], main="Ljung-Box tests for MIN (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e3[,1], main="Acf of residuals for MIN (VARX)", lag.max = 60) ##line 36
plot(e3[,1],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
plot(gvtest(e3[,2],1:60)[,4], main="Generalized variance tests for UTIL (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 31
abline(h=0.05,lty=2)##line 32
plot(LjungBox(e3[,2],1:60)[,4], main="Ljung-Box tests for UTIL (VARX)",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1)) ##line 34
abline(h=0.05, lty=2) ##line 35
Acf(e3[,2], main="Acf of residuals for UTIL (VARX)", lag.max = 60) ##line 36
plot(e3[,2],ylab="Residuals") ##line 37
title("Residuals series") ##line 38
abline(h=0,lty=2)
#prediction
detach(package:forecast)
CNA.ls2_2=estVARXls(window(CNA_2, end = c(1985,12)),max.lag = 2)
S.p=forecast(CNA.ls2_2, conditioning.inputs=CNA_2$input)
S.p$forecast
par(mar=c(1,1,1,1))
tfplot(S.p)
#AFIMA for UTIL
library(fracdiff)
(x.fd= fracdiff(P1[,6], nar = 2, nma = 1, M=30))
par(mfrow=c(2,2))
e4=residuals(x.fd)
plot(gvtest(e4,1:60)[,4], main="Generalized variance tests for AFIMA",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1))
abline(h=0.05,lty=2)
plot(LjungBox(e4,1:60)[,4], main="Ljung-Box tests for AFIMA",
ylab="p-value", xlab="lag", pch=16, ylim=c(0,1))
abline(h=0.05, lty=2)
Acf(e4, main="Acf of residuals for AFIMA", lag.max = 60)
plot(e4,ylab="Residuals")
title("Residuals series")
abline(h=0,lty=2)
pred = predict(x.fd, n.ahead=144, ci=0.95)
par(mfrow=c(1,1))
plot(pred)