intermediate: R6, too slow
[talweg.git] / pkg / R / computeForecast.R
... / ...
CommitLineData
1#' @title get Forecast
2#'
3#' @description Predict time-series curves for the selected days indices (lines in data).
4#'
5#' @param data Dataset, object of type \code{Data} output of \code{getData}
6#' @param indices Days indices where to forecast (the day after)
7#' @param forecaster Name of the main forcaster
8#' \itemize{
9#' \item Persistence : use values of last (similar, next) day
10#' \item Neighbors : use values from the k closest neighbors' tomorrows
11#' \item Average : global average of all the (similar) "tomorrow of past"
12#' \item Zero : just output 0 (benchmarking purpose)
13#' }
14#' @param pjump How to predict the jump at the interface between two days ?
15#' \itemize{
16#' \item Persistence : use last (similar) day values
17#' \item Neighbors: re-use the weights optimized in corresponding forecaster
18#' \item Zero: just output 0 (no adjustment)
19#' }
20#' @param memory Data depth (in days) to be used for prediction
21#' @param horizon Number of time steps to predict
22#' @param ... Additional parameters for the forecasting models
23#'
24#' @return A list with the following items
25#' \itemize{
26#' \item serie: forecasted serie
27#' \item params: corresponding list of parameters (weights, neighbors...)
28#' \item index: corresponding index in data object
29#' }
30#'
31#' @examples
32#' ts_data = system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
33#' exo_data = system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
34#' data = getData(ts_data, exo_data, input_tz = "Europe/Paris",
35#' working_tz="Europe/Paris", predict_at=7)
36#' pred = computeForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
37#' \dontrun{#Sketch for real-time mode:
38#' data = new("Data", ...)
39#' forecaster = new(..., data=data)
40#' repeat {
41#' data$append(some_new_data)
42#' pred = forecaster$predict(data$getSize(), ...)
43#' #do_something_with_pred
44#' }}
45#' @export
46computeForecast = function(data, indices, forecaster, pjump,
47 memory=Inf, horizon=data$getStdHorizon(), ...)
48{
49 # (basic) Arguments sanity checks
50 horizon = as.integer(horizon)[1]
51 if (horizon<=0 || horizon>length(data$getCenteredSerie(2)))
52 stop("Horizon too short or too long")
53 indices = sapply( seq_along(indices), function(i) dateIndexToInteger(indices[i], data) )
54 if (any(indices<=0 | indices>data$getSize()))
55 stop("Indices out of range")
56 indices = sapply(indices, dateIndexToInteger, data)
57 if (!is.character(forecaster))
58 stop("forecaster (name) should be of class character") #pjump could be NULL
59
60 pred = Forecast$new()
61 forecaster_class_name = getFromNamespace(paste(forecaster,"Forecaster",sep=""), "talweg")
62 forecaster = forecaster_class_name$new(data=data,
63 pjump = getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
64 for (today in indices)
65 {
66 pred$append(
67 new_serie = forecaster$predictSerie(today, memory, horizon, ...),
68 new_params = forecaster$getParameters(),
69 new_index = today
70 )
71 }
72 pred
73}