intermediate: R6, too slow
[talweg.git] / pkg / R / computeForecast.R
CommitLineData
3d69ff21
BA
1#' @title get Forecast
2#'
3#' @description Predict time-series curves for the selected days indices (lines in data).
3d69ff21
BA
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)
e030a6e3 7#' @param forecaster Name of the main forcaster
3d69ff21
BA
8#' \itemize{
9#' \item Persistence : use values of last (similar, next) day
e030a6e3 10#' \item Neighbors : use values from the k closest neighbors' tomorrows
3d69ff21 11#' \item Average : global average of all the (similar) "tomorrow of past"
e030a6e3 12#' \item Zero : just output 0 (benchmarking purpose)
3d69ff21 13#' }
e030a6e3 14#' @param pjump How to predict the jump at the interface between two days ?
3d69ff21
BA
15#' \itemize{
16#' \item Persistence : use last (similar) day values
e030a6e3 17#' \item Neighbors: re-use the weights optimized in corresponding forecaster
3d69ff21
BA
18#' \item Zero: just output 0 (no adjustment)
19#' }
e030a6e3
BA
20#' @param memory Data depth (in days) to be used for prediction
21#' @param horizon Number of time steps to predict
3d69ff21
BA
22#' @param ... Additional parameters for the forecasting models
23#'
25b75559
BA
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#' }
3d69ff21
BA
30#'
31#' @examples
44a9990b
BA
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)
99f83c9a 36#' pred = computeForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
3d69ff21
BA
37#' \dontrun{#Sketch for real-time mode:
38#' data = new("Data", ...)
e030a6e3 39#' forecaster = new(..., data=data)
3d69ff21
BA
40#' repeat {
41#' data$append(some_new_data)
e030a6e3 42#' pred = forecaster$predict(data$getSize(), ...)
3d69ff21
BA
43#' #do_something_with_pred
44#' }}
45#' @export
25b75559 46computeForecast = function(data, indices, forecaster, pjump,
e030a6e3 47 memory=Inf, horizon=data$getStdHorizon(), ...)
3d69ff21 48{
e030a6e3 49 # (basic) Arguments sanity checks
3d69ff21
BA
50 horizon = as.integer(horizon)[1]
51 if (horizon<=0 || horizon>length(data$getCenteredSerie(2)))
52 stop("Horizon too short or too long")
09cf9c19 53 indices = sapply( seq_along(indices), function(i) dateIndexToInteger(indices[i], data) )
3d69ff21
BA
54 if (any(indices<=0 | indices>data$getSize()))
55 stop("Indices out of range")
56 indices = sapply(indices, dateIndexToInteger, data)
e5aa669a
BA
57 if (!is.character(forecaster))
58 stop("forecaster (name) should be of class character") #pjump could be NULL
3d69ff21 59
25b75559
BA
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"))
3d69ff21
BA
64 for (today in indices)
65 {
25b75559
BA
66 pred$append(
67 new_serie = forecaster$predictSerie(today, memory, horizon, ...),
68 new_params = forecaster$getParameters(),
69 new_index = today
e030a6e3 70 )
3d69ff21 71 }
25b75559 72 pred
3d69ff21 73}