X-Git-Url: https://git.auder.net/?p=talweg.git;a=blobdiff_plain;f=pkg%2FR%2FForecaster.R;h=2bd2e4ea0a4dae7ed32b4f695d1a17ae425e39ac;hp=4c8437e195b6bcf6ab501ec187e23bd42d0469cd;hb=a66a84b56467194852f2faee15f4725759b24158;hpb=25b75559e2d9bf84e2de35b851d93fefdae36e17 diff --git a/pkg/R/Forecaster.R b/pkg/R/Forecaster.R index 4c8437e..2bd2e4e 100644 --- a/pkg/R/Forecaster.R +++ b/pkg/R/Forecaster.R @@ -2,70 +2,49 @@ #' #' Forecaster (abstract class, implemented by all forecasters) #' +#' @docType class +#' @importFrom R6 R6Class +#' #' @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 ? #' -#' @docType class -#' @importFrom R6 R6Class +#' @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.} } Forecaster = R6::R6Class("Forecaster", private = list( - .params = "list", - .data = "Data", - .pjump = "function" + .params = list(), + .data = NULL, + .pjump = NULL ), public = list( initialize = function(data, pjump) - initialize(self, private, data, pjump) - , + { + private$.data <- data + private$.pjump <- pjump + invisible(self) + }, predictSerie = function(today, memory, horizon, ...) - predictSerie(private, today, memory, horizon, ...) - , + { + # Parameters (potentially) computed during shape prediction stage + predicted_shape = o$predictShape(today, memory, horizon, ...) + predicted_delta = private$.pjump(private$.data,today,memory,horizon,private$.params,...) + # Predicted shape is aligned it on the end of current day + jump + predicted_shape+tail(private$.data$getSerie(today),1)-predicted_shape[1]+predicted_delta + }, predictShape = function(today, memory, horizon, ...) - predictShape(private, today, memory, horizon, ...) + #empty default implementation: to implement in inherited classes , getParameters = function() - getParameters(private) + private$.params ) ) - -#' Initialize (generic) Forecaster object -#' -#' @param o Object of class Forecaster -#' @param private List of private members in o -#' @param data Object of class Data -#' @param pjump Function to predict jump -initialize = function(o, private, data, pjump) -{ - .params <<- list() - .data <<- data - .pjump <<- pjump - invisible(o) -} - -#' Obtain a new forecasted time-serie -#' -#' @inheritParams initialize -#' @param today Index for current prediction -#' @param memory Depth in data (in days) -#' @param horizon Number of hours to forecast -predictSerie = function(private, today, memory, horizon, ...) -{ - # Parameters (potentially) computed during shape prediction stage - predicted_shape = predictShape(today, memory, horizon, ...) - predicted_delta = private$.pjump(private$.data, today, memory, horizon, params, ...) - # Predicted shape is aligned it on the end of current day + jump - predicted_shape + tail(private$.data$getSerie(today),1) - predicted_shape[1] + predicted_delta -} - -#' Shape prediction (centered curve) -#' -#' @inheritParams predictSerie -predictShape = function(private, today, memory, horizon, ...) - #empty default implementation: to implement in inherited classes - -#' Get parameters list -#' -#' @inheritParams initialize -getParameters = function(private) - private$.params