X-Git-Url: https://git.auder.net/?p=talweg.git;a=blobdiff_plain;f=pkg%2FR%2FForecaster.R;fp=pkg%2FR%2FForecaster.R;h=784f86e48d216ee270f3ae6e775d9297ad891a8e;hp=cefa1ea676031c2a138793cbd5cbb9622ad86b6e;hb=af718fd5a9a330b13b331e78824a47407a3479ae;hpb=c8a81efd2e8302cde424165539f49e4bb7466fc3 diff --git a/pkg/R/Forecaster.R b/pkg/R/Forecaster.R index cefa1ea..784f86e 100644 --- a/pkg/R/Forecaster.R +++ b/pkg/R/Forecaster.R @@ -6,8 +6,9 @@ #' 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. +#' serie, and then calls the "jump prediction" function if it's provided -- see "field" +#' section -- to adjust it based on the last observed values. The main method in derived +#' forecasters is \code{predictShape()}; see 'Methods' section. #' #' @usage # Forecaster$new(pjump) #warning: predictShape() is unimplemented #' @@ -15,25 +16,27 @@ #' @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. +#' \item data: object output of \code{getData()}, +#' \item today: index of the current day in data (known until predict_from-1), +#' \item memory: number of days to use in the past (including today), +#' \item predict_from: first time step to predict (in [1,24]) +#' \item horizon: last time step to predict (in [predict_from,24]), +#' \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 +#' Initialize a Forecaster object with a Data object and a jump prediction function, +#' or NULL if \code{predictShape()} returns an adjusted curve.} +#' \item{\code{predictSerie(data,today,memory,predict_from,horizon,...)}}{ +#' Predict the next curve (at index today) from predict_from to horizon (hours), using #' \code{memory} days in the past.} +#' \item{\code{predictShape(data,today,memory,predict_from,horizon,...)}}{ +#' Predict the shape of the next curve (at index today) from predict_from to horizon +#' (hours), using \code{memory} days in the past.} #' \item{\code{getParameters()}}{ #' Return (internal) parameters.} #' } @@ -52,15 +55,29 @@ Forecaster = R6::R6Class("Forecaster", private$.pjump <- pjump invisible(self) }, - predictSerie = function(data, today, memory, horizon, ...) + predictSerie = function(data, today, memory, predict_from, horizon, ...) { # Parameters (potentially) computed during shape prediction stage - predicted_shape = self$predictShape(data, today, memory, horizon, ...) - predicted_delta = private$.pjump(data,today,memory,horizon,private$.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 + predicted_shape <- self$predictShape(data,today,memory,predict_from,horizon,...) + + if (is.na(predicted_shape)) + return (NA) + + predicted_delta <- + if (is.null(private$.pjump)) + NULL + else + private$.pjump(data,today,memory,predict_from,horizon,private$.params,...) + + # Predicted shape is aligned on the end of current day + jump (if jump!=NULL) + c( data$getSerie(today)[if (predict_from>=2) 1:(predict_from-1) else c()], + predicted_shape + ifelse( is.null(private$.pjump), + 0, + predicted_delta - predicted_shape[1] + + ifelse(predict_from>=2, + data$getSerie(today)[predict_from-1], tail(data$getSerie(today-1),1)) ) ) }, - predictShape = function(data, today, memory, horizon, ...) + predictShape = function(data, today, memory, predict_from, horizon, ...) NULL #empty default implementation: to implement in inherited classes , getParameters = function()