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