f1b92ce7d6f46bb0d9f59d2ff924cc0711c3c0af
[talweg.git] / pkg / R / Forecast.R
1 #' Forecast
2 #'
3 #' Forecast encapsulation
4 #'
5 #' @docType class
6 #' @importFrom R6 R6Class
7 #'
8 #' @field .pred List with \itemize{
9 #' \item serie: forecasted serie
10 #' \item params: corresponding list of parameters (weights, neighbors...)
11 #' \item index: corresponding index in data object}
12 #' @field .dates vector of day indices where forcast occurs
13 #'
14 #' @section Methods: \describe{
15 #' \item{\code{initialize(dates)}}
16 #' {Initialize a Forecast object with a series of date indices.}
17 #' \item{\code{predictSerie(today,memory,horizon,...)}}
18 #' {Predict a new serie of \code{horizon} values at day index \code{today} using \code{memory}
19 #' days in the past.} TODO: continue #######################################
20 #' \item{\code{predictShape(today,memory,horizon,...)}}
21 #' {Predict a new shape of \code{horizon} values at day index \code{today} using \code{memory}
22 #' days in the past.}
23 #' \item{\code{getParameters()}}
24 #' {Return (internal) parameters.} }
25 Forecast = R6::R6Class("Forecast",
26 private = list(
27 .pred = list(),
28 .dates = c()
29 ),
30 public = list(
31 initialize = function(dates)
32 initializeForecast(self, private, dates)
33 ,
34 getSize = function()
35 getSizeForecast(private)
36 ,
37 append = function(new_serie, new_params, new_index)
38 appendForecast(private, new_serie, new_params, new_index)
39 ,
40 getDates = function()
41 getDatesForecast(private)
42 ,
43 getSerie = function(index)
44 getSerieForecast(private, index)
45 ,
46 getParams = function(index)
47 getParamsForecast(private, index)
48 ,
49 getIndexInData = function(index)
50 getIndexInDataForecast(private, index)
51 )
52 )
53
54 #' Initialize empty Forecast object
55 #'
56 #' @param o Object of class Forecast
57 #' @param private List of private members in o
58 #' @param dates vector of dates where forecast occurs
59 initializeForecast = function(o, private, dates)
60 {
61 private$.dates <- dates
62 invisible(o)
63 }
64
65 #' Number of individual forecasts"
66 #'
67 #' @inheritParams initializeForecast
68 getSizeForecast = function(private)
69 length(private$.pred)
70
71 #' Obtain a new pair (serie, params)"
72 #'
73 #' @inheritParams initializeForecast
74 #' @param new_serie Values of a new serie
75 #' @param new_params Associated (optimized) parameters
76 #' @param new_index_in_data Corresponding index in data
77 appendForecast = function(private, new_serie, new_params, new_index_in_data)
78 {
79 private$.pred[[length(private$.pred)+1]] <-
80 list("serie"=new_serie, "params"=new_params, "index_in_data"=new_index_in_data)
81 }
82
83 #' Dates where prediction occurs
84 #'
85 #' inheritParams initializeForecast
86 getDatesForecast = function(private)
87 private$.dates
88
89 #' Serie values at specified index"
90 #'
91 #' @inheritParams initializeForecast
92 #' @param index Return value at this index
93 getSerieForecast = function(index)
94 {
95 if (is(index,"Date"))
96 index = match(index, private$.dates)
97 private$.pred[[index]]$serie
98 }
99
100 #' Params at specified index"
101 #'
102 #' @inheritParams getSerieForecast
103 getParamsForecast = function(index)
104 {
105 if (is(index,"Date"))
106 index = match(index, private$.dates)
107 private$.pred[[index]]$params
108 }
109
110 #' (day) Index in data where prediction took place"
111 #'
112 #' @inheritParams getSerieForecast
113 getIndexInDataForecast = function(index)
114 {
115 if (is(index,"Date"))
116 index = match(index, private$.dates)
117 private$.pred[[index]]$index_in_data
118 }