X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FForecast.R;h=323ebfb30c87450125ecbe0696b37181d8a45901;hb=4e821712ee4349b08a0ab3f6bcb35a00342ddef4;hp=b68b6c97f765071e3462c73996f269c5b89e74a2;hpb=25b75559e2d9bf84e2de35b851d93fefdae36e17;p=talweg.git diff --git a/pkg/R/Forecast.R b/pkg/R/Forecast.R index b68b6c9..323ebfb 100644 --- a/pkg/R/Forecast.R +++ b/pkg/R/Forecast.R @@ -1,107 +1,85 @@ #' Forecast #' -#' 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. +#' +#' @usage # Forecast$new(dates) +#' +#' @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.} #' } #' #' @docType class -#' @importFrom R6 R6Class +#' @format R6 class #' -#' @export Forecast = R6::R6Class("Forecast", private = list( - .pred = "list", - .dates = "Date" + .pred = list(), + .dates = integer(0) #store dates as integers (from 1970-01-01) ), public = list( initialize = function(dates) - initialize(self, private, dates) - , + { + private$.dates <- dates + invisible(self) + }, getSize = function() - getSize(private) - , - append = function(new_serie, new_params, new_index) - append(private, new_serie, new_params, new_index) + length(private$.pred) , + append = function(forecast, params, index_in_data) + { + private$.pred[[length(private$.pred)+1]] <- + list("forecast"=forecast, "params"=params, "index_in_data"=index_in_data) + }, getDates = function() - getDates(private) - , - getSerie = function(index) - getSerie(private, index) + as.Date( private$.dates, origin="1970-01-01" ) , + getForecast = function(index) + { + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$forecast + }, getParams = function(index) - getParams(private, index) - , + { + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$params + }, getIndexInData = function(index) - getIndexInData(private, index) + { + if (is(index,"Date")) + index = match(index, private$.dates) + private$.pred[[index]]$index_in_data + } ) ) - -#' Initialize empty Forecast object -#' -#' @param o Object of class Forecast -#' @param private List of private members in o -#' @param dates vector of dates where forecast occurs -initialize = function(o, private, dates) -{ - private$.dates <<- dates - private$.pred <<- list() - invisible(o) -} - -#' Number of individual forecasts" -#' -#' @inheritParams initialize -getSize = function(private) - length(private$.pred) - -#' Obtain a new pair (serie, params)" -#' -#' @inheritParams initialize -append = function(new_serie, new_params, new_index_in_data) -{ - private$.pred[[length(private$.pred)+1]] <<- - list("serie"=new_serie, "params"=new_params, "index_in_data"=new_index_in_data) -} - -#' Dates where prediction occurs -#' -#' inheritParams initialize -getDates = function(private) - private$.dates - -#' Serie values at specified index" -#' -#' @inheritParams initialize -#' @param index Return value at this index -getSerie = function(index) -{ - if (is(index,"Date")) - index = match(index, private$.dates) - private$.pred[[index]]$serie -} - -#' Params at specified index" -#' -#' @inheritParams getSerie -getParams = function(index) -{ - if (is(index,"Date")) - index = match(index, private$.dates) - private$.pred[[index]]$params -} - -#' (day) Index in data where prediction took place" -#' -#' @inheritParams getSerie -getIndexInData = function(index) -{ - if (is(index,"Date")) - index = match(index, private$.dates) - private$.pred[[index]]$index_in_data -}