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