X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FData.R;h=8aa149d89fa4e381768577afe0b9585dcec3d626;hb=7c4b2952874de1d40a742e72efe51999b99050f5;hp=42e8c69b1ad0380f50404b1b1572a99cd73f0593;hpb=98e958cab563866f8e00886b54336018a2e8bc97;p=talweg.git diff --git a/pkg/R/Data.R b/pkg/R/Data.R index 42e8c69..8aa149d 100644 --- a/pkg/R/Data.R +++ b/pkg/R/Data.R @@ -1,99 +1,134 @@ #' Data #' -#' Data encapsulation +#' Data encapsulation, in the form of a few lists (time-series + exogenous variables). #' -#' @docType class -#' @importFrom R6 R6Class +#' The private field .tvp is a list where each cell contains the hourly variables for a +#' period of time of 24 hours, from 1am to next midnight. The other lists contain +#' informations on series' levels and exogenous variables (both measured and predicted). +#' +#' @usage # Data$new() #' -#' @field .data List of \itemize{ +#' @field .tvp List of "time-values"; in each cell: +#' \itemize{ #' \item time: vector of times -#' \item serie: centered series -#' \item level: corresponding levels -#' \item exo: exogenous variables -#' \item exo_hat: predicted exogenous variables} +#' \item serie: (measured) serie +#' \item level_hat: predicted level for current day +#' } +#' @field .level Vector of measured levels +#' @field .exo List of measured exogenous variables, cell = numerical vector. +#' @field .exo_hat List of predicted exogenous variables, cell = numerical vector. #' #' @section Methods: #' \describe{ #' \item{\code{getSize()}}{ -#' Return number of series in dataset.} -#' \item{\code{getStdHorizon()}}{ -#' Return number of time steps from serie[1] until midnight} -#' \item{\code{append(new_time, new_centered_serie, new_level, new_exo, new_exo_hat)}}{ -#' Acquire a new vector of lists (time, centered_serie, level, exo, exo_hat).} +#' Number of series in dataset.} +#' \item{\code{append(time, value, level_hat, exo, exo_hat)}}{ +#' Measured data for given vector of times + exogenous predictions from +#' last midgnight.} #' \item{\code{getTime(index)}}{ -#' Get times at specified index.} -#' \item{\code{getCenteredSerie(index)}}{ -#' Get centered serie at specified index.} -#' \item{\code{getCenteredSeries(indices)}}{ -#' Get centered series at specified indices (in columns).} -#' \item{\code{getLevel(index)}}{ -#' Get level at specified index.} +#' Times (vector) at specified index.} #' \item{\code{getSerie(index)}}{ -#' Get serie (centered+level) at specified index.} +#' Serie (centered+level) at specified index.} #' \item{\code{getSeries(indices)}}{ -#' Get series at specified indices (in columns).} +#' Series at specified indices (in columns).} +#' \item{\code{getLevel(index)}}{ +#' Measured level at specified index.} +#' \item{\code{getLevelHat(index)}}{ +#' Predicted level vector at specified index (by hour).} +#' \item{\code{getCenteredSerie(index)}}{ +#' Centered serie at specified index.} +#' \item{\code{getCenteredSeries(indices)}}{ +#' Centered series at specified indices (in columns).} #' \item{\code{getExo(index)}}{ -#' Get exogenous variables at specified index.} +#' Measured exogenous variables at specified index.} #' \item{\code{getExoHat(index)}}{ -#' Get estimated exogenous variables at specified index.} +#' Predicted exogenous variables at specified index.} #' } +#' +#' @docType class +#' @format R6 class +#' Data = R6::R6Class("Data", private = list( - .data = list() + .tvp = list(), + .level = vector("double",0), + .exo = list(), + .exo_hat = list() ), public = list( getSize = function() - length(private$.data) + length(private$.tvp) , - getStdHorizon = function() - 24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1 - , - append = function(new_time, new_centered_serie, new_level, new_exo, new_exo_hat) + append = function(time=NULL, value=NULL, level_hat=NULL, exo=NULL, exo_hat=NULL) { - private$.data[[length(private$.data)+1]] <- list( - "time"=new_time, "centered_serie"=new_centered_serie, "level"=new_level, - "exo"=new_exo, "exo_hat"=new_exo_hat) + if (!is.null(time) && !is.null(value) && !is.null(level_hat)) + { + L = length(private$.tvp) + if (L == 0 || strftime( tail(private$.tvp[[L]]$time,1), + format="%H:%M:%S", tz="GMT" ) == "00:00:00") + { + # Append a new cell + private$.tvp[[L+1]] <- list("time"=time, "serie"=value, "level_hat"=level_hat) + } + else + { + # Complete current cell + private$.tvp[[L]]$time <- c(private$.tvp[[L]]$time, time) + private$.tvp[[L]]$serie <- c(private$.tvp[[L]]$serie, value) + private$.tvp[[L]]$level_hat <- c(private$.tvp[[L]]$levem_hat, level_hat) + } + } + if (strftime( tail(private$.tvp[[length(private$.tvp)]]$time,1), + format="%H:%M:%S", tz="GMT" ) == "00:00:00") + { + private$.level = c(private$.level, + mean(private$.tvp[[length(private$.tvp)]]$serie, na.rm=TRUE)) + } + if (!is.null(exo)) + private$.exo[[length(private$.exo)+1]] = exo + if (!is.null(exo_hat)) + private$.exo_hat[[length(private$.exo_hat)+1]] = exo_hat }, getTime = function(index) { index = dateIndexToInteger(index, self) - private$.data[[index]]$time + private$.tvp[[index]]$time }, - getCenteredSerie = function(index) + getSerie = function(index) { index = dateIndexToInteger(index, self) - private$.data[[index]]$centered_serie + private$.tvp[[index]]$serie }, - getCenteredSeries = function(indices) - sapply(indices, function(i) self$getCenteredSerie(i)) + getSeries = function(indices) + sapply(indices, function(i) self$getSerie(i)) , getLevel = function(index) { index = dateIndexToInteger(index, self) - private$.data[[index]]$level + private$.level[index] }, - getSerie = function(index) + getLevelHat = function(index) { index = dateIndexToInteger(index, self) - private$.data[[index]]$centered_serie + private$.data[[index]]$level + private$.tvp[[index]]$level_hat }, - getSeries = function(indices) - sapply(indices, function(i) self$getSerie(i)) + getCenteredSerie = function(index) + { + index = dateIndexToInteger(index, self) + private$.tvp[[index]]$serie - private$.level[index] + }, + getCenteredSeries = function(indices) + sapply(indices, function(i) self$getCenteredSerie(i)) , getExo = function(index) { index = dateIndexToInteger(index, self) - private$.data[[index]]$exo + private$.exo[[index]] }, getExoHat = function(index) { index = dateIndexToInteger(index, self) - private$.data[[index]]$exo_hat - }, - removeFirst = function() - private$.data <- private$.data[2:length(private$.data)] - , - removeLast = function() - private$.data <- private$.data[1:(length(private$.data)-1)] + private$.exo_hat[[index]] + } ) )