53a8d5b1d5245629ae0ed28b34580f65f49f68f3
[talweg.git] / pkg / R / Data.R
1 #' Data
2 #'
3 #' Data encapsulation
4 #'
5 #' @docType class
6 #' @importFrom R6 R6Class
7 #'
8 #' @field .data List of
9 #' \itemize{
10 #' \item time: vector of times
11 #' \item centered_serie: centered series
12 #' \item level: corresponding levels
13 #' \item exo: exogenous variables
14 #' \item exo_hat: predicted exogenous variables
15 #' }
16 #'
17 #' @section Methods:
18 #' \describe{
19 #' \item{\code{getSize()}}{
20 #' Return number of series in dataset.}
21 #' \item{\code{getStdHorizon()}}{
22 #' Return number of time steps from serie[1] until midnight}
23 #' \item{\code{appendHat(time, exo_hat)}}{
24 #' New estimated exogenous variables + time}
25 #' \item{\code{append(serie, exo)}}{
26 #' New measured data; call *after* \code{appendHat()}}
27 #' \item{\code{getTime(index)}}{
28 #' Get times at specified index.}
29 #' \item{\code{getCenteredSerie(index)}}{
30 #' Get (measured or predicted) centered serie at specified index.}
31 #' \item{\code{getCenteredSeries(indices)}}{
32 #' Get centered series at specified indices (in columns).}
33 #' \item{\code{getLevel(index)}}{
34 #' Get level at specified index.}
35 #' \item{\code{getSerie(index)}}{
36 #' Get serie (centered+level) at specified index.}
37 #' \item{\code{getSeries(indices)}}{
38 #' Get series at specified indices (in columns).}
39 #' \item{\code{getExoHat(index)}}{
40 #' Get predicted exogenous variables at specified index.}
41 #' \item{\code{getExo(index)}}{
42 #' Get exogenous variables at specified index.}
43 #' \item{\code{removeFirst()}}{
44 #' Remove first list element (if truncated).}
45 #' \item{\code{removeLast()}}{
46 #' Remove last list element (if truncated).}
47 #' }
48 #'
49 Data = R6::R6Class("Data",
50 private = list(
51 .data = list()
52 ),
53 public = list(
54 getSize = function()
55 length(private$.data)
56 ,
57 getStdHorizon = function()
58 24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1
59 ,
60 appendHat = function(time, exo_hat)
61 private$.data[[length(private$.data)+1]] <- list("time"=time,"exo_hat"=exo_hat)
62 ,
63 append = function(time, serie, exo)
64 {
65 index <- length(private$.data)
66 level = mean(serie, na.rm=TRUE)
67 centered_serie = serie - level
68 private$.data[[index]]$time <- time
69 private$.data[[index]]$centered_serie <- centered_serie
70 private$.data[[index]]$level <- level
71 private$.data[[index]]$exo <- exo
72 },
73 getTime = function(index)
74 {
75 index = dateIndexToInteger(index, self)
76 private$.data[[index]]$time
77 },
78 getCenteredSerie = function(index)
79 {
80 index = dateIndexToInteger(index, self)
81 private$.data[[index]]$centered_serie
82 },
83 getCenteredSeries = function(indices)
84 sapply(indices, function(i) self$getCenteredSerie(i))
85 ,
86 getLevel = function(index)
87 {
88 index = dateIndexToInteger(index, self)
89 private$.data[[index]]$level
90 },
91 getSerie = function(index)
92 {
93 index = dateIndexToInteger(index, self)
94 private$.data[[index]]$centered_serie + private$.data[[index]]$level
95 },
96 getSeries = function(indices)
97 sapply(indices, function(i) self$getSerie(i))
98 ,
99 getExoHat = function(index)
100 {
101 index = dateIndexToInteger(index, self)
102 private$.data[[index]]$exo_hat
103 },
104 getExo = function(index)
105 {
106 index = dateIndexToInteger(index, self)
107 private$.data[[index]]$exo
108 },
109 removeFirst = function()
110 private$.data <- private$.data[2:length(private$.data)]
111 ,
112 removeLast = function()
113 private$.data <- private$.data[1:(length(private$.data)-1)]
114 )
115 )