| 1 | #' Data |
| 2 | #' |
| 3 | #' Data encapsulation, in the form of a few lists (time-series + exogenous variables). |
| 4 | #' |
| 5 | #' The private field .tv 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 .tv List of "time-values"; in each cell: |
| 12 | #' \itemize{ |
| 13 | #' \item time: vector of times |
| 14 | #' \item serie: (measured) serie |
| 15 | #' } |
| 16 | #' @field .level Vector of measured levels |
| 17 | #' @field .level_hat Vector of predicted 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 at specified index.} |
| 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 | .tv = list(), |
| 54 | .level = vector("double",0), |
| 55 | .level_hat = vector("double",0), |
| 56 | .exo = list(), |
| 57 | .exo_hat = list() |
| 58 | ), |
| 59 | public = list( |
| 60 | getSize = function() |
| 61 | length(private$.tv) |
| 62 | , |
| 63 | append = function(time=NULL, value=NULL, level_hat=NULL, exo=NULL, exo_hat=NULL) |
| 64 | { |
| 65 | if (!is.null(time) && !is.null(value)) |
| 66 | { |
| 67 | L = length(private$.tv) |
| 68 | if (L == 0 || strftime( tail(private$.tv[[L]]$time,1), |
| 69 | format="%H:%M:%S", tz="GMT" ) == "00:00:00") |
| 70 | { |
| 71 | # Append a new cell |
| 72 | private$.tv[[L+1]] <- list("time"=time, "serie"=value) |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | # Complete current cell |
| 77 | private$.tv[[L]]$time = c(private$.tv[[L]]$time, time) |
| 78 | private$.tv[[L]]$serie = c(private$.tv[[L]]$serie, value) |
| 79 | } |
| 80 | } |
| 81 | if (strftime( tail(private$.tv[[length(private$.tv)]]$time,1), |
| 82 | format="%H:%M:%S", tz="GMT" ) == "00:00:00") |
| 83 | { |
| 84 | private$.level = c(private$.level, |
| 85 | mean(private$.tv[[length(private$.tv)]]$serie, na.rm=TRUE)) |
| 86 | } |
| 87 | if (!is.null(level_hat)) |
| 88 | private$.level_hat = c(private$.level_hat, level_hat) |
| 89 | if (!is.null(exo)) |
| 90 | private$.exo[[length(private$.exo)+1]] = exo |
| 91 | if (!is.null(exo_hat)) |
| 92 | private$.exo_hat[[length(private$.exo_hat)+1]] = exo_hat |
| 93 | }, |
| 94 | getTime = function(index) |
| 95 | { |
| 96 | index = dateIndexToInteger(index, self) |
| 97 | private$.tv[[index]]$time |
| 98 | }, |
| 99 | getSerie = function(index) |
| 100 | { |
| 101 | index = dateIndexToInteger(index, self) |
| 102 | private$.tv[[index]]$serie |
| 103 | }, |
| 104 | getSeries = function(indices) |
| 105 | sapply(indices, function(i) self$getSerie(i)) |
| 106 | , |
| 107 | getLevel = function(index) |
| 108 | { |
| 109 | index = dateIndexToInteger(index, self) |
| 110 | private$.level[index] |
| 111 | }, |
| 112 | getLevelHat = function(index) |
| 113 | { |
| 114 | index = dateIndexToInteger(index, self) |
| 115 | private$.level_hat[index] |
| 116 | }, |
| 117 | getCenteredSerie = function(index) |
| 118 | { |
| 119 | index = dateIndexToInteger(index, self) |
| 120 | private$.tv[[index]]$serie - private$.level[index] |
| 121 | }, |
| 122 | getCenteredSeries = function(indices) |
| 123 | sapply(indices, function(i) self$getCenteredSerie(i)) |
| 124 | , |
| 125 | getExo = function(index) |
| 126 | { |
| 127 | index = dateIndexToInteger(index, self) |
| 128 | private$.exo[[index]] |
| 129 | }, |
| 130 | getExoHat = function(index) |
| 131 | { |
| 132 | index = dateIndexToInteger(index, self) |
| 133 | private$.exo_hat[[index]] |
| 134 | } |
| 135 | ) |
| 136 | ) |