fix 2 tests out of 3; TODO: test forecasters
[talweg.git] / pkg / R / F_Average.R
CommitLineData
e030a6e3 1#' @include Forecaster.R
3d69ff21 2#'
e030a6e3 3#' @title Average Forecaster
3d69ff21 4#'
09cf9c19 5#' @description Return the (pointwise) average of the all the (similar) centered day curves
e030a6e3
BA
6#' in the past. Inherits \code{\link{Forecaster}}
7AverageForecaster = setRefClass(
8 Class = "AverageForecaster",
9 contains = "Forecaster",
3d69ff21
BA
10
11 methods = list(
12 initialize = function(...)
13 {
14 callSuper(...)
15 },
44a9990b
BA
16 predict = function(today, memory, horizon, ...)
17 {
18 predicted_shape = predictShape(today, memory, horizon, ...)
19 #Take care of never passing same_day==FALSE (when pjump == Persistence)
20 predicted_delta =
21 if (#as.character(substitute(pjump))=="Persistence" && #TODO: doesn't work
22 hasArg("same_day") && list(...)$same_day==FALSE)
23 {
24 args = list(...)
25 args$same_day = TRUE
26 do.call(pjump, append(list("today"=today,"memory"=memory,"horizon"=horizon), args))
27 }
28 else
29 pjump(data, today, memory, horizon, params, ...)
30 predicted_shape + tail(data$getSerie(today),1) - predicted_shape[1] + predicted_delta
31 },
e030a6e3 32 predictShape = function(today, memory, horizon, ...)
3d69ff21
BA
33 {
34 avg = rep(0., horizon)
09cf9c19
BA
35 first_day = max(1, today-memory)
36 index = today-7 + 1
37 nb_no_na_series = 0
38 repeat
3d69ff21 39 {
09cf9c19
BA
40 {
41 serie_on_horizon = data$getCenteredSerie(index)[1:horizon]
42 index = index - 7
43 };
44 if (!any(is.na(serie_on_horizon)))
45 {
46 avg = avg + serie_on_horizon
47 nb_no_na_series = nb_no_na_series + 1
48 };
49 if (index < first_day)
50 break
3d69ff21 51 }
09cf9c19 52 avg / nb_no_na_series
3d69ff21
BA
53 }
54 )
55)