after merg
[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#'
7c4b2952 5#' The private field .tvp is a list where each cell contains the hourly variables for a
d2ab47a7
BA
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#'
7c4b2952 11#' @field .tvp List of "time-values"; in each cell:
2057c793 12#' \itemize{
3d69ff21 13#' \item time: vector of times
d2ab47a7 14#' \item serie: (measured) serie
7c4b2952 15#' \item level_hat: predicted level for current day
2057c793 16#' }
d2ab47a7 17#' @field .level Vector of measured levels
d2ab47a7
BA
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)}}{
7c4b2952 37#' Predicted level vector at specified index (by hour).}
d2ab47a7
BA
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(
7c4b2952 53 .tvp = list(),
d2ab47a7 54 .level = vector("double",0),
d2ab47a7
BA
55 .exo = list(),
56 .exo_hat = list()
35eb1443 57 ),
f17665c7 58 public = list(
3d69ff21 59 getSize = function()
7c4b2952 60 length(private$.tvp)
35eb1443 61 ,
d2ab47a7 62 append = function(time=NULL, value=NULL, level_hat=NULL, exo=NULL, exo_hat=NULL)
72b9c501 63 {
7c4b2952 64 if (!is.null(time) && !is.null(value) && !is.null(level_hat))
d2ab47a7 65 {
7c4b2952
BA
66 L = length(private$.tvp)
67 if (L == 0 || strftime( tail(private$.tvp[[L]]$time,1),
d2ab47a7
BA
68 format="%H:%M:%S", tz="GMT" ) == "00:00:00")
69 {
70 # Append a new cell
7c4b2952 71 private$.tvp[[L+1]] <- list("time"=time, "serie"=value, "level_hat"=level_hat)
d2ab47a7
BA
72 }
73 else
74 {
75 # Complete current cell
7c4b2952
BA
76 private$.tvp[[L]]$time <- c(private$.tvp[[L]]$time, time)
77 private$.tvp[[L]]$serie <- c(private$.tvp[[L]]$serie, value)
78 private$.tvp[[L]]$level_hat <- c(private$.tvp[[L]]$levem_hat, level_hat)
d2ab47a7
BA
79 }
80 }
7c4b2952
BA
81 if (strftime( tail(private$.tvp[[length(private$.tvp)]]$time,1),
82 format="%H:%M:%S", tz="GMT" ) == "00:00:00")
d2ab47a7
BA
83 {
84 private$.level = c(private$.level,
7c4b2952 85 mean(private$.tvp[[length(private$.tvp)]]$serie, na.rm=TRUE))
d2ab47a7 86 }
d2ab47a7
BA
87 if (!is.null(exo))
88 private$.exo[[length(private$.exo)+1]] = exo
89 if (!is.null(exo_hat))
90 private$.exo_hat[[length(private$.exo_hat)+1]] = exo_hat
a66a84b5 91 },
3d69ff21 92 getTime = function(index)
a66a84b5
BA
93 {
94 index = dateIndexToInteger(index, self)
7c4b2952 95 private$.tvp[[index]]$time
a66a84b5 96 },
d2ab47a7 97 getSerie = function(index)
a66a84b5
BA
98 {
99 index = dateIndexToInteger(index, self)
7c4b2952 100 private$.tvp[[index]]$serie
a66a84b5 101 },
d2ab47a7
BA
102 getSeries = function(indices)
103 sapply(indices, function(i) self$getSerie(i))
98e958ca 104 ,
2057c793 105 getLevel = function(index)
a66a84b5
BA
106 {
107 index = dateIndexToInteger(index, self)
d2ab47a7 108 private$.level[index]
a66a84b5 109 },
d2ab47a7 110 getLevelHat = function(index)
a66a84b5
BA
111 {
112 index = dateIndexToInteger(index, self)
7c4b2952 113 private$.tvp[[index]]$level_hat
a66a84b5 114 },
d2ab47a7 115 getCenteredSerie = function(index)
2057c793
BA
116 {
117 index = dateIndexToInteger(index, self)
7c4b2952 118 private$.tvp[[index]]$serie - private$.level[index]
2057c793 119 },
d2ab47a7
BA
120 getCenteredSeries = function(indices)
121 sapply(indices, function(i) self$getCenteredSerie(i))
122 ,
2057c793 123 getExo = function(index)
a66a84b5
BA
124 {
125 index = dateIndexToInteger(index, self)
d2ab47a7 126 private$.exo[[index]]
a66a84b5 127 },
d2ab47a7
BA
128 getExoHat = function(index)
129 {
130 index = dateIndexToInteger(index, self)
131 private$.exo_hat[[index]]
132 }
3d69ff21
BA
133 )
134)