update documention, fix package to compete with 'method Bruno'
[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 5#' For example, if the current day (argument "today") is a tuesday, then all series
4f3fdbb8
BA
6#' corresponding to tuesday in the past (until the beginning or memory limit) -- and in
7#' the future if 'opera' is FALSE -- are averaged to provide a smooth prediction. This
8#' forecast will most of the time be wrong, but will also look plausible enough.
102bcfda 9#'
4e821712 10#' @usage # AverageForecaster$new(pjump)
689aa1d3 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(
d2ab47a7 20 predictShape = function(data, today, memory, predict_from, horizon, ...)
3d69ff21 21 {
d2ab47a7 22 avg = rep(0., (horizon-predict_from+1))
09cf9c19 23 first_day = max(1, today-memory)
d2ab47a7 24 index <- today
09cf9c19 25 nb_no_na_series = 0
638f27f4 26 opera = ifelse(hasArg("opera"), list(...)$opera, FALSE)
09cf9c19 27 repeat
3d69ff21 28 {
d2ab47a7
BA
29 index = index - 7
30 if (index < first_day)
31 break
32 serie_on_horizon = data$getCenteredSerie(index)[predict_from:horizon]
09cf9c19
BA
33 if (!any(is.na(serie_on_horizon)))
34 {
35 avg = avg + serie_on_horizon
36 nb_no_na_series = nb_no_na_series + 1
d2ab47a7 37 }
3d69ff21 38 }
638f27f4
BA
39 if (!opera)
40 {
41 # The same, in the future
42 index <- today
43 repeat
44 {
45 index = index + 7
46 if (index > data$getSize())
47 break
48 serie_on_horizon = data$getCenteredSerie(index)[predict_from:horizon]
49 if (!any(is.na(serie_on_horizon)))
50 {
51 avg = avg + serie_on_horizon
52 nb_no_na_series = nb_no_na_series + 1
53 }
54 }
55 }
09cf9c19 56 avg / nb_no_na_series
3d69ff21
BA
57 }
58 )
59)