intermediate: R6, too slow
[talweg.git] / pkg / R / Forecaster.R
CommitLineData
25b75559 1#' Forecaster
e030a6e3 2#'
25b75559 3#' Forecaster (abstract class, implemented by all forecasters)
e030a6e3
BA
4#'
5#' @field params List of computed parameters, for post-run analysis (dev)
6#' @field data Dataset, object of class Data
7#' @field pjump Function: how to predict the jump at day interface ?
25b75559
BA
8#'
9#' @docType class
10#' @importFrom R6 R6Class
11Forecaster = R6::R6Class("Forecaster",
12 private = list(
13 .params = "list",
14 .data = "Data",
15 .pjump = "function"
e030a6e3 16 ),
25b75559
BA
17 public = list(
18 initialize = function(data, pjump)
19 initialize(self, private, data, pjump)
20 ,
21 predictSerie = function(today, memory, horizon, ...)
22 predictSerie(private, today, memory, horizon, ...)
23 ,
e030a6e3 24 predictShape = function(today, memory, horizon, ...)
25b75559
BA
25 predictShape(private, today, memory, horizon, ...)
26 ,
e030a6e3 27 getParameters = function()
25b75559 28 getParameters(private)
e030a6e3
BA
29 )
30)
25b75559
BA
31
32#' Initialize (generic) Forecaster object
33#'
34#' @param o Object of class Forecaster
35#' @param private List of private members in o
36#' @param data Object of class Data
37#' @param pjump Function to predict jump
38initialize = function(o, private, data, pjump)
39{
40 .params <<- list()
41 .data <<- data
42 .pjump <<- pjump
43 invisible(o)
44}
45
46#' Obtain a new forecasted time-serie
47#'
48#' @inheritParams initialize
49#' @param today Index for current prediction
50#' @param memory Depth in data (in days)
51#' @param horizon Number of hours to forecast
52predictSerie = function(private, today, memory, horizon, ...)
53{
54 # Parameters (potentially) computed during shape prediction stage
55 predicted_shape = predictShape(today, memory, horizon, ...)
56 predicted_delta = private$.pjump(private$.data, today, memory, horizon, params, ...)
57 # Predicted shape is aligned it on the end of current day + jump
58 predicted_shape + tail(private$.data$getSerie(today),1) - predicted_shape[1] + predicted_delta
59}
60
61#' Shape prediction (centered curve)
62#'
63#' @inheritParams predictSerie
64predictShape = function(private, today, memory, horizon, ...)
65 #empty default implementation: to implement in inherited classes
66
67#' Get parameters list
68#'
69#' @inheritParams initialize
70getParameters = function(private)
71 private$.params