almost finished debug
[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 \itemize{
9 #' \item time: vector of times
10 #' \item serie: centered series
11 #' \item level: corresponding levels
12 #' \item exo: exogenous variables
13 #' \item exo_hat: predicted exogenous variables}
14 #'
15 #' @section Methods: \describe{
16 #' \item{\code{getSize()}}
17 #' {Return number of series in dataset.}
18 #' \item{\code{getStdHorizon()}}
19 #' {Return number of time steps from serie[1] until midnight}
20 #' \item{\code{append(new_time, new_centered_serie, new_level, new_exo, new_exo_hat)}}
21 #' {Acquire a new vector of lists (time, centered_serie, level, exo, exo_hat).}
22 #' \item{\code{getTime(index)}}
23 #' {Get times at specified index.}
24 #' \item{\code{getCenteredSerie(index)}}
25 #' {Get centered serie at specified index.}
26 #' \item{\code{getLevel(index)}}
27 #' {Get level at specified index.}
28 #' \item{\code{getSerie(index)}}
29 #' {Get serie (centered+level) at specified index.}
30 #' \item{\code{getExo(index)}}
31 #' {Get exogenous variables at specified index.}
32 #' \item{\code{getExoHat(index)}}
33 #' {Get estimated exogenous variables at specified index.}
34 #' \item{\code{getCoupleDays(first,last)}}
35 #' {Get indices of no-NA series followed by no-NA, within [first,last] range.}}
36 Data = R6::R6Class("Data",
37 private = list(
38 .data = list()
39 ),
40 public = list(
41 getSize = function()
42 length(private$.data)
43 ,
44 getStdHorizon = function()
45 24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1
46 ,
47 append = function(new_time, new_centered_serie, new_level, new_exo, new_exo_hat)
48 {
49 private$.data[[length(private$.data)+1]] <- list(
50 "time"=new_time, "centered_serie"=new_centered_serie, "level"=new_level,
51 "exo"=new_exo, "exo_hat"=new_exo_hat)
52 },
53 getTime = function(index)
54 {
55 index = dateIndexToInteger(index, self)
56 private$.data[[index]]$time
57 },
58 getCenteredSerie = function(index)
59 {
60 index = dateIndexToInteger(index, self)
61 private$.data[[index]]$centered_serie
62 },
63 getLevel = function(index)
64 {
65 index = dateIndexToInteger(index, self)
66 private$.data[[index]]$level
67 },
68 getSerie = function(index)
69 {
70 index = dateIndexToInteger(index, self)
71 private$.data[[index]]$centered_serie + private$.data[[index]]$level
72 },
73 getExo = function(index)
74 {
75 index = dateIndexToInteger(index, self)
76 private$.data[[index]]$exo
77 },
78 getExoHat = function(index)
79 {
80 index = dateIndexToInteger(index, self)
81 private$.data[[index]]$exo_hat
82 },
83 removeFirst = function()
84 private$.data <- private$.data[2:length(private$.data)]
85 ,
86 removeLast = function()
87 private$.data <- private$.data[1:(length(private$.data)-1)]
88 ,
89 getCoupleDays = function(first, last)
90 {
91 (first:last)[ sapply(first:last, function(i) {
92 !any(
93 is.na(private$.data$getCenteredSerie(i)) |
94 is.na(private$.data$getCenteredSerie(i+1)) )
95 }) ]
96 }
97 )
98 )