update documention, fix package to compete with 'method Bruno'
[talweg.git] / pkg / R / Data.R
CommitLineData
f17665c7 1#' Data
3d69ff21 2#'
4f3fdbb8 3#' Data encapsulation, in the form of a few lists (time-series + exogenous variables).
3d69ff21 4#'
d2ab47a7
BA
5#' The private field .tv is a list where each cell contains the hourly variables for a
6#' period of time of 24 hours, from 1am to next midnight. The other lists contain
7#' informations on series' levels and exogenous variables (both measured and predicted).
a66a84b5 8#'
4e821712 9#' @usage # Data$new()
689aa1d3 10#'
d2ab47a7 11#' @field .tv List of "time-values"; in each cell:
2057c793 12#' \itemize{
3d69ff21 13#' \item time: vector of times
d2ab47a7 14#' \item serie: (measured) serie
2057c793 15#' }
d2ab47a7
BA
16#' @field .level Vector of measured levels
17#' @field .level_hat Vector of predicted levels
18#' @field .exo List of measured exogenous variables, cell = numerical vector.
19#' @field .exo_hat List of predicted exogenous variables, cell = numerical vector.
f17665c7 20#'
98e958ca
BA
21#' @section Methods:
22#' \describe{
23#' \item{\code{getSize()}}{
102bcfda 24#' Number of series in dataset.}
d2ab47a7 25#' \item{\code{append(time, value, level_hat, exo, exo_hat)}}{
3ddf1c12
BA
26#' Measured data for given vector of times + exogenous predictions from
27#' last midgnight.}
98e958ca 28#' \item{\code{getTime(index)}}{
102bcfda 29#' Times (vector) at specified index.}
2057c793 30#' \item{\code{getSerie(index)}}{
102bcfda 31#' Serie (centered+level) at specified index.}
2057c793 32#' \item{\code{getSeries(indices)}}{
102bcfda 33#' Series at specified indices (in columns).}
d2ab47a7
BA
34#' \item{\code{getLevel(index)}}{
35#' Measured level at specified index.}
36#' \item{\code{getLevelHat(index)}}{
37#' Predicted level at specified index.}
38#' \item{\code{getCenteredSerie(index)}}{
39#' Centered serie at specified index.}
40#' \item{\code{getCenteredSeries(indices)}}{
41#' Centered series at specified indices (in columns).}
2057c793 42#' \item{\code{getExo(index)}}{
102bcfda 43#' Measured exogenous variables at specified index.}
d2ab47a7
BA
44#' \item{\code{getExoHat(index)}}{
45#' Predicted exogenous variables at specified index.}
98e958ca 46#' }
546b0cb6 47#'
102bcfda
BA
48#' @docType class
49#' @format R6 class
50#'
25b75559 51Data = R6::R6Class("Data",
35eb1443 52 private = list(
d2ab47a7
BA
53 .tv = list(),
54 .level = vector("double",0),
55 .level_hat = vector("double",0),
56 .exo = list(),
57 .exo_hat = list()
35eb1443 58 ),
f17665c7 59 public = list(
3d69ff21 60 getSize = function()
d2ab47a7 61 length(private$.tv)
35eb1443 62 ,
d2ab47a7 63 append = function(time=NULL, value=NULL, level_hat=NULL, exo=NULL, exo_hat=NULL)
72b9c501 64 {
d2ab47a7
BA
65 if (!is.null(time) && !is.null(value))
66 {
67 L = length(private$.tv)
68 if (L == 0 || strftime( tail(private$.tv[[L]]$time,1),
69 format="%H:%M:%S", tz="GMT" ) == "00:00:00")
70 {
71 # Append a new cell
72 private$.tv[[L+1]] <- list("time"=time, "serie"=value)
73 }
74 else
75 {
76 # Complete current cell
77 private$.tv[[L]]$time = c(private$.tv[[L]]$time, time)
78 private$.tv[[L]]$serie = c(private$.tv[[L]]$serie, value)
79 }
80 }
81 if (strftime( tail(private$.tv[[length(private$.tv)]]$time,1),
82 format="%H:%M:%S", tz="GMT" ) == "00:00:00")
83 {
84 private$.level = c(private$.level,
85 mean(private$.tv[[length(private$.tv)]]$serie, na.rm=TRUE))
86 }
87 if (!is.null(level_hat))
88 private$.level_hat = c(private$.level_hat, level_hat)
89 if (!is.null(exo))
90 private$.exo[[length(private$.exo)+1]] = exo
91 if (!is.null(exo_hat))
92 private$.exo_hat[[length(private$.exo_hat)+1]] = exo_hat
a66a84b5 93 },
3d69ff21 94 getTime = function(index)
a66a84b5
BA
95 {
96 index = dateIndexToInteger(index, self)
d2ab47a7 97 private$.tv[[index]]$time
a66a84b5 98 },
d2ab47a7 99 getSerie = function(index)
a66a84b5
BA
100 {
101 index = dateIndexToInteger(index, self)
d2ab47a7 102 private$.tv[[index]]$serie
a66a84b5 103 },
d2ab47a7
BA
104 getSeries = function(indices)
105 sapply(indices, function(i) self$getSerie(i))
98e958ca 106 ,
2057c793 107 getLevel = function(index)
a66a84b5
BA
108 {
109 index = dateIndexToInteger(index, self)
d2ab47a7 110 private$.level[index]
a66a84b5 111 },
d2ab47a7 112 getLevelHat = function(index)
a66a84b5
BA
113 {
114 index = dateIndexToInteger(index, self)
d2ab47a7 115 private$.level_hat[index]
a66a84b5 116 },
d2ab47a7 117 getCenteredSerie = function(index)
2057c793
BA
118 {
119 index = dateIndexToInteger(index, self)
d2ab47a7 120 private$.tv[[index]]$serie - private$.level[index]
2057c793 121 },
d2ab47a7
BA
122 getCenteredSeries = function(indices)
123 sapply(indices, function(i) self$getCenteredSerie(i))
124 ,
2057c793 125 getExo = function(index)
a66a84b5
BA
126 {
127 index = dateIndexToInteger(index, self)
d2ab47a7 128 private$.exo[[index]]
a66a84b5 129 },
d2ab47a7
BA
130 getExoHat = function(index)
131 {
132 index = dateIndexToInteger(index, self)
133 private$.exo_hat[[index]]
134 }
3d69ff21
BA
135 )
136)