-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2hrs_pv1_report_generator.R
302 lines (258 loc) · 9.57 KB
/
2hrs_pv1_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
source('funcs.R')
# benchmark models ----
report.full(model = 'snaive()',
series = '2hrs pv1',
transformation = 'identity()',
traindays = 7,
testdays = 1)
report.full(model = 'meanf()',
series = '2hrs pv1',
transformation = 'identity()',
traindays = 7,
testdays = 1)
report.full(model = 'naive()',
series = '2hrs pv1',
transformation = 'identity()',
traindays = 7,
testdays = 1)
# try to find the best ARIMA model ----
report(model = 'Arima(order=c(1, 0, 0))',
series = '2hrs pv1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
#MA wi worse than AR
report(model = 'Arima(order=c(0, 0, 1))',
series = '2hrs pv1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
traindays = 7,
testdays = 3)
report(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0))',
series = '2hrs pv1',
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=2))',
series = '2hrs pv1',
transformation = 'identity()',
diffs = 'identity()',
sdiffs = 'identity()',
startday = 0,
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.2hrsPv1 <- NULL
best.traindays <- 0
best.testdays <- 0
for(traindays in 3: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")',
dataset = datasets[['2hrs pv1']]$series,
transformation = 'identity()',
traindays = traindays,
testdays = testdays,
xreg=NULL)
if(is.null(best.fcast.2hrsPv1) || current$accuracy[[2]] < best.fcast.2hrsPv1$accuracy[[2]])
{
best.fcast.2hrsPv1 <- 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 pv1',
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.2hrsPv1 <- NULL
best.k <- 0
#K must be not be greater than period/2
for(k in 1:(frequency(datasets[['2hrs pv1']]$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 pv1']]$series,
transformation = 'identity()',
traindays = best.traindays, # ?
testdays = best.testdays, # ?
xreg=xreg)
if(is.null(best.fcast.k.2hrsPv1) || current$accuracy[[2]] < best.fcast.k.2hrsPv1$accuracy[[2]])
{
best.fcast.k.2hrsPv1 <- 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 pv1',
transformation = 'identity()',
traindays = best.traindays, # 5
testdays = best.testdays, # 2
xreg = paste('fourier(., h=h, K=', best.k, ')')) # 2
# Best model: 5:2, ARIMA(1, 0, 0)(1, 0, 0), K=2, RMSE=488 MAE=244 ----
# 8th-10th obs dummies rmse=489, mae=242
# without seasonal part, only fourier: rmse=466, mae=238
# without seasonal part, 2AR, only fourier: rmse=466, mae=239
# dummies without seasonal part, only fourier: rmse=467, mae=241
# dummies without seasonal part, 2AR, only fourier: rmse=467, mae=241
report.full(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS", xreg=fourier(., K=2))',
series = '2hrs pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = 'fourier(., h=h, K=2)')
dummies.fcast <- quote(
{cbind(
dummies=getNthObsDummies(8, 2, h, frequency(.)),
fourier(., h=h, K=2)
)}
)
dummies.fit <- quote(
{cbind(
dummies=getNthObsDummies(8, 2, 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 pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
# NO SAR, with dummies
# rmse=466, mae=238
report.full(model = paste0('Arima(order=c(1, 0, 0), method="CSS", xreg=', paste0(deparse(dummies.fit), collapse='') ,')'),
series = '2hrs pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
# no SAR with dummies
# 2 AR terms
# rmse=466, mae=239
report.full(model = paste0('Arima(order=c(2, 0, 0), method="CSS", xreg=', paste0(deparse(dummies.fit), collapse='') ,')'),
series = '2hrs pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dummies.fcast), collapse=''))
# no SAR, no dummies
# rmse=467, mae=241
report.full(model = 'Arima(order=c(1, 0, 0), method="CSS", xreg=fourier(., K=2))',
series = '2hrs pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = 'fourier(., h=h, K=2)')
# no SAR, no dummies, 1 MA
# rmse=467, mae=242
report.full(model = 'Arima(order=c(1, 0, 1), method="CSS", xreg=fourier(., K=2))',
series = '2hrs pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = 'fourier(., h=h, K=2)')
# no SAR, no dummies, 2 AR terms
# rmse=467, mae=241
report.full(model = 'Arima(order=c(2, 0, 0), method="CSS", xreg=fourier(., K=2))',
series = '2hrs pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = 'fourier(., h=h, K=2)')
# 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)
)}
)
# 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 = '2hrs pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(dailyD.fcast), collapse=''))
# dummies on the 4-8th+1-3th obs (the "outliers") ----
best.fcast.dummy.2hrsPv1 <- NULL
best.startDummy <- 0
best.lenDummy <- 0
for(startDummy in 4:8)
{
for(lenDummy in 1:3)
{
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 pv1']]$series,
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(obsDummies.fcast), collapse=''))
if(is.null(best.fcast.dummy.2hrsPv1) || current$accuracy[[2]] < best.fcast.dummy.2hrsPv1$accuracy[[2]])
{
best.fcast.dummy.2hrsPv1 <- 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)
)
# 5:2, dummies: 8:2, rmse=489, mae=242
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 pv1',
transformation = 'identity()',
traindays = 5,
testdays = 2,
xreg = paste0(deparse(bestObsDummies.fcast), collapse=''))