'update'
[talweg.git] / pkg / R / Forecaster.R
CommitLineData
25b75559 1#' Forecaster
e030a6e3 2#'
102bcfda 3#' Forecaster (abstract class, implemented by all forecasters).
e030a6e3 4#'
102bcfda
BA
5#' A Forecaster object encapsulates parameters (which can be of various kinds, for
6#' example "Neighbors" method stores informations about the considered neighborhood for
7#' the current prediction task) and one main function: \code{predictSerie()}. This last
8#' function (by default) calls \code{predictShape()} to get a forecast of a centered
4f3fdbb8
BA
9#' serie, and then calls the "jump prediction" function if it's provided -- see "field"
10#' section -- to adjust it based on the last observed values. The main method in derived
11#' forecasters is \code{predictShape()}; see 'Methods' section.
a66a84b5 12#'
4e821712 13#' @usage # Forecaster$new(pjump) #warning: predictShape() is unimplemented
689aa1d3 14#'
102bcfda
BA
15#' @field .params List of computed parameters (if applicable).
16#' @field .pjump Function: how to predict the jump at day interface? The arguments of
17#' this function are -- in this order:
18#' \itemize{
4f3fdbb8
BA
19#' \item data: object output of \code{getData()},
20#' \item today: index of the current day in data (known until predict_from-1),
21#' \item memory: number of days to use in the past (including today),
22#' \item predict_from: first time step to predict (in [1,24])
23#' \item horizon: last time step to predict (in [predict_from,24]),
24#' \item params: optimized parameters in the main method \code{predictShape()},
25#' \item ...: additional arguments.
102bcfda
BA
26#' }
27#' .pjump returns an estimation of the jump after the last observed value.
25b75559 28#'
98e958ca
BA
29#' @section Methods:
30#' \describe{
8f5671db
BA
31#' \item{\code{initialize(pjump)}}{
32#' Initialize a Forecaster object with a jump prediction function.}
4f3fdbb8
BA
33#' \item{\code{predictSerie(data,today,memory,predict_from,horizon,...)}}{
34#' Predict the next curve (at index today) from predict_from to horizon (hours), using
102bcfda 35#' \code{memory} days in the past.}
4f3fdbb8
BA
36#' \item{\code{predictShape(data,today,memory,predict_from,horizon,...)}}{
37#' Predict the shape of the next curve (at index today) from predict_from to horizon
38#' (hours), using \code{memory} days in the past.}
98e958ca 39#' \item{\code{getParameters()}}{
546b0cb6
BA
40#' Return (internal) parameters.}
41#' }
42#'
102bcfda
BA
43#' @docType class
44#' @format R6 class
45#'
25b75559
BA
46Forecaster = R6::R6Class("Forecaster",
47 private = list(
a66a84b5 48 .params = list(),
a66a84b5 49 .pjump = NULL
e030a6e3 50 ),
25b75559 51 public = list(
98e958ca 52 initialize = function(pjump)
a66a84b5 53 {
a66a84b5
BA
54 private$.pjump <- pjump
55 invisible(self)
56 },
d2ab47a7 57 predictSerie = function(data, today, memory, predict_from, horizon, ...)
a66a84b5
BA
58 {
59 # Parameters (potentially) computed during shape prediction stage
4f3fdbb8 60 predicted_shape <- self$predictShape(data,today,memory,predict_from,horizon,...)
8ab64202 61
a3344f75 62 if (is.na(predicted_shape[1]))
8ab64202
BA
63 return (NA)
64
8f5671db
BA
65 predicted_delta <- private$.pjump(data, today, memory, predict_from,
66 horizon, private$.params, first_pred=predicted_shape[1], ...)
d2ab47a7 67
8f5671db 68 # Predicted shape is aligned on the end of current day + jump
d2ab47a7 69 c( data$getSerie(today)[if (predict_from>=2) 1:(predict_from-1) else c()],
8f5671db
BA
70 (predicted_shape - predicted_shape[1]) + #shape with first_pred = 0
71 ifelse(predict_from>=2, #last observed value
72 data$getSerie(today)[predict_from-1], tail(data$getSerie(today-1),1)) +
73 predicted_delta ) #jump
a66a84b5 74 },
d2ab47a7 75 predictShape = function(data, today, memory, predict_from, horizon, ...)
5d83d815 76 NULL #empty default implementation: to implement in inherited classes
25b75559 77 ,
e030a6e3 78 getParameters = function()
a66a84b5 79 private$.params
e030a6e3
BA
80 )
81)