add realtime option, slightly refactor data acquisition
[talweg.git] / pkg / R / F_Persistence.R
1 #' @include Forecaster.R
2 #'
3 #' Persistence Forecaster
4 #'
5 #' Return the last centered (similar) day curve.
6 #' Inherits \code{\link{Forecaster}}
7 #'
8 PersistenceForecaster = R6::R6Class("PersistenceForecaster",
9 inherit = Forecaster,
10
11 public = list(
12 predictShape = function(data, today, memory, horizon, ...)
13 {
14 # Return centered last (similar) day curve, avoiding NAs until memory is run
15 first_day = max(1, today-memory)
16 same_day = ifelse(hasArg("same_day"), list(...)$same_day, TRUE)
17 realtime = ifelse(hasArg("realtime"), list(...)$realtime, FALSE)
18 # If 'same_day', get the last known future of similar day: -7 + 1 == -6
19 index = today - ifelse(same_day,6,0)
20 repeat
21 {
22 {
23 last_serie =
24 data$getCenteredSerie(index,hat=(index==today && realtime))[1:horizon]
25 index = index - ifelse(same_day,7,1)
26 };
27 if (!any(is.na(last_serie)))
28 return (last_serie);
29 if (index < first_day)
30 return (NA)
31 }
32 }
33 )
34 )