improve doc of computeForecast for realtime usage
[talweg.git] / pkg / R / computeForecast.R
1 #' Compute forecast
2 #'
3 #' Predict time-series curves for the selected days indices (lines in data).
4 #'
5 #' @param data Object of type \code{Data}, output of \code{getData()}.
6 #' @param indices Indices where to forecast (the day after); integers relative to the
7 #' beginning of data, or (convertible to) Date objects.
8 #' @param forecaster Name of the main forecaster; more details: ?F_<forecastername>
9 #' \itemize{
10 #' \item Persistence : use last (similar, next) day
11 #' \item Neighbors : weighted tomorrows of similar days
12 #' \item Average : average tomorrow of all same day-in-week
13 #' \item Zero : just output 0 (benchmarking purpose)
14 #' }
15 #' @param pjump Function to predict the jump at the interface between two days;
16 #' more details: ?J_<functionname>
17 #' \itemize{
18 #' \item Persistence : use last (similar, next) day
19 #' \item Neighbors: re-use the weights from F_Neighbors
20 #' \item Zero: just output 0 (no adjustment)
21 #' }
22 #' @param memory Data depth (in days) to be used for prediction.
23 #' @param horizon Number of time steps to predict.
24 #' @param ncores Number of cores for parallel execution (1 to disable).
25 #' @param ... Additional parameters for the forecasting models.
26 #'
27 #' @return An object of class Forecast
28 #'
29 #' @examples
30 #' ts_data <- system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
31 #' exo_data <- system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
32 #' data <- getData(ts_data, exo_data, input_tz="GMT", working_tz="GMT", predict_at=7)
33 #' pred <- computeForecast(data, 2200:2230, "Persistence", "Zero",
34 #' memory=500, horizon=12, ncores=1)
35 #' \dontrun{#Sketch for real-time mode:
36 #' data <- Data$new()
37 #' # Initialize: first day has no predictions attached
38 #' data$initialize()
39 #' forecaster <- MyForecaster$new(myJumpPredictFunc)
40 #' repeat {
41 #' # During the night between days j and j+1:
42 #' data$appendExoHat(exogenous_predictions)
43 #' # In the morning 7am+ or afternoon 1pm+:
44 #' data$setMeasures(
45 #' data$getSize()-1,
46 #' times_from_H+1_yersteday_to_Hnow,
47 #' PM10_values_of_last_24h,
48 #' exogenous_measures_for_yersteday)
49 #' pred <- forecaster$predictSerie(data, data$getSize()-1, ...)
50 #' #do_something_with_pred
51 #' }}
52 #' @export
53 computeForecast = function(data, indices, forecaster, pjump,
54 memory=Inf, horizon=data$getStdHorizon(), ncores=3, ...)
55 {
56 # (basic) Arguments sanity checks
57 horizon = as.integer(horizon)[1]
58 if (horizon<=0 || horizon>length(data$getCenteredSerie(1)))
59 stop("Horizon too short or too long")
60 integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
61 if (any(integer_indices<=0 | integer_indices>data$getSize()))
62 stop("Indices out of range")
63 if (!is.character(forecaster) || !is.character(pjump))
64 stop("forecaster (name) and pjump (function) should be of class character")
65
66 pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
67 forecaster_class_name = getFromNamespace(
68 paste(forecaster,"Forecaster",sep=""), "talweg")
69 forecaster = forecaster_class_name$new( #.pjump =
70 getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
71
72 if (ncores > 1 && requireNamespace("parallel",quietly=TRUE))
73 {
74 p <- parallel::mclapply(seq_along(integer_indices), function(i) {
75 list(
76 "forecast" = forecaster$predictSerie(
77 data, integer_indices[i], memory, horizon, ...),
78 "params"= forecaster$getParameters(),
79 "index" = integer_indices[i] )
80 }, mc.cores=ncores)
81 }
82 else
83 {
84 p <- lapply(seq_along(integer_indices), function(i) {
85 list(
86 "forecast" = forecaster$predictSerie(
87 data, integer_indices[i], memory, horizon, ...),
88 "params"= forecaster$getParameters(),
89 "index" = integer_indices[i] )
90 })
91 }
92
93 # TODO: find a way to fill pred in //...
94 for (i in seq_along(integer_indices))
95 {
96 pred$append(
97 forecast = p[[i]]$forecast,
98 params = p[[i]]$params,
99 index_in_data = p[[i]]$index
100 )
101 }
102 pred
103 }