intermediate: R6, too slow
[talweg.git] / pkg / R / Forecaster.R
1 #' Forecaster
2 #'
3 #' Forecaster (abstract class, implemented by all forecasters)
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 ?
8 #'
9 #' @docType class
10 #' @importFrom R6 R6Class
11 Forecaster = R6::R6Class("Forecaster",
12 private = list(
13 .params = "list",
14 .data = "Data",
15 .pjump = "function"
16 ),
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 ,
24 predictShape = function(today, memory, horizon, ...)
25 predictShape(private, today, memory, horizon, ...)
26 ,
27 getParameters = function()
28 getParameters(private)
29 )
30 )
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
38 initialize = 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
52 predictSerie = 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
64 predictShape = function(private, today, memory, horizon, ...)
65 #empty default implementation: to implement in inherited classes
66
67 #' Get parameters list
68 #'
69 #' @inheritParams initialize
70 getParameters = function(private)
71 private$.params