92ba2c1bdcc12147a5a74bd4cd54e59c1116ec0b
[talweg.git] / pkg / R / Data.R
1 #' Data
2 #'
3 #' Data encapsulation as a list (days) of lists (components).
4 #'
5 #' The private field .data is a list where each cell contains the variables for a period
6 #' of time of 24 hours, from time P+1 until P the next day where P is an integer between
7 #' 0 and 23. .data[[1]] refers to the first measured data (serie and exogenous
8 #' variables): the corresponding times can be accessed through the function
9 #' \code{getTime()} below. Each cell .data[[i]] is itself a list containing five slots,
10 #' as described in the 'field' section.
11 #'
12 #' @field .data[[i]] List of
13 #' \itemize{
14 #' \item time: vector of times
15 #' \item centered_serie: centered serie
16 #' \item level: corresponding level
17 #' \item exo: exogenous variables
18 #' \item exo_hat: predicted exogenous variables (for next day)
19 #' }
20 #'
21 #' @section Methods:
22 #' \describe{
23 #' \item{\code{getSize()}}{
24 #' Number of series in dataset.}
25 #' \item{\code{getStdHorizon()}}{
26 #' Number of time steps from serie[1] until midnight}
27 #' \item{\code{append(time, serie, exo, exo_hat)}}{
28 #' Measured data for given vector of times + exogenous predictions from
29 #' last midgnight.}
30 #' \item{\code{getTime(index)}}{
31 #' Times (vector) at specified index.}
32 #' \item{\code{getCenteredSerie(index)}}{
33 #' Centered serie at specified index.}
34 #' \item{\code{getCenteredSeries(indices)}}{
35 #' Centered series at specified indices (in columns).}
36 #' \item{\code{getLevel(index)}}{
37 #' Level at specified index.}
38 #' \item{\code{getSerie(index)}}{
39 #' Serie (centered+level) at specified index.}
40 #' \item{\code{getSeries(indices)}}{
41 #' Series at specified indices (in columns).}
42 #' \item{\code{getExoHat(index)}}{
43 #' Predicted exogenous variables at specified index.}
44 #' \item{\code{getExo(index)}}{
45 #' Measured exogenous variables at specified index.}
46 #' \item{\code{removeFirst()}}{
47 #' Remove first list element (if truncated).}
48 #' \item{\code{removeLast()}}{
49 #' Remove last list element (if truncated).}
50 #' }
51 #'
52 #' @docType class
53 #' @format R6 class
54 #'
55 Data = R6::R6Class("Data",
56 private = list(
57 .data = list()
58 ),
59 public = list(
60 getSize = function()
61 length(private$.data)
62 ,
63 getStdHorizon = function()
64 24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1
65 ,
66 append = function(time, serie, exo, exo_hat)
67 {
68 level = mean(serie, na.rm=TRUE)
69 centered_serie = serie - level
70 private$.data[[length(private$.data)+1]] <- list(
71 "time"=time, #H-24 --> H-1
72 "centered_serie"=centered_serie, #at 'time'
73 "level"=level, #at 'time'
74 "exo"=exo, #at 'time' (yersteday 0am to last midnight)
75 "exo_hat"=exo_hat) #today 0am to next midnight
76 },
77 getTime = function(index)
78 {
79 index = dateIndexToInteger(index, self)
80 private$.data[[index]]$time
81 },
82 getCenteredSerie = function(index)
83 {
84 index = dateIndexToInteger(index, self)
85 private$.data[[index]]$centered_serie
86 },
87 getCenteredSeries = function(indices)
88 sapply(indices, function(i) self$getCenteredSerie(i))
89 ,
90 getLevel = function(index)
91 {
92 index = dateIndexToInteger(index, self)
93 private$.data[[index]]$level
94 },
95 getSerie = function(index)
96 {
97 index = dateIndexToInteger(index, self)
98 private$.data[[index]]$centered_serie + private$.data[[index]]$level
99 },
100 getSeries = function(indices)
101 sapply(indices, function(i) self$getSerie(i))
102 ,
103 getExoHat = function(index)
104 {
105 index = dateIndexToInteger(index, self)
106 private$.data[[index]]$exo_hat
107 },
108 getExo = function(index)
109 {
110 index = dateIndexToInteger(index, self)
111 private$.data[[index]]$exo
112 },
113 removeFirst = function()
114 private$.data <- private$.data[2:length(private$.data)]
115 ,
116 removeLast = function()
117 private$.data <- private$.data[1:(length(private$.data)-1)]
118 )
119 )