Skip to content

Commit bfc9a0b

Browse files
committed
marked the best number of observations when doing observation based modelling and implemented fullforecast.serial.obs
1 parent 7eae810 commit bfc9a0b

File tree

3 files changed

+103
-7
lines changed

3 files changed

+103
-7
lines changed

Diff for: 1hrs_ph3_report_generator.R

+6-4
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,12 @@ for(testobs in 1:(frequency(datasets[['1hrs ph3']]$series) - 1)) # 1:23
224224
}
225225
}
226226

227+
#RMSE 336
227228
report.full(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="CSS")',
228229
series = '1hrs ph3',
229230
transformation = 'identity()',
230231
traindays = trainobs,
231-
testdays = best.testobs, # ?
232+
testdays = best.testobs, # 1
232233
obs = TRUE)
233234

234235

@@ -252,10 +253,11 @@ for(testobs in 1:(frequency(datasets[['1hrs ph3']]$series) - 1)) # 1:11
252253
}
253254
}
254255

256+
#RMSE 316
255257
report.full(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), xreg=fourier(., K=2), method="CSS")',
256258
series = '1hrs ph3',
257259
transformation = 'identity()',
258260
traindays = trainobs,
259-
testdays = best.fourier.testobs, # ?
260-
xreg = 'fourier(., h=h, K=1)',
261-
obs = TRUE)
261+
testdays = best.fourier.testobs, # 1
262+
xreg = 'fourier(., h=h, K=2)',
263+
obs = TRUE)

Diff for: funcs.R

+70
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,73 @@ fullforecast.obs <- function(dataset, transformation, model, trainobs, testobs,
337337

338338
return(fcasts)
339339
}
340+
341+
342+
fullforecast.serial.obs <- function(dataset, transformation, model, trainobs, testobs, xreg, max.iterations = NULL)
343+
{
344+
startobs <- 0
345+
346+
if(is.null(max.iterations))
347+
{
348+
endobs <- length(dataset) - (trainobs + testobs)
349+
}else{
350+
endobs <- max.iterations
351+
}
352+
353+
fcasts <- list()
354+
fcasts$points <- c()
355+
356+
for(currentobs in startobs:endobs)
357+
{
358+
print(paste("Current obs =", currentobs))
359+
datachunk <- get_obs(dataset, currentobs, trainobs, testobs)
360+
361+
eval(parse(text=paste('datachunk$train %>%',
362+
transformation))) -> datachunk$train.transformed
363+
364+
h <- testobs
365+
366+
# for naive, snaive and meanf models
367+
# the model already returns the forecasts, no separate forecast step
368+
# is needed
369+
if(any(startsWith(model, c("naive()", "snaive()", "meanf()"))))
370+
{
371+
model <- sub("()", paste("(h=", h, ")", sep=""), model, fixed=TRUE)
372+
}
373+
374+
eval(parse(text=paste('datachunk$train.transformed %>%', model))) -> fit
375+
376+
fcast <- NULL
377+
378+
# for naive, snaive and meanf models
379+
# the model already returns the forecasts, no separate forecast step
380+
# is needed
381+
if(any(startsWith(model, c("naive(", "snaive(", "meanf("))))
382+
{
383+
chunk.fcast <- fit
384+
} else
385+
{
386+
fourier.terms <- NULL
387+
388+
if(!is.null(xreg))
389+
{
390+
eval(parse(text=paste('datachunk$test %>%', xreg))) -> fourier.terms
391+
}
392+
393+
fit %>% forecast(h=h, xreg=fourier.terms) -> chunk.fcast
394+
}
395+
396+
fcasts$points <- c(fcasts$points, chunk.fcast$mean)
397+
398+
gc()
399+
}
400+
401+
gc()
402+
403+
fcasts$points <- ts(fcasts$points, start=trainobs/frequency(dataset), frequency = frequency(dataset))
404+
testpoints <- get_obs(dataset, trainobs, NULL, NULL)$train
405+
406+
fcasts$accuracy <- accuracy(fcasts$points, testpoints)
407+
408+
return(fcasts)
409+
}

Diff for: mem_profile.R

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
library(lineprof)
1+
library(profvis)
22

33
source("funcs.R")
44
prof.2hrsph3 <- lineprof(
@@ -9,7 +9,7 @@ prof.2hrsph3 <- lineprof(
99
testdays = 1,
1010
xreg = NULL,
1111
max.iterations = 2))
12-
shine(prof.2hrsph3)
12+
print(prof.2hrsph3)
1313

1414
prof.ph3 <- lineprof(
1515
fullforecast.serial(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="ML")',
@@ -19,4 +19,28 @@ prof.ph3 <- lineprof(
1919
testdays = 1,
2020
xreg = NULL,
2121
max.iterations = 2))
22-
shine(prof.ph3)
22+
print(prof.ph3)
23+
24+
prof.ph3.serial.obs <- profvis(
25+
fullforecast.serial.obs(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="ML")',
26+
dataset = datasets[['ph3']]$series,
27+
transformation = 'identity()',
28+
trainobs = 12, #frequency(datasets[['ph3']]$series),
29+
testobs = 1,
30+
xreg = NULL,
31+
max.iterations = 0)
32+
)
33+
34+
print(prof.ph3.serial.obs)
35+
36+
37+
prof.2hrsph3.serial.obs <- profvis(
38+
fullforecast.serial.obs(model = 'Arima(order=c(1, 0, 0), seasonal=c(1, 0, 0), method="ML")',
39+
dataset = datasets[['2hrs ph3']]$series,
40+
transformation = 'identity()',
41+
trainobs = 12, #frequency(datasets[['ph3']]$series),
42+
testobs = 1,
43+
xreg = NULL,
44+
max.iterations = 0)
45+
)
46+
print(prof.2hrsph3.serial.obs)

0 commit comments

Comments
 (0)