draft Neighbors2; fix bug in Neighbors1
[talweg.git] / pkg / R / Data.R
CommitLineData
f17665c7 1#' Data
3d69ff21 2#'
f17665c7 3#' Data encapsulation
3d69ff21 4#'
a66a84b5
BA
5#' @docType class
6#' @importFrom R6 R6Class
7#'
8#' @field .data List of \itemize{
3d69ff21
BA
9#' \item time: vector of times
10#' \item serie: centered series
11#' \item level: corresponding levels
f17665c7 12#' \item exo: exogenous variables
a66a84b5 13#' \item exo_hat: predicted exogenous variables}
f17665c7 14#'
98e958ca
BA
15#' @section Methods:
16#' \describe{
17#' \item{\code{getSize()}}{
18#' Return number of series in dataset.}
19#' \item{\code{getStdHorizon()}}{
20#' Return number of time steps from serie[1] until midnight}
21#' \item{\code{append(new_time, new_centered_serie, new_level, new_exo, new_exo_hat)}}{
22#' Acquire a new vector of lists (time, centered_serie, level, exo, exo_hat).}
23#' \item{\code{getTime(index)}}{
24#' Get times at specified index.}
25#' \item{\code{getCenteredSerie(index)}}{
26#' Get centered serie at specified index.}
27#' \item{\code{getCenteredSeries(indices)}}{
28#' Get centered series at specified indices (in columns).}
29#' \item{\code{getLevel(index)}}{
30#' Get level at specified index.}
31#' \item{\code{getSerie(index)}}{
32#' Get serie (centered+level) at specified index.}
33#' \item{\code{getSeries(indices)}}{
34#' Get series at specified indices (in columns).}
35#' \item{\code{getExo(index)}}{
36#' Get exogenous variables at specified index.}
37#' \item{\code{getExoHat(index)}}{
38#' Get estimated exogenous variables at specified index.}
39#' }
546b0cb6 40#'
25b75559 41Data = R6::R6Class("Data",
35eb1443 42 private = list(
25b75559 43 .data = list()
35eb1443 44 ),
f17665c7 45 public = list(
3d69ff21 46 getSize = function()
a66a84b5 47 length(private$.data)
35eb1443 48 ,
e030a6e3 49 getStdHorizon = function()
a66a84b5 50 24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1
35eb1443 51 ,
5d83d815 52 append = function(new_time, new_centered_serie, new_level, new_exo, new_exo_hat)
a66a84b5
BA
53 {
54 private$.data[[length(private$.data)+1]] <- list(
55 "time"=new_time, "centered_serie"=new_centered_serie, "level"=new_level,
56 "exo"=new_exo, "exo_hat"=new_exo_hat)
57 },
3d69ff21 58 getTime = function(index)
a66a84b5
BA
59 {
60 index = dateIndexToInteger(index, self)
61 private$.data[[index]]$time
62 },
3d69ff21 63 getCenteredSerie = function(index)
a66a84b5
BA
64 {
65 index = dateIndexToInteger(index, self)
66 private$.data[[index]]$centered_serie
67 },
98e958ca
BA
68 getCenteredSeries = function(indices)
69 sapply(indices, function(i) self$getCenteredSerie(i))
70 ,
3d69ff21 71 getLevel = function(index)
a66a84b5
BA
72 {
73 index = dateIndexToInteger(index, self)
74 private$.data[[index]]$level
75 },
3d69ff21 76 getSerie = function(index)
a66a84b5
BA
77 {
78 index = dateIndexToInteger(index, self)
79 private$.data[[index]]$centered_serie + private$.data[[index]]$level
80 },
98e958ca
BA
81 getSeries = function(indices)
82 sapply(indices, function(i) self$getSerie(i))
83 ,
1e20780e 84 getExo = function(index)
a66a84b5
BA
85 {
86 index = dateIndexToInteger(index, self)
87 private$.data[[index]]$exo
88 },
66877df3 89 getExoHat = function(index)
a66a84b5
BA
90 {
91 index = dateIndexToInteger(index, self)
92 private$.data[[index]]$exo_hat
93 },
94 removeFirst = function()
95 private$.data <- private$.data[2:length(private$.data)]
96 ,
97 removeLast = function()
98 private$.data <- private$.data[1:(length(private$.data)-1)]
3d69ff21
BA
99 )
100)