X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FForecast.R;h=c64d24f1cc01e2399ad01ac25d8b9e8c42949d27;hb=546b0cb65870355a2a2c3705c91418570499d3a6;hp=57817e7ba75fb6955cafdb6bfab3e0a33ed403e5;hpb=469529710f56c790ae932b45d13fed2e34bcabf2;p=talweg.git diff --git a/pkg/R/Forecast.R b/pkg/R/Forecast.R index 57817e7..c64d24f 100644 --- a/pkg/R/Forecast.R +++ b/pkg/R/Forecast.R @@ -1,57 +1,74 @@ -#' @title Forecast +#' Forecast #' -#' @description Forecast encapsulation +#' Forecast encapsulation #' -#' @field pred List with -#' \itemize{ +#' @docType class +#' @importFrom R6 R6Class +#' +#' @field .pred List with \itemize{ #' \item serie: 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 day indices where forcast 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(new_serie, new_params, new_index_in_data)}}{ +#' Acquire a new individual forecast, with its (optimized) parameters and the corresponding +#' index in the dataset.} +#' \item{\code{getDates()}}{ +#' Get dates where forecast occurs.} +#' \item{\code{getSerie(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" +Forecast = R6::R6Class("Forecast", + private = list( + .pred = list(), + .dates = integer(0) #store dates as integers (from 1970-01-01) ), - - methods = list( - initialize = function(...) - { - "Initialize empty Forecast object" - - callSuper(...) - }, - append = function(new_serie, new_params, new_index) + public = list( + initialize = function(dates) { - "Obtain a new pair (serie, params)" - - pred[[length(pred)+1]] <<- list("serie"=new_serie, "params"=new_params, "index"=new_index) + private$.dates <- dates + invisible(self) }, getSize = function() + length(private$.pred) + , + append = function(new_serie, new_params, new_index_in_data) { - length(pred) + private$.pred[[length(private$.pred)+1]] <- + list("serie"=new_serie, "params"=new_params, "index_in_data"=new_index_in_data) }, + getDates = function() + as.Date( private$.dates, origin="1970-01-01" ) + , getSerie = function(index) { - "Get serie values at specified index" - - pred[[index]]$serie + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$serie }, getParams = function(index) { - "Get params at specified index" - - pred[[index]]$params + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$params }, getIndexInData = function(index) { - "Get (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 } ) )