work on doc
[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 #' "Similar day" means "same day in the week".
5 #'
6 #' @format R6 class, inherits Forecaster
7 #' @alias F_Persistence
8 #'
9 PersistenceForecaster = R6::R6Class("PersistenceForecaster",
10 inherit = Forecaster,
11
12 public = list(
13 predictShape = function(data, today, memory, horizon, ...)
14 {
15 # Return centered last (similar) day curve, avoiding NAs until memory is run
16 first_day = max(1, today-memory)
17 same_day = ifelse(hasArg("same_day"), list(...)$same_day, TRUE)
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 = data$getCenteredSerie(index)[1:horizon]
24 index = index - ifelse(same_day,7,1)
25 };
26 if (!any(is.na(last_serie)))
27 return (last_serie);
28 if (index < first_day)
29 return (NA)
30 }
31 }
32 )
33 )