R CMD check OK
[talweg.git] / pkg / R / F_Persistence.R
CommitLineData
25b75559 1#' Persistence Forecaster
3d69ff21 2#'
c4c329f6 3#' Look for the most recent similar day in the past, and return its corresponding curve.
c4c329f6 4#'
102bcfda
BA
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#'
4e821712 11#' @usage # PersistenceForecaster$new(pjump)
689aa1d3 12#'
102bcfda 13#' @docType class
c4c329f6 14#' @format R6 class, inherits Forecaster
3ddf1c12 15#' @aliases F_Persistence
546b0cb6 16#'
25b75559 17PersistenceForecaster = R6::R6Class("PersistenceForecaster",
a66a84b5 18 inherit = Forecaster,
3d69ff21 19
25b75559 20 public = list(
98e958ca 21 predictShape = function(data, today, memory, horizon, ...)
3d69ff21 22 {
e5aa669a 23 # Return centered last (similar) day curve, avoiding NAs until memory is run
09cf9c19 24 first_day = max(1, today-memory)
e5aa669a
BA
25 same_day = ifelse(hasArg("same_day"), list(...)$same_day, TRUE)
26 # If 'same_day', get the last known future of similar day: -7 + 1 == -6
27 index = today - ifelse(same_day,6,0)
09cf9c19
BA
28 repeat
29 {
30 {
2057c793 31 last_serie = data$getCenteredSerie(index)[1:horizon]
e5aa669a 32 index = index - ifelse(same_day,7,1)
09cf9c19 33 };
e5aa669a
BA
34 if (!any(is.na(last_serie)))
35 return (last_serie);
09cf9c19
BA
36 if (index < first_day)
37 return (NA)
38 }
3d69ff21
BA
39 }
40 )
41)