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