-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2hrs_ph1_report_generator.R
294 lines (250 loc) · 9.29 KB
/
2hrs_ph1_report_generator.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
source('funcs.R')
# benchmark models ----
report.full(model = 'snaive()',
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 1)
report.full(model = 'meanf()',
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 1)
report.full(model = 'naive()',
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 1)
# try to find the best ARIMA model ----
# sinusoidal ACF and 1 lag in PACF from the beginning of the dataset
report(model = 'Arima(order=c(1, 0, 0))',
series = '2hrs ph1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
# sinusoidal ACF and 1 lag in PACF
report(model = 'Arima(order=c(1, 0, 0))',
series = '2hrs ph1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = -10,
traindays = 7,
testdays = 3)
# significant values in lags of order 12
report(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0))',
series = '2hrs ph1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = -10,
traindays = 7,
testdays = 3)
# two significant lags around the seasonal period
report(model = 'Arima(order=c(1, 0, 0), seasonal=c(2, 0, 0), method="CSS")',
series = '2hrs ph1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = -10,
traindays = 7,
testdays = 3)
# seems fine, but the forecast is overall a bit too high
report(model = 'Arima(order=c(1, 0, 0), seasonal=c(2, 0, 0), include.mean=FALSE, method="CSS")',
series = '2hrs ph1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = -10,
traindays = 7,
testdays = 3)
# regression with ARMA errors on fourier terms, (tries with 2 seasonal AR terms, but results were worse)
report(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), xreg=fourier(., K=2), method="CSS")',
series = '2hrs ph1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = -10,
traindays = 7,
testdays = 3,
xreg='fourier(., K=2, h=h)')
# Find the best train:test days ratio for ARIMA(1,0,0)(1,0,0) ----
best.fcast.2hrsPh1 <- NULL
best.traindays <- 0
best.testdays <- 0
for(traindays in 3:7)
{
for(testdays in 1:3)
{
print(paste("Trying", traindays, "train days and", testdays, "test days"))
current <- fullforecast(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS")',
dataset = datasets[['2hrs ph1']]$series,
transformation = 'identity()',
traindays = traindays,
testdays = testdays,
xreg=NULL)
if(is.null(best.fcast.2hrsPh1) || current$accuracy[[2]] < best.fcast.2hrsPh1$accuracy[[2]])
{
best.fcast.2hrsPh1 <- current
best.traindays <- traindays
best.testdays <- testdays
}
}
}
report.full(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="ML")',
series = '2hrs ph1',
transformation = 'identity()',
traindays = best.traindays, # 7
testdays = best.testdays) # 2
# Skip over the step where I hardcode a fourier value ----
# Find best K for the above model ARIMA(1,0,0)(1,0,0) ----
best.fcast.k.2hrsPh1 <- NULL
best.k <- 0
#K must be not be greater than period/2
for(k in 1:(frequency(datasets[['2hrs ph1']]$series)/2))
{
print(paste("Trying k =", k))
m <- paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=fourier(., K=', k, '))')
xreg <- paste0('fourier(., h=h, K=', k, ')')
current <- fullforecast(model = m,
dataset = datasets[['2hrs ph1']]$series,
transformation = 'identity()',
traindays = best.traindays, # 7
testdays = best.testdays, # 2
xreg=xreg)
if(is.null(best.fcast.k.2hrsPh1) || current$accuracy[[2]] < best.fcast.k.2hrsPh1$accuracy[[2]])
{
best.fcast.k.2hrsPh1 <- current
best.k <- k
}
}
report.full(model = paste('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=fourier(., K=', best.k, '))', sep=''),
series = '2hrs ph1',
transformation = 'identity()',
traindays = best.traindays, # 7
testdays = best.testdays, # 2
xreg = paste('fourier(., h=h, K=', best.k, ')')) #2
# Best model: 7:2, ARIMA(1, 0, 0)(1, 0, 0), K=2, RMSE=469.4395 MAE=223.396 ----
# 6th-9th obs dummies rmse=464, mae=222
# without seasonal part, only fourier: rmse=467, mae=220
# dummies without seasonal part, only fourier: rmse=462, mae=218
report.full(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=fourier(., K=2))',
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 2,
xreg = 'fourier(., h=h, K=2)')
dummies.fcast <- quote(
{cbind(
dummies=getNthObsDummies(6, 3, h, frequency(.)),
fourier(., h=h, K=2)
)}
)
dummies.fit <- quote(
{cbind(
dummies=getNthObsDummies(6, 3, length(.), frequency(.)),
fourier(., K=2)
)}
)
report.full(model = paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(dummies.fit), collapse='') ,')'),
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
#rmse=462, mae=218
report.full(model = paste0('Arima(order=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(dummies.fit), collapse='') ,')'),
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
# rmse=467, mae=220
report.full(model = 'Arima(order=c(1, 0, 0), method="CSS", xreg=fourier(., K=2))',
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 2,
xreg = 'fourier(., h=h, K=2)')
# dummies on 6th day - not applicable, patterns are too crazy ----
# dummies on every weekday ----
dailyD.fcast <- quote(
{cbind(
dummies=getDailyDummies(h, frequency(.), start(.)[[1]]),
fourier(., h=h, K=2)
)}
)
dailyD.fit <- quote(
{cbind(
dummies=getDailyDummies(length(.), frequency(.), start(.)[[1]]),
fourier(., K=2)
)}
)
# 7:2 rmse=486, mae=242
report.full(model = paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(dailyD.fit), collapse='') ,')'),
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 2,
xreg = paste0(deparse(dailyD.fcast), collapse=''))
# dummies on the 4-8th+1-5th obs (the "outliers") ----
best.fcast.dummy.2hrsPh1 <- NULL
best.startDummy <- 0
best.lenDummy <- 0
for(startDummy in 4:8)
{
for(lenDummy in 1:5)
{
print(paste("Trying startDummy =", startDummy, ", length =", lenDummy))
obsDummies.fcast <- substitute(
{cbind(
dummies=getNthObsDummies(startDummy, lenDummy, h, frequency(.)),
fourier(., h=h, K=2)
)},
list(startDummy=startDummy, lenDummy=lenDummy)
)
obsDummies.fit <- substitute(
{cbind(
dummies=getNthObsDummies(startDummy, lenDummy, length(.), frequency(.)),
fourier(., K=2)
)},
list(startDummy=startDummy, lenDummy=lenDummy)
)
current <- fullforecast(model = paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(obsDummies.fit), collapse='') ,')'),
dataset = datasets[['2hrs ph1']]$series,
transformation = 'identity()',
traindays = 7,
testdays = 2,
xreg = paste0(deparse(obsDummies.fcast), collapse=''))
if(is.null(best.fcast.dummy.2hrsPh1) || current$accuracy[[2]] < best.fcast.dummy.2hrsPh1$accuracy[[2]])
{
best.fcast.dummy.2hrsPh1 <- current
best.startDummy <- startDummy
best.lenDummy <- lenDummy
}
}
}
bestObsDummies.fcast <- substitute(
{cbind(
dummies=getNthObsDummies(best.startDummy, best.lenDummy, h, frequency(.)),
fourier(., h=h, K=2)
)},
list(best.startDummy = best.startDummy, best.lenDummy = best.lenDummy)
)
bestObsDummies.fit <- substitute(
{cbind(
dummies=getNthObsDummies(best.startDummy, best.lenDummy, length(.), frequency(.)),
fourier(., K=2)
)},
list(best.startDummy = best.startDummy, best.lenDummy = best.lenDummy)
)
# 7:2, dummies: 6:3, rmse=464, mae=222
report.full(model = paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(bestObsDummies.fit), collapse='') ,')'),
series = '2hrs ph1',
transformation = 'identity()',
traindays = 7,
testdays = 2,
xreg = paste0(deparse(bestObsDummies.fcast), collapse=''))