TODO: unit tests for simil days
[talweg.git] / pkg / R / F_Average.R
... / ...
CommitLineData
1#' Average Forecaster
2#'
3#' Pointwise average of all the series of the same day of week in the past.
4#'
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
11#' @format R6 class, inherits Forecaster
12#' @aliases F_Average
13#'
14AverageForecaster = R6::R6Class("AverageForecaster",
15 inherit = Forecaster,
16
17 public = list(
18 predictShape = function(data, today, memory, horizon, ...)
19 {
20 avg = rep(0., horizon)
21 first_day = max(1, today-memory)
22 index = today-7 + 1
23 nb_no_na_series = 0
24 repeat
25 {
26 {
27 serie_on_horizon = data$getCenteredSerie(index)[1:horizon]
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
37 }
38 avg / nb_no_na_series
39 }
40 )
41)