d0b40b4ba69bd50b8c3f6c65ce49b7e5a432390b
[talweg.git] / pkg / R / F_Average.R
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 #' @usage # AverageForecaster$new(pjump)
11 #'
12 #' @docType class
13 #' @format R6 class, inherits Forecaster
14 #' @aliases F_Average
15 #'
16 AverageForecaster = R6::R6Class("AverageForecaster",
17 inherit = Forecaster,
18
19 public = list(
20 predictShape = function(data, today, memory, horizon, ...)
21 {
22 avg = rep(0., horizon)
23 first_day = max(1, today-memory)
24 index = today-7 + 1
25 nb_no_na_series = 0
26 repeat
27 {
28 {
29 serie_on_horizon = data$getCenteredSerie(index)[1:horizon]
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
39 }
40 avg / nb_no_na_series
41 }
42 )
43 )