new version, persistence -7 days
[talweg.git] / R / getForecast.R
index e126946..fde8e45 100644 (file)
@@ -1,44 +1,46 @@
 #' @title get Forecast
 #'
 #' @description Predict time-series curves for the selected days indices (lines in data).
-#'   Run the forecasting task described by \code{delta_forecaster_name} and
-#'   \code{shape_forecaster_name} on data obtained with \code{getData}
 #'
 #' @param data Dataset, object of type \code{Data} output of \code{getData}
 #' @param indices Days indices where to forecast (the day after)
-#' @param memory Data depth (in days) to be used for prediction
-#' @param horizon Number of time steps to predict
-#' @param shape_forecaster_name Name of the shape forcaster
+#' @param forecaster Name of the main forcaster
 #' \itemize{
 #'   \item Persistence : use values of last (similar, next) day
-#'   \item Neighbors : use PM10 from the k closest neighbors' tomorrows
+#'   \item Neighbors : use values from the k closest neighbors' tomorrows
 #'   \item Average : global average of all the (similar) "tomorrow of past"
+#'   \item Zero : just output 0 (benchmarking purpose)
+#'   \item Level : output a flat serie repeating the last observed level
 #' }
-#' @param delta_forecaster_name Name of the delta forecaster
+#' @param pjump How to predict the jump at the interface between two days ?
 #' \itemize{
 #'   \item Persistence : use last (similar) day values
-#'   \item Neighbors: re-use the weights optimized in corresponding shape forecaster
+#'   \item Neighbors: re-use the weights optimized in corresponding forecaster
 #'   \item Zero: just output 0 (no adjustment)
 #' }
+#' @param memory Data depth (in days) to be used for prediction
+#' @param horizon Number of time steps to predict
 #' @param ... Additional parameters for the forecasting models
 #'
 #' @return An object of class Forecast
 #'
 #' @examples
 #' data = getData(ts_data="data/pm10_mesures_H_loc.csv", exo_data="data/meteo_extra_noNAs.csv",
-#'   input_tz = "Europe/Paris", working_tz="Europe/Paris", predict_at="07")
-#' pred = getForecast(data, 2200:2230, Inf, 12, "Persistence", "Persistence")
+#'   input_tz = "Europe/Paris", working_tz="Europe/Paris", predict_at=7)
+#' pred = getForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
 #' \dontrun{#Sketch for real-time mode:
 #' data = new("Data", ...)
+#' forecaster = new(..., data=data)
 #' repeat {
 #'   data$append(some_new_data)
-#'   pred = getForecast(data, ...)
+#'   pred = forecaster$predict(data$getSize(), ...)
 #'   #do_something_with_pred
 #' }}
 #' @export
-getForecast = function(data, indices, memory, horizon,
-       shape_forecaster_name, delta_forecaster_name, ...)
+getForecast = function(data, indices, forecaster, pjump,
+       memory=Inf, horizon=data$getStdHorizon(), ...)
 {
+       # (basic) Arguments sanity checks
        horizon = as.integer(horizon)[1]
        if (horizon<=0 || horizon>length(data$getCenteredSerie(2)))
                stop("Horizon too short or too long")
@@ -46,30 +48,19 @@ getForecast = function(data, indices, memory, horizon,
        if (any(indices<=0 | indices>data$getSize()))
                stop("Indices out of range")
        indices = sapply(indices, dateIndexToInteger, data)
-
-       #NOTE: some assymetry here...
-       shape_forecaster = new(paste(shape_forecaster_name,"ShapeForecaster",sep=""), data=data)
-       #A little bit strange, but match.fun() and get() fail
-       delta_forecaster = getFromNamespace(
-               paste("get",delta_forecaster_name,"DeltaForecast",sep=""), "talweg")
+       if (!is.character(forecaster) || !is.character(pjump))
+               stop("forecaster and pjump should be of class character")
 
        pred = list()
+       forecaster = new(paste(forecaster,"Forecaster",sep=""), data=data,
+               pjump = getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
        for (today in indices)
        {
-               #shape always predicted first (on centered series, no scaling taken into account),
-               #with side-effect: optimize some parameters (h, weights, ...)
-               predicted_shape = shape_forecaster$predict(today, memory, horizon, ...)
-               #then, delta prediction can re-use some variables optimized previously (like neighbors infos)
-               predicted_delta = delta_forecaster(data, today, memory, horizon,
-                       shape_forecaster$getParameters(), ...)
-
-               #TODO: this way is faster than a call to append(); why ?
                pred[[length(pred)+1]] = list(
-                       # Predict shape and align it on end of current day
-                       serie = predicted_shape + tail( data$getSerie(today), 1 ) - predicted_shape[1] +
-                               predicted_delta, #add predicted jump
-                       params = shape_forecaster$getParameters(),
-                       index = today )
+                       "serie" = forecaster$predict(today, memory, horizon, ...),
+                       "params" = forecaster$getParameters(),
+                       "index" = today
+               )
        }
        new("Forecast",pred=pred)
 }