revise package structure: always predict from 1am to horizon, dataset not cut at...
[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#'
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
BA
25 nb_no_na_series = 0
26 repeat
3d69ff21 27 {
d2ab47a7
BA
28 index = index - 7
29 if (index < first_day)
30 break
31 serie_on_horizon = data$getCenteredSerie(index)[predict_from:horizon]
09cf9c19
BA
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
d2ab47a7 36 }
3d69ff21 37 }
09cf9c19 38 avg / nb_no_na_series
3d69ff21
BA
39 }
40 )
41)