'update'
[talweg.git] / pkg / R / Forecast.R
1 #' Forecast
2 #'
3 #' Forecast encapsulation as a list (days where prediction occur) of lists (components).
4 #'
5 #' The private field .pred is a list where each cell contains the predicted variables for
6 #' a period of time of H<=24 hours, from hour P+1 until P+H, where P+1 is taken right
7 #' after the end of the period designated by \code{getIndexInData()}. In other terms,
8 #' \code{forecast$getForecast(i)} return forecasts for \code{data$getSerie(i+1)}.
9 #' Each cell .pred[[i]] is itself a list containing three slots, as described in the
10 #' 'field' section.
11 #'
12 #' @usage f <- Forecast$new(dates)
13 #'
14 #' @field .pred List with
15 #' \itemize{
16 #' \item serie: the forecasted serie
17 #' \item params: corresponding list of parameters (weights, neighbors...)
18 #' \item index_in_data: corresponding index in data object
19 #' }
20 #' @field .dates vector of (integer) day indices where forecast occurs
21 #'
22 #' @section Methods:
23 #' \describe{
24 #' \item{\code{initialize(dates)}}{
25 #' Initialize a Forecast object with a series of date indices.}
26 #' \item{\code{getSize()}}{
27 #' Return number of individual forecasts.}
28 #' \item{\code{append(forecast, params, index_in_data)}}{
29 #' Acquire an individual forecast, with its (optimized) parameters and the
30 #' corresponding index in the dataset.}
31 #' \item{\code{getDates()}}{
32 #' Get dates where forecast occurs.}
33 #' \item{\code{getForecast(index)}}{
34 #' Get forecasted serie at specified index.}
35 #' \item{\code{getParams(index)}}{
36 #' Get parameters at specified index (for 'Neighbors' method).}
37 #' \item{\code{getIndexInData(index)}}{
38 #' Get index in data which corresponds to current forecast.}
39 #' }
40 #'
41 #' @docType class
42 #' @format R6 class
43 #'
44 Forecast = R6::R6Class("Forecast",
45 private = list(
46 .pred = list(),
47 .dates = integer(0) #store dates as integers (from 1970-01-01)
48 ),
49 public = list(
50 initialize = function(dates)
51 {
52 private$.dates <- dates
53 invisible(self)
54 },
55 getSize = function()
56 length(private$.pred)
57 ,
58 append = function(forecast, params, index_in_data)
59 {
60 private$.pred[[length(private$.pred)+1]] <-
61 list("forecast"=forecast, "params"=params, "index_in_data"=index_in_data)
62 },
63 getDates = function()
64 as.Date( private$.dates, origin="1970-01-01" )
65 ,
66 getForecast = function(index)
67 {
68 if (is(index,"Date"))
69 index = match(index, private$.dates)
70 private$.pred[[index]]$forecast
71 },
72 getParams = function(index)
73 {
74 if (is(index,"Date"))
75 index = match(index, private$.dates)
76 private$.pred[[index]]$params
77 },
78 getIndexInData = function(index)
79 {
80 if (is(index,"Date"))
81 index = match(index, private$.dates)
82 private$.pred[[index]]$index_in_data
83 }
84 )
85 )