add realtime option, slightly refactor data acquisition
[talweg.git] / pkg / R / computeForecast.R
... / ...
CommitLineData
1#' Compute forecast
2#'
3#' 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 ncores Number of cores for parallel execution (1 to disable)
23#' @param ... Additional parameters for the forecasting models;
24#' In particular, realtime=TRUE to use predictions instead of measurements
25#'
26#' @return An object of class Forecast
27#'
28#' @examples
29#' ts_data = system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
30#' exo_data = system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
31#' data = getData(ts_data, exo_data, input_tz="GMT", working_tz="GMT", predict_at=7)
32#' pred = computeForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
33#' \dontrun{#Sketch for real-time mode:
34#' data = new("Data", ...)
35#' forecaster = new(..., data=data)
36#' repeat {
37#' data$append(some_new_data)
38#' pred = forecaster$predict(data$getSize(), ...)
39#' #do_something_with_pred
40#' }}
41#' @export
42computeForecast = function(data, indices, forecaster, pjump,
43 memory=Inf, horizon=data$getStdHorizon(), ncores=3, ...)
44{
45 # (basic) Arguments sanity checks
46 horizon = as.integer(horizon)[1]
47 if (horizon<=0 || horizon>length(data$getCenteredSerie(1)))
48 stop("Horizon too short or too long")
49 integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
50 if (any(integer_indices<=0 | integer_indices>data$getSize()))
51 stop("Indices out of range")
52 if (!is.character(forecaster) || !is.character(pjump))
53 stop("forecaster (name) and pjump (function) should be of class character")
54
55 pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
56 forecaster_class_name = getFromNamespace(
57 paste(forecaster,"Forecaster",sep=""), "talweg")
58 forecaster = forecaster_class_name$new( #.pjump =
59 getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
60
61 if (ncores > 1 && requireNamespace("parallel",quietly=TRUE))
62 {
63 p <- parallel::mclapply(seq_along(integer_indices), function(i) {
64 list(
65 "forecast" = forecaster$predictSerie(
66 data, integer_indices[i], memory, horizon, ...),
67 "params"= forecaster$getParameters(),
68 "index" = integer_indices[i] )
69 }, mc.cores=ncores)
70 }
71 else
72 {
73 p <- lapply(seq_along(integer_indices), function(i) {
74 list(
75 "forecast" = forecaster$predictSerie(
76 data, integer_indices[i], memory, horizon, ...),
77 "params"= forecaster$getParameters(),
78 "index" = integer_indices[i] )
79 })
80 }
81
82 # TODO: find a way to fill pred in //...
83 for (i in seq_along(integer_indices))
84 {
85 pred$append(
86 forecast = p[[i]]$forecast,
87 params = p[[i]]$params,
88 index_in_data = p[[i]]$index
89 )
90 }
91 pred
92}