-
Notifications
You must be signed in to change notification settings - Fork 1
/
comparison of methods.R
283 lines (230 loc) · 9.5 KB
/
comparison of methods.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
# rm(list=ls())
library(githubinstall) # library for getting the under-development part of mlr package
# gh_install_packages("mlr-org/mlr", ref = "forecasting")
# installation of a package augmentation -- cross-validation for time-series
library(RCurl) # data download from ftp server
library(dplyr)
library(kernlab) # SVR via ksvm() - caret does not support svm() with RBF kerneù
library(forecast) # ARIMA via auto.arima()
library(tseries)
library(PSF) # PSF via psf()
library(mlr) # train() for optimalization
library(emoa) # required by mlr package
library(FSelector)
library(parallelMap)
set.seed(100)
##### Naive SVRARIMA #####
# MAE
plot(naiveSVRARIMA2[[3]][[1]], type = "l")
lines(naiveSVRARIMA3[[3]][[1]], col = "red")
lines(naiveSVRARIMA4[[3]][[1]], col = "blue")
lines(naiveSVRARIMA5[[3]][[1]], col = "green")
sum(naiveSVRARIMA2[[3]][[1]])
sum(naiveSVRARIMA3[[3]][[1]])
sum(naiveSVRARIMA4[[3]][[1]])
sum(naiveSVRARIMA5[[3]][[1]])
# MAPE
plot(naiveSVRARIMA2[[3]][[2]], type = "l")
lines(naiveSVRARIMA3[[3]][[2]], col = "red")
lines(naiveSVRARIMA4[[3]][[2]], col = "blue")
lines(naiveSVRARIMA5[[3]][[2]], col = "green")
# RMSE
plot(naiveSVRARIMA2[[3]][[3]], type = "l")
lines(naiveSVRARIMA3[[3]][[3]], col = "red")
lines(naiveSVRARIMA4[[3]][[3]], col = "blue")
lines(naiveSVRARIMA5[[3]][[3]], col = "green")
sum(naiveSVRARIMA2[[3]][[3]])
sum(naiveSVRARIMA3[[3]][[3]])
sum(naiveSVRARIMA4[[3]][[3]])
sum(naiveSVRARIMA5[[3]][[3]])
##### SVRARIMA #####
# plots the measures over the 100-week period and also gives us the overall sum of the errors
# excluding MAPE because of the existence of 0 in the time series (i.e. infinity)
# MAE
plot(diffSVRARIMA2[[3]][[1]], type = "l")
lines(diffSVRARIMA3[[3]][[1]], col = "red")
lines(diffSVRARIMA4[[3]][[1]], col = "blue")
lines(diffSVRARIMA5[[3]][[1]], col = "green")
sum(diffSVRARIMA2[[3]][[1]])
sum(diffSVRARIMA3[[3]][[1]])
sum(diffSVRARIMA4[[3]][[1]]) # appears to be the best
sum(diffSVRARIMA5[[3]][[1]])
# MAPE
plot(diffSVRARIMA2[[3]][[2]], type = "l")
lines(diffSVRARIMA3[[3]][[2]], col = "red")
lines(diffSVRARIMA4[[3]][[2]], col = "blue")
lines(diffSVRARIMA5[[3]][[2]], col = "green")
# RMSE
plot(diffSVRARIMA2[[3]][[3]], type = "l")
lines(diffSVRARIMA3[[3]][[3]], col = "red")
lines(diffSVRARIMA4[[3]][[3]], col = "blue")
lines(diffSVRARIMA5[[3]][[3]], col = "green")
sum(diffSVRARIMA2[[3]][[3]])
sum(diffSVRARIMA3[[3]][[3]])
sum(diffSVRARIMA4[[3]][[3]])
sum(diffSVRARIMA5[[3]][[3]])
##### SVR #####
# MAE
plot(diffLag2[[2]][[1]], type = "l")
lines(diffLag3[[2]][[1]], col = "red")
lines(diffLag4[[2]][[1]], col = "blue")
lines(diffLag5[[2]][[1]], col = "green")
sum(diffLag2[[2]][[1]])
sum(diffLag3[[2]][[1]])
sum(diffLag4[[2]][[1]])
sum(diffLag5[[2]][[1]])
# MAPE
plot(diffLag2[[2]][[2]], type = "l")
lines(diffLag3[[2]][[2]], col = "red")
lines(diffLag4[[2]][[2]], col = "blue")
lines(diffLag5[[2]][[2]], col = "green")
# RMSE
plot(diffLag2[[2]][[3]], type = "l")
lines(diffLag3[[2]][[3]], col = "red")
lines(diffLag4[[2]][[3]], col = "blue")
lines(diffLag5[[2]][[3]], col = "green")
sum(diffLag2[[2]][[3]])
sum(diffLag3[[2]][[3]])
sum(diffLag4[[2]][[3]])
sum(diffLag5[[2]][[3]])
##### ARIMA #####
# plots the measures over the 100-week period and also gives us the overall sum of the errors
# excluding MAPE because of the existence of 0 in the time series (i.e. infinity)
# MAE
plot(arimaPerf[[1]][1:100], type = "l")
sum(arimaPerf[[1]][1:100])
# MAPE
plot(arimaPerf[[2]][1:100], type = "l")
# RMSE
plot(arimaPerf[[3]][1:100], type = "l")
sum(arimaPerf[[3]][1:100])
##### PSF #####
# plots the measures over the 100-week period and also gives us the overall sum of the errors
# excluding MAPE because of the existence of 0 in the time series (i.e. infinity)
# MAE
plot(unlist(PSF[[1]][[1]]), type = "l")
sum(PSF[[1]][[1]])
# MAPE
plot(unlist(PSF[[1]][[2]]), type = "l")
# RMSE
plot(unlist(PSF[[1]][[3]]), type = "l")
sum(PSF[[1]][[3]])
##### Comparison #####
# compares ARIMA and SVR and how often one outperforms another
arimaVsvr2 <- c(sum(arimaPerf[[1]][1:100] < diffLag2[[2]][[1]]), sum(arimaPerf[[3]][1:100] < diffLag2[[2]][[3]]))
arimaVsvr3 <- c(sum(arimaPerf[[1]][1:100] < diffLag3[[2]][[1]]), sum(arimaPerf[[3]][1:100] < diffLag3[[2]][[3]]))
arimaVsvr4 <- c(sum(arimaPerf[[1]][1:100] < diffLag4[[2]][[1]]), sum(arimaPerf[[3]][1:100] < diffLag4[[2]][[3]]))
arimaVsvr5 <- c(sum(arimaPerf[[1]][1:100] < diffLag5[[2]][[1]]), sum(arimaPerf[[3]][1:100] < diffLag5[[2]][[3]]))
# compares ARIMA and SVRARIMA and how often one outperforms another
arimaVhybrid2 <- c(sum(arimaPerf[[1]][1:100] < diffSVRARIMA2[[3]][[1]]), sum(arimaPerf[[3]][1:100] < diffSVRARIMA2[[3]][[3]]))
arimaVhybrid3 <- c(sum(arimaPerf[[1]][1:100] < diffSVRARIMA3[[3]][[1]]), sum(arimaPerf[[3]][1:100] < diffSVRARIMA3[[3]][[3]]))
arimaVhybrid4 <- c(sum(arimaPerf[[1]][1:100] < diffSVRARIMA4[[3]][[1]]), sum(arimaPerf[[3]][1:100] < diffSVRARIMA4[[3]][[3]]))
arimaVhybrid5 <- c(sum(arimaPerf[[1]][1:100] < diffSVRARIMA5[[3]][[1]]), sum(arimaPerf[[3]][1:100] < diffSVRARIMA5[[3]][[3]]))
# plots the difference between sVRARIMA and ARIMA - MAE, RMSE
plot(diffSVRARIMA2[[3]][[1]] - arimaPerf[[1]][1:100], type = "l")
abline(0, 0)
lines(diffSVRARIMA3[[3]][[1]] - arimaPerf[[1]][1:100], col = "red")
lines(diffSVRARIMA4[[3]][[1]] - arimaPerf[[1]][1:100], col = "blue")
lines(diffSVRARIMA5[[3]][[1]] - arimaPerf[[1]][1:100], col = "green")
plot(diffSVRARIMA2[[3]][[3]] - arimaPerf[[3]][1:100], type = "l")
abline(0, 0)
lines(diffSVRARIMA3[[3]][[3]] - arimaPerf[[3]][1:100], col = "red")
lines(diffSVRARIMA4[[3]][[3]] - arimaPerf[[3]][1:100], col = "blue")
lines(diffSVRARIMA5[[3]][[3]] - arimaPerf[[3]][1:100], col = "green")
##### Diebold-Mariano test #####
orig <- list()
for (i in 1:100){
orig[[i]] <- ts2[(529 + (i-1)*168):(696 + (i-1)*168)]
}
dmTest <- function(orig, pred1, pred2){ # test for calculating all 100 statistics in 1 line
outcome <- list(a, b)
statistics <- vector()
pvals <- vector()
for (i in 1:100){
e1 <- pred1[[i]] - orig[[i]]
e2 <- pred2[[i]] - orig[[i]]
outcome[[1]][[i]] <- dm.test(e1, e2, power = 1)
statistics[[i]] <- outcome[[1]][[i]]$statistic
pvals[[i]] <- outcome[[1]][[i]]$p.value
}
outcome[[2]] <- statistics
outcome[[3]] <- pvals
return(outcome)
}
dmTestSVR2 <- dmTest(orig, arimaPred, diffLag2[[3]]) # the tests themsleves
dmTestSVR3 <- dmTest(orig, arimaPred, diffLag3[[3]])
dmTestSVR4 <- dmTest(orig, arimaPred, diffLag4[[3]])
dmTestSVR5 <- dmTest(orig, arimaPred, diffLag5[[3]])
dmTestSVR2b <- dmTest(orig, diffLag2[[3]], diffSVRARIMA2[[2]]) #list(a, b, c)
#for(i in c(1:93, 95:100)){
# dmTestSVR2b[[1]][[i]] <- dm.test(diffLag2[[3]][[i]] - orig[[i]], diffSVRARIMA2[[2]][[i]] - orig[[i]], power = 1)
# dmTestSVR2b[[2]][i] <- dmTestSVR2b[[1]][[i]]$statistic
# dmTestSVR2b[[3]][i] <- dmTestSVR2b[[1]][[i]]$p.value
#}
dmTestSVR3b <- dmTest(orig, diffLag3[[3]], diffSVRARIMA3[[2]])
dmTestSVR4b <- dmTest(orig, diffLag4[[3]], diffSVRARIMA4[[2]])
dmTestSVR5b <- dmTest(orig, diffLag5[[3]], diffSVRARIMA5[[2]])
dmTestSVRARIMA2 <- dmTest(orig, arimaPred, diffSVRARIMA2[[2]])
dmTestSVRARIMA3 <- dmTest(orig, arimaPred, diffSVRARIMA3[[2]])
dmTestSVRARIMA4 <- dmTest(orig, arimaPred, diffSVRARIMA4[[2]])
dmTestSVRARIMA5 <- dmTest(orig, arimaPred, diffSVRARIMA5[[2]])
plot(density(dmTestSVR2[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVR3[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVR4[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVR5[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
c(sum(dmTestSVR2[[2]] < -1.96), sum(-1.96 < dmTestSVR2[[2]] & dmTestSVR2[[2]] < 1.96),
sum(dmTestSVR2[[2]] > 1.96))
c(sum(dmTestSVR3[[2]] < -1.96), sum(-1.96 < dmTestSVR3[[2]] & dmTestSVR3[[2]] < 1.96),
sum(dmTestSVR3[[2]] > 1.96))
c(sum(dmTestSVR4[[2]] < -1.96), sum(-1.96 < dmTestSVR4[[2]] & dmTestSVR4[[2]] < 1.96),
sum(dmTestSVR4[[2]] > 1.96))
c(sum(dmTestSVR5[[2]] < -1.96), sum(-1.96 < dmTestSVR5[[2]] & dmTestSVR5[[2]] < 1.96),
sum(dmTestSVR5[[2]] > 1.96))
plot(density(dmTestSVR2b[[2]], na.rm = T))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVR3b[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVR4b[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVR5b[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
c(sum(dmTestSVR2b[[2]] < -1.96, na.rm = T), sum(-1.96 < dmTestSVR2b[[2]] & dmTestSVR2b[[2]] < 1.96, na.rm = T),
sum(dmTestSVR2b[[2]] > 1.96, na.rm = T))
c(sum(dmTestSVR3b[[2]] < -1.96), sum(-1.96 < dmTestSVR3b[[2]] & dmTestSVR3b[[2]] < 1.96),
sum(dmTestSVR3b[[2]] > 1.96))
c(sum(dmTestSVR4b[[2]] < -1.96), sum(-1.96 < dmTestSVR4b[[2]] & dmTestSVR4b[[2]] < 1.96),
sum(dmTestSVR4b[[2]] > 1.96))
c(sum(dmTestSVR5b[[2]] < -1.96), sum(-1.96 < dmTestSVR5b[[2]] & dmTestSVR5b[[2]] < 1.96),
sum(dmTestSVR5b[[2]] > 1.96))
plot(density(dmTestSVRARIMA2[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVRARIMA3[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVRARIMA4[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
plot(density(dmTestSVRARIMA5[[2]]))
abline(v = 1.96)
abline(v = - 1.96)
c(sum(dmTestSVRARIMA2[[2]] < -1.96), sum(-1.96 < dmTestSVRARIMA2[[2]] & dmTestSVRARIMA2[[2]] < 1.96),
sum(dmTestSVRARIMA2[[2]] > 1.96))
c(sum(dmTestSVRARIMA3[[2]] < -1.96), sum(-1.96 < dmTestSVRARIMA3[[2]] & dmTestSVRARIMA3[[2]] < 1.96),
sum(dmTestSVRARIMA3[[2]] > 1.96))
c(sum(dmTestSVRARIMA4[[2]] < -1.96), sum(-1.96 < dmTestSVRARIMA4[[2]] & dmTestSVRARIMA4[[2]] < 1.96),
sum(dmTestSVRARIMA4[[2]] > 1.96))
c(sum(dmTestSVRARIMA5[[2]] < -1.96), sum(-1.96 < dmTestSVRARIMA5[[2]] & dmTestSVRARIMA5[[2]] < 1.96),
sum(dmTestSVRARIMA5[[2]] > 1.96))