7035231f5f798e2db66cd50ac1c67421e054c38a
[talweg.git] / pkg / R / F_Persistence.R
1 #' Persistence Forecaster
2 #'
3 #' Look for the most recent similar day in the past, and return its corresponding curve.
4 #'
5 #' There are two variations, depending whether "similar day" means "same day in the week"
6 #' or "most recent day" (regardless of day type). The corresponding argument is named
7 #' "same_day": a value of TRUE implies the former interpretation (same day in week).
8 #' If the last similar day has missing values, the next one is searched, and so on until
9 #' one full serie is found (if no one is found, NA is returned).
10 #'
11 #' @usage # PersistenceForecaster$new(pjump)
12 #'
13 #' @docType class
14 #' @format R6 class, inherits Forecaster
15 #' @aliases F_Persistence
16 #'
17 PersistenceForecaster = R6::R6Class("PersistenceForecaster",
18 inherit = Forecaster,
19
20 public = list(
21 predictShape = function(data, today, memory, predict_from, horizon, ...)
22 {
23 # Return centered last (similar) day curve, avoiding NAs until memory is run
24 first_day = max(1, today-memory)
25 same_day = ifelse(hasArg("same_day"), list(...)$same_day, TRUE)
26 index <- today
27 repeat
28 {
29 # If 'same_day', get the last known future of similar day
30 index = index - ifelse(same_day,7,1)
31 if (index < first_day)
32 return (NA)
33 last_serie = data$getCenteredSerie(index)[predict_from:horizon]
34 if (!any(is.na(last_serie)))
35 return (last_serie)
36 }
37 }
38 )
39 )