fix tests
[talweg.git] / pkg / R / Data.R
CommitLineData
f17665c7 1#' Data
3d69ff21 2#'
102bcfda 3#' Data encapsulation as a list (days) of lists (components).
3d69ff21 4#'
102bcfda
BA
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.
a66a84b5 11#'
102bcfda 12#' @field .data[[i]] List of
2057c793 13#' \itemize{
3d69ff21 14#' \item time: vector of times
102bcfda
BA
15#' \item centered_serie: centered serie
16#' \item level: corresponding level
f17665c7 17#' \item exo: exogenous variables
102bcfda 18#' \item exo_hat: predicted exogenous variables (for next day)
2057c793 19#' }
f17665c7 20#'
98e958ca
BA
21#' @section Methods:
22#' \describe{
23#' \item{\code{getSize()}}{
102bcfda 24#' Number of series in dataset.}
98e958ca 25#' \item{\code{getStdHorizon()}}{
102bcfda
BA
26#' Number of time steps from serie[1] until midnight}
27#' \item{\code{append(time, serie, exo, exo_hat)}}{
3ddf1c12
BA
28#' Measured data for given vector of times + exogenous predictions from
29#' last midgnight.}
98e958ca 30#' \item{\code{getTime(index)}}{
102bcfda 31#' Times (vector) at specified index.}
2057c793 32#' \item{\code{getCenteredSerie(index)}}{
102bcfda 33#' Centered serie at specified index.}
2057c793 34#' \item{\code{getCenteredSeries(indices)}}{
102bcfda 35#' Centered series at specified indices (in columns).}
2057c793 36#' \item{\code{getLevel(index)}}{
102bcfda 37#' Level at specified index.}
2057c793 38#' \item{\code{getSerie(index)}}{
102bcfda 39#' Serie (centered+level) at specified index.}
2057c793 40#' \item{\code{getSeries(indices)}}{
102bcfda 41#' Series at specified indices (in columns).}
2057c793 42#' \item{\code{getExoHat(index)}}{
102bcfda 43#' Predicted exogenous variables at specified index.}
2057c793 44#' \item{\code{getExo(index)}}{
102bcfda 45#' Measured exogenous variables at specified index.}
2057c793
BA
46#' \item{\code{removeFirst()}}{
47#' Remove first list element (if truncated).}
48#' \item{\code{removeLast()}}{
49#' Remove last list element (if truncated).}
98e958ca 50#' }
546b0cb6 51#'
102bcfda
BA
52#' @docType class
53#' @format R6 class
54#'
25b75559 55Data = R6::R6Class("Data",
35eb1443 56 private = list(
25b75559 57 .data = list()
35eb1443 58 ),
f17665c7 59 public = list(
3d69ff21 60 getSize = function()
a66a84b5 61 length(private$.data)
35eb1443 62 ,
e030a6e3 63 getStdHorizon = function()
a66a84b5 64 24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1
35eb1443 65 ,
4f5204f0 66 append = function(time, serie, exo, exo_hat)
72b9c501
BA
67 {
68 level = mean(serie, na.rm=TRUE)
69 centered_serie = serie - level
4f5204f0
BA
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
a66a84b5 76 },
3d69ff21 77 getTime = function(index)
a66a84b5
BA
78 {
79 index = dateIndexToInteger(index, self)
80 private$.data[[index]]$time
81 },
2057c793 82 getCenteredSerie = function(index)
a66a84b5
BA
83 {
84 index = dateIndexToInteger(index, self)
2057c793 85 private$.data[[index]]$centered_serie
a66a84b5 86 },
2057c793
BA
87 getCenteredSeries = function(indices)
88 sapply(indices, function(i) self$getCenteredSerie(i))
98e958ca 89 ,
2057c793 90 getLevel = function(index)
a66a84b5
BA
91 {
92 index = dateIndexToInteger(index, self)
2057c793 93 private$.data[[index]]$level
a66a84b5 94 },
2057c793 95 getSerie = function(index)
a66a84b5
BA
96 {
97 index = dateIndexToInteger(index, self)
2057c793 98 private$.data[[index]]$centered_serie + private$.data[[index]]$level
a66a84b5 99 },
2057c793
BA
100 getSeries = function(indices)
101 sapply(indices, function(i) self$getSerie(i))
98e958ca 102 ,
2057c793
BA
103 getExoHat = function(index)
104 {
105 index = dateIndexToInteger(index, self)
106 private$.data[[index]]$exo_hat
107 },
108 getExo = function(index)
a66a84b5
BA
109 {
110 index = dateIndexToInteger(index, self)
2057c793 111 private$.data[[index]]$exo
a66a84b5
BA
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)]
3d69ff21
BA
118 )
119)