| 1 | #' @include Forecaster.R |
| 2 | #' |
| 3 | #' @title Average Forecaster |
| 4 | #' |
| 5 | #' @description Return the (pointwise) average of the all the (similar) centered day curves |
| 6 | #' in the past. Inherits \code{\link{Forecaster}} |
| 7 | AverageForecaster = setRefClass( |
| 8 | Class = "AverageForecaster", |
| 9 | contains = "Forecaster", |
| 10 | |
| 11 | methods = list( |
| 12 | initialize = function(...) |
| 13 | { |
| 14 | callSuper(...) |
| 15 | }, |
| 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 | }, |
| 32 | predictShape = function(today, memory, horizon, ...) |
| 33 | { |
| 34 | avg = rep(0., horizon) |
| 35 | first_day = max(1, today-memory) |
| 36 | index = today-7 + 1 |
| 37 | nb_no_na_series = 0 |
| 38 | repeat |
| 39 | { |
| 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 |
| 51 | } |
| 52 | avg / nb_no_na_series |
| 53 | } |
| 54 | ) |
| 55 | ) |