intermediate: R6, too slow
[talweg.git] / pkg / R / Forecast.R
CommitLineData
25b75559 1#' Forecast
3d69ff21 2#'
25b75559 3#' Forecast encapsulation
3d69ff21
BA
4#'
5#' @field pred List with
6#' \itemize{
7#' \item serie: forecasted serie
8#' \item params: corresponding list of parameters (weights, neighbors...)
9#' \item index: corresponding index in data object
10#' }
11#'
25b75559
BA
12#' @docType class
13#' @importFrom R6 R6Class
14#'
15#' @export
16Forecast = R6::R6Class("Forecast",
17 private = list(
18 .pred = "list",
19 .dates = "Date"
3d69ff21 20 ),
25b75559
BA
21 public = list(
22 initialize = function(dates)
23 initialize(self, private, dates)
24 ,
1e20780e 25 getSize = function()
25b75559
BA
26 getSize(private)
27 ,
3d69ff21 28 append = function(new_serie, new_params, new_index)
25b75559
BA
29 append(private, new_serie, new_params, new_index)
30 ,
31 getDates = function()
32 getDates(private)
33 ,
3d69ff21 34 getSerie = function(index)
25b75559
BA
35 getSerie(private, index)
36 ,
3d69ff21 37 getParams = function(index)
25b75559
BA
38 getParams(private, index)
39 ,
3d69ff21 40 getIndexInData = function(index)
25b75559 41 getIndexInData(private, index)
3d69ff21
BA
42 )
43)
25b75559
BA
44
45#' Initialize empty Forecast object
46#'
47#' @param o Object of class Forecast
48#' @param private List of private members in o
49#' @param dates vector of dates where forecast occurs
50initialize = function(o, private, dates)
51{
52 private$.dates <<- dates
53 private$.pred <<- list()
54 invisible(o)
55}
56
57#' Number of individual forecasts"
58#'
59#' @inheritParams initialize
60getSize = function(private)
61 length(private$.pred)
62
63#' Obtain a new pair (serie, params)"
64#'
65#' @inheritParams initialize
66append = function(new_serie, new_params, new_index_in_data)
67{
68 private$.pred[[length(private$.pred)+1]] <<-
69 list("serie"=new_serie, "params"=new_params, "index_in_data"=new_index_in_data)
70}
71
72#' Dates where prediction occurs
73#'
74#' inheritParams initialize
75getDates = function(private)
76 private$.dates
77
78#' Serie values at specified index"
79#'
80#' @inheritParams initialize
81#' @param index Return value at this index
82getSerie = function(index)
83{
84 if (is(index,"Date"))
85 index = match(index, private$.dates)
86 private$.pred[[index]]$serie
87}
88
89#' Params at specified index"
90#'
91#' @inheritParams getSerie
92getParams = function(index)
93{
94 if (is(index,"Date"))
95 index = match(index, private$.dates)
96 private$.pred[[index]]$params
97}
98
99#' (day) Index in data where prediction took place"
100#'
101#' @inheritParams getSerie
102getIndexInData = function(index)
103{
104 if (is(index,"Date"))
105 index = match(index, private$.dates)
106 private$.pred[[index]]$index_in_data
107}