'update'
[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#'
689aa1d3
BA
10#' @usage f <- AverageForecaster$new(pjump)
11#'
102bcfda 12#' @docType class
c4c329f6 13#' @format R6 class, inherits Forecaster
3ddf1c12 14#' @aliases F_Average
546b0cb6 15#'
25b75559 16AverageForecaster = R6::R6Class("AverageForecaster",
a66a84b5 17 inherit = Forecaster,
3d69ff21 18
25b75559 19 public = list(
98e958ca 20 predictShape = function(data, today, memory, horizon, ...)
3d69ff21
BA
21 {
22 avg = rep(0., horizon)
09cf9c19
BA
23 first_day = max(1, today-memory)
24 index = today-7 + 1
25 nb_no_na_series = 0
26 repeat
3d69ff21 27 {
09cf9c19 28 {
98e958ca 29 serie_on_horizon = data$getCenteredSerie(index)[1:horizon]
09cf9c19
BA
30 index = index - 7
31 };
32 if (!any(is.na(serie_on_horizon)))
33 {
34 avg = avg + serie_on_horizon
35 nb_no_na_series = nb_no_na_series + 1
36 };
37 if (index < first_day)
38 break
3d69ff21 39 }
09cf9c19 40 avg / nb_no_na_series
3d69ff21
BA
41 }
42 )
43)