bd19b8d1297831fda59b3b0da354c2f9576349ec
[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 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 An object of class Forecast
25 #'
26 #' @examples
27 #' ts_data = system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
28 #' exo_data = system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
29 #' data = getData(ts_data, exo_data, input_tz = "Europe/Paris",
30 #' working_tz="Europe/Paris", predict_at=7)
31 #' pred = computeForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
32 #' \dontrun{#Sketch for real-time mode:
33 #' data = new("Data", ...)
34 #' forecaster = new(..., data=data)
35 #' repeat {
36 #' data$append(some_new_data)
37 #' pred = forecaster$predict(data$getSize(), ...)
38 #' #do_something_with_pred
39 #' }}
40 #' @export
41 computeForecast = function(data, indices, forecaster, pjump,
42 memory=Inf, horizon=data$getStdHorizon(), ...)
43 {
44 # (basic) Arguments sanity checks
45 horizon = as.integer(horizon)[1]
46 if (horizon<=0 || horizon>length(data$getCenteredSerie(2)))
47 stop("Horizon too short or too long")
48 integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
49 if (any(integer_indices<=0 | integer_indices>data$getSize()))
50 stop("Indices out of range")
51 if (!is.character(forecaster) || !is.character(pjump))
52 stop("forecaster (name) and pjump (function) should be of class character")
53
54 pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
55 forecaster_class_name = getFromNamespace(paste(forecaster,"Forecaster",sep=""), "talweg")
56 forecaster = forecaster_class_name$new( #.pjump =
57 getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
58
59 #oo = forecaster$predictSerie(data, integer_indices[1], memory, horizon, ...)
60 #browser()
61
62 parll=TRUE #FALSE
63 if (parll)
64 {
65 library(parallel)
66 ppp <- parallel::mclapply(seq_along(integer_indices), function(i) {
67 list(
68 "forecast" = forecaster$predictSerie(data, integer_indices[i], memory, horizon, ...),
69 "params"= forecaster$getParameters(),
70 "index" = integer_indices[i] )
71 }, mc.cores=3)
72 }
73 else
74 {
75 ppp <- lapply(seq_along(integer_indices), function(i) {
76 list(
77 "forecast" = forecaster$predictSerie(data, integer_indices[i], memory, horizon, ...),
78 "params"= forecaster$getParameters(),
79 "index" = integer_indices[i] )
80 })
81 }
82 #browser()
83
84 for (i in seq_along(integer_indices))
85 {
86 pred$append(
87 new_serie = ppp[[i]]$forecast,
88 new_params = ppp[[i]]$params,
89 new_index_in_data = ppp[[i]]$index
90 )
91 }
92
93 pred
94 }