TODO: unit tests for simil days
[talweg.git] / pkg / R / F_Average.R
CommitLineData
25b75559 1#' Average Forecaster
3d69ff21 2#'
102bcfda 3#' Pointwise average of all the series of the same day of week in the past.
c4c329f6 4#'
102bcfda
BA
5#' For example, if the current day (argument "today") is a tuesday, then all series
6#' corresponding to wednesdays in the past (until the beginning or memory limit) are
7#' averaged to provide a smooth prediction. This forecast will most of the time be wrong,
8#' but will also look plausible enough.
9#'
10#' @docType class
c4c329f6 11#' @format R6 class, inherits Forecaster
3ddf1c12 12#' @aliases F_Average
546b0cb6 13#'
25b75559 14AverageForecaster = R6::R6Class("AverageForecaster",
a66a84b5 15 inherit = Forecaster,
3d69ff21 16
25b75559 17 public = list(
98e958ca 18 predictShape = function(data, today, memory, horizon, ...)
3d69ff21
BA
19 {
20 avg = rep(0., horizon)
09cf9c19
BA
21 first_day = max(1, today-memory)
22 index = today-7 + 1
23 nb_no_na_series = 0
24 repeat
3d69ff21 25 {
09cf9c19 26 {
98e958ca 27 serie_on_horizon = data$getCenteredSerie(index)[1:horizon]
09cf9c19
BA
28 index = index - 7
29 };
30 if (!any(is.na(serie_on_horizon)))
31 {
32 avg = avg + serie_on_horizon
33 nb_no_na_series = nb_no_na_series + 1
34 };
35 if (index < first_day)
36 break
3d69ff21 37 }
09cf9c19 38 avg / nb_no_na_series
3d69ff21
BA
39 }
40 )
41)