-
Notifications
You must be signed in to change notification settings - Fork 1
/
1hrs_pv2_report_generator.R
315 lines (273 loc) · 10.6 KB
/
1hrs_pv2_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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
source('funcs.R')
# benchmark models ----
report.full(model = 'snaive()',
series = '1hrs pv2',
transformation = 'identity()',
traindays = 7,
testdays = 1)
report.full(model = 'meanf()',
series = '1hrs pv2',
transformation = 'identity()',
traindays = 7,
testdays = 1)
report.full(model = 'naive()',
series = '1hrs pv2',
transformation = 'identity()',
traindays = 7,
testdays = 1)
# try to find the best ARIMA model ----
report(model = 'Arima(order=c(1, 0, 0))',
series = '1hrs pv2',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
#try with one MA as well since I have one significant 1st positive lag and 2nd significant negative lag
report(model = 'Arima(order=c(1, 0, 1))',
series = '1hrs pv2',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
# sinusoidal ACF and 2 lag in PACF from the beginning of the dataset, but also lambda= 0.2654112
report(model = 'Arima(order=c(2, 0, 0), lambda=0.2654112)',
series = '1hrs pv2',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
# sinusoidal ACF and 2 lag in PACF from the beginning of the dataset
report(model = 'Arima(order=c(2, 0, 0))',
series = '1hrs pv2',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
# significant term in PACF, so add 1 SAR term
report(model = 'Arima(order=c(2, 0, 0), seasonal=c(1, 0, 0))',
series = '1hrs pv2',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
# try to reduce the AR term, indeed, better results
report(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0))',
series = '1hrs pv2',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
# add fourier terms
report(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), xreg=fourier(., K=5))',
series = '1hrs pv2',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3,
xreg='fourier(., K=5, h=h)')
# Find the best train:test days ratio for ARIMA(1,0,0)(1,0,0) ----
scaler <- 1/100000000000
best.fcast.1hrsPv2 <- NULL
best.traindays <- 0
best.testdays <- 0
for(traindays in 4:7)
{
for(testdays in 2: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")',
# we need scaling, otherwise we have an error similar to:
# computationally singular: reciprocal condition number = 1.90892e-19
# https://stackoverflow.com/questions/29522841/the-curious-case-of-arima-modelling-using-r
dataset = datasets[['1hrs pv2']]$series * scaler,
transformation = 'identity()',
traindays = traindays,
testdays = testdays,
xreg=NULL)
if(is.null(best.fcast.1hrsPv2) || current$accuracy[[2]] < best.fcast.1hrsPv2$accuracy[[2]])
{
best.fcast.1hrsPv2 <- current
best.traindays <- traindays
best.testdays <- testdays
}
}
}
report.full(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="ML")',
series = '1hrs pv2',
transformation = 'identity()',
traindays = best.traindays, # 5
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.1hrsPv2 <- NULL
best.k <- 0
#K must be not be greater than period/2
for(k in 1:(frequency(datasets[['1hrs pv2']]$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[['1hrs pv2']]$series * scaler,
transformation = 'identity()',
traindays = best.traindays, # 5
testdays = best.testdays, # 2
xreg=xreg)
if(is.null(best.fcast.k.1hrsPv2) || current$accuracy[[2]] < best.fcast.k.1hrsPv2$accuracy[[2]])
{
best.fcast.k.1hrsPv2 <- 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 = '1hrs pv2',
transformation = 'identity()',
traindays = best.traindays, # 5
testdays = best.testdays, # 2
xreg = paste('fourier(., h=h, K=', best.k, ')')) # 3
# Best model: 5:2, ARIMA(1, 0, 0)(1, 0, 0), K=3, RMSE=452 MAE=219 ----
# 13th-15th obs dummies rmse=453, mae=219
# without seasonal part, only fourier: rmse=448, mae=220
# without seasonal part, 2AR, only fourier: rmse=448, mae=220
# dummies without seasonal part, only fourier: rmse=448, mae=219
# dummies without seasonal part, 2AR, only fourier: rmse=448, mae=219
report.full(model = paste('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=fourier(., K=', best.k, '))', sep=''),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste('fourier(., h=h, K=3)'))
dummies.fcast <- quote(
{cbind(
dummies=getNthObsDummies(13, 2, h, frequency(.)),
fourier(., h=h, K=3)
)}
)
dummies.fit <- quote(
{cbind(
dummies=getNthObsDummies(13, 2, length(.), frequency(.)),
fourier(., K=3)
)}
)
report.full(model = paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(dummies.fit), collapse='') ,')'),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
report.full(model = paste('Arima(order=c(1, 0, 0), method="CSS", xreg=fourier(., K=', best.k, '))', sep=''),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste('fourier(., h=h, K=3)'))
report.full(model = paste('Arima(order=c(2, 0, 0), method="CSS", xreg=fourier(., K=', best.k, '))', sep=''),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste('fourier(., h=h, K=3)'))
report.full(model = paste0('Arima(order=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(dummies.fit), collapse='') ,')'),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
report.full(model = paste0('Arima(order=c(2, 0, 0), method="CSS", xreg=', paste0(deparse(dummies.fit), collapse='') ,')'),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
# dummies on every weekday ----
dailyD.fcast <- quote(
{cbind(
dummies=getDailyDummies(h, frequency(.), start(.)[[1]]),
fourier(., h=h, K=3)
)}
)
dailyD.fit <- quote(
{cbind(
dummies=getDailyDummies(length(.), frequency(.), start(.)[[1]]),
fourier(., K=3)
)}
)
# 5:2 rmse=?, mae=?
report.full(model = paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(dailyD.fit), collapse='') ,')'),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dailyD.fcast), collapse=''))
# dummies on the 8-13th+1-5th obs (the "outliers") ----
best.fcast.dummy.1hrsPv2 <- NULL
best.startDummy <- 0
best.lenDummy <- 0
for(startDummy in 8:13)
{
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=3)
)},
list(startDummy=startDummy, lenDummy=lenDummy)
)
obsDummies.fit <- substitute(
{cbind(
dummies=getNthObsDummies(startDummy, lenDummy, length(.), frequency(.)),
fourier(., K=3)
)},
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[['1hrs pv2']]$series,
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(obsDummies.fcast), collapse=''))
if(is.null(best.fcast.dummy.1hrsPv2) || current$accuracy[[2]] < best.fcast.dummy.1hrsPv2$accuracy[[2]])
{
best.fcast.dummy.1hrsPv2 <- current
best.startDummy <- startDummy
best.lenDummy <- lenDummy
}
}
}
bestObsDummies.fcast <- substitute(
{cbind(
dummies=getNthObsDummies(best.startDummy, best.lenDummy, h, frequency(.)),
fourier(., h=h, K=3)
)},
list(best.startDummy = best.startDummy, best.lenDummy = best.lenDummy)
)
bestObsDummies.fit <- substitute(
{cbind(
dummies=getNthObsDummies(best.startDummy, best.lenDummy, length(.), frequency(.)),
fourier(., K=3)
)},
list(best.startDummy = best.startDummy, best.lenDummy = best.lenDummy)
)
# 5:2, dummies: 13:2, rmse=453, mae=219
report.full(model = paste0('Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(bestObsDummies.fit), collapse='') ,')'),
series = '1hrs pv2',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(bestObsDummies.fcast), collapse=''))