X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FForecaster.R;h=52587602ee9b6c3533c70fc1245b3bb0dc4616e4;hb=d2ab47a744d8fb29c03a76a7ca2368dae53f9a57;hp=71cb667430a94a1a2f91e807c7231bf795f709ae;hpb=469529710f56c790ae932b45d13fed2e34bcabf2;p=talweg.git diff --git a/pkg/R/Forecaster.R b/pkg/R/Forecaster.R index 71cb667..5258760 100644 --- a/pkg/R/Forecaster.R +++ b/pkg/R/Forecaster.R @@ -1,48 +1,74 @@ -#' @title Forecaster (abstract class) +#' Forecaster #' -#' @description Abstract class to represent a forecaster (they all inherit this) +#' Forecaster (abstract class, implemented by all forecasters). #' -#' @field params List of computed parameters, for post-run analysis (dev) -#' @field data Dataset, object of class Data -#' @field pjump Function: how to predict the jump at day interface ? -Forecaster = setRefClass( - Class = "Forecaster", - - fields = list( - params = "list", - data = "Data", - pjump = "function" +#' A Forecaster object encapsulates parameters (which can be of various kinds, for +#' example "Neighbors" method stores informations about the considered neighborhood for +#' the current prediction task) and one main function: \code{predictSerie()}. This last +#' function (by default) calls \code{predictShape()} to get a forecast of a centered +#' serie, and then calls the "jump prediction" function -- see "field" section -- to +#' adjust it based on the last observed values. +#' +#' @usage # Forecaster$new(pjump) #warning: predictShape() is unimplemented +#' +#' @field .params List of computed parameters (if applicable). +#' @field .pjump Function: how to predict the jump at day interface? The arguments of +#' this function are -- in this order: +#' \itemize{ +#' \item data : object output of \code{getData()}, +#' \item today : index (integer or date) of the last known day in data, +#' \item memory : number of days to use in the past (including today), +#' \item horizon : number of time steps to predict, +#' \item params : optimized parameters in the main method \code{predictShape()}, +#' \item ... : additional arguments. +#' } +#' .pjump returns an estimation of the jump after the last observed value. +#' +#' @section Methods: +#' \describe{ +#' \item{\code{initialize(data, pjump)}}{ +#' Initialize a Forecaster object with a Data object and a jump prediction function.} +#' \item{\code{predictSerie(today,memory,horizon,...)}}{ +#' Predict a new serie of \code{horizon} values at day index \code{today} using +#' \code{memory} days in the past.} +#' \item{\code{predictShape(today,memory,horizon,...)}}{ +#' Predict a new shape of \code{horizon} values at day index \code{today} using +#' \code{memory} days in the past.} +#' \item{\code{getParameters()}}{ +#' Return (internal) parameters.} +#' } +#' +#' @docType class +#' @format R6 class +#' +Forecaster = R6::R6Class("Forecaster", + private = list( + .params = list(), + .pjump = NULL ), - - methods = list( - initialize = function(...) + public = list( + initialize = function(pjump) { - "Initialize (generic) Forecaster object" - - callSuper(...) - if (!hasArg(data)) - stop("Forecaster must be initialized with a Data object") - params <<- list() + private$.pjump <- pjump + invisible(self) }, - predict = function(today, memory, horizon, ...) + predictSerie = function(data, today, memory, predict_from, horizon, ...) { - "Obtain a new forecasted time-serie" - # Parameters (potentially) computed during shape prediction stage - predicted_shape = predictShape(today, memory, horizon, ...) - predicted_delta = pjump(data, today, memory, horizon, params, ...) - # Predicted shape is aligned it on the end of current day + jump - predicted_shape + tail(data$getSerie(today),1) - predicted_shape[1] + predicted_delta - }, - predictShape = function(today, memory, horizon, ...) - { - "Shape prediction (centered curve)" + predicted_shape = self$predictShape(data,today,memory,predict_from,horizon,...) + predicted_delta = private$.pjump(data, today, memory, predict_from, horizon, + private$.params, ...) - #empty default implementation: to implement in inherited classes + # Predicted shape is aligned on the end of current day + jump + c( data$getSerie(today)[if (predict_from>=2) 1:(predict_from-1) else c()], + predicted_shape - predicted_shape[1] + predicted_delta + + ifelse(predict_from>=2, + data$getSerie(today)[predict_from-1], tail(data$getSerie(today-1),1)) ) }, + predictShape = function(data, today, memory, predict_from, horizon, ...) + NULL #empty default implementation: to implement in inherited classes + , getParameters = function() - { - params - } + private$.params ) )