X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FForecast.R;h=6036daf6504593809680291f8e53929b5cefb4a3;hb=102bcfda4afbb5cfee885cbee0f55545624168fd;hp=274f50eb7896511eca82cbbed8f7fdbae5c10575;hpb=1e20780ee1505fac6c7ed68d340892c497524561;p=talweg.git diff --git a/pkg/R/Forecast.R b/pkg/R/Forecast.R index 274f50e..6036daf 100644 --- a/pkg/R/Forecast.R +++ b/pkg/R/Forecast.R @@ -1,59 +1,83 @@ -#' @title Forecast +#' Forecast #' -#' @description Forecast encapsulation +#' Forecast encapsulation as a list (days where prediction occur) of lists (components). #' -#' @field pred List with +#' The private field .pred is a list where each cell contains the predicted variables for +#' a period of time of H<=24 hours, from hour P+1 until P+H, where P+1 is taken right +#' after the end of the period designated by \code{getIndexInData()}. In other terms, +#' \code{forecast$getForecast(i)} return forecasts for \code{data$getSerie(i+1)}. +#' Each cell .pred[[i]] is itself a list containing three slots, as described in the +#' 'field' section. +#' +#' @field .pred List with #' \itemize{ -#' \item serie: forecasted serie +#' \item serie: the forecasted serie #' \item params: corresponding list of parameters (weights, neighbors...) -#' \item index: corresponding index in data object +#' \item index_in_data: corresponding index in data object +#' } +#' @field .dates vector of (integer) day indices where forecast occurs +#' +#' @section Methods: +#' \describe{ +#' \item{\code{initialize(dates)}}{ +#' Initialize a Forecast object with a series of date indices.} +#' \item{\code{getSize()}}{ +#' Return number of individual forecasts.} +#' \item{\code{append(forecast, params, index_in_data)}}{ +#' Acquire an individual forecast, with its (optimized) parameters and the +#' corresponding index in the dataset.} +#' \item{\code{getDates()}}{ +#' Get dates where forecast occurs.} +#' \item{\code{getForecast(index)}}{ +#' Get forecasted serie at specified index.} +#' \item{\code{getParams(index)}}{ +#' Get parameters at specified index (for 'Neighbors' method).} +#' \item{\code{getIndexInData(index)}}{ +#' Get index in data which corresponds to current forecast.} #' } #' -#' @exportClass Forecast -#' @export Forecast -Forecast = setRefClass( - Class = "Forecast", - - fields = list( - pred = "list" +#' @docType class +#' @format R6 class +#' +Forecast = R6::R6Class("Forecast", + private = list( + .pred = list(), + .dates = integer(0) #store dates as integers (from 1970-01-01) ), - - methods = list( - initialize = function(...) + public = list( + initialize = function(dates) { - "Initialize empty Forecast object" - - callSuper(...) + private$.dates <- dates + invisible(self) }, getSize = function() + length(private$.pred) + , + append = function(forecast, params, index_in_data) { - "Number of individual forecasts" - - length(pred) - }, - append = function(new_serie, new_params, new_index) - { - "Obtain a new pair (serie, params)" - - pred[[length(pred)+1]] <<- list("serie"=new_serie, "params"=new_params, "index"=new_index) + private$.pred[[length(private$.pred)+1]] <- + list("forecast"=forecast, "params"=params, "index_in_data"=index_in_data) }, - getSerie = function(index) + getDates = function() + as.Date( private$.dates, origin="1970-01-01" ) + , + getForecast = function(index) { - "Serie values at specified index" - - pred[[index]]$serie + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$forecast }, getParams = function(index) { - "Params at specified index" - - pred[[index]]$params + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$params }, getIndexInData = function(index) { - "(day) Index in data where prediction took place" - - pred[[index]]$index + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$index_in_data } ) )