improvements, updated report
[talweg.git] / R / getForecast.R
1 #' @title get Forecast
2 #'
3 #' @description 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 #' \item Level : output a flat serie repeating the last observed level
14 #' }
15 #' @param pjump How to predict the jump at the interface between two days ?
16 #' \itemize{
17 #' \item Persistence : use last (similar) day values
18 #' \item Neighbors: re-use the weights optimized in corresponding forecaster
19 #' \item Zero: just output 0 (no adjustment)
20 #' }
21 #' @param memory Data depth (in days) to be used for prediction
22 #' @param horizon Number of time steps to predict
23 #' @param ... Additional parameters for the forecasting models
24 #'
25 #' @return An object of class Forecast
26 #'
27 #' @examples
28 #' data = getData(ts_data="data/pm10_mesures_H_loc.csv", exo_data="data/meteo_extra_noNAs.csv",
29 #' input_tz = "Europe/Paris", working_tz="Europe/Paris", predict_at=7)
30 #' pred = getForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
31 #' \dontrun{#Sketch for real-time mode:
32 #' data = new("Data", ...)
33 #' forecaster = new(..., data=data)
34 #' repeat {
35 #' data$append(some_new_data)
36 #' pred = forecaster$predict(data$getSize(), ...)
37 #' #do_something_with_pred
38 #' }}
39 #' @export
40 getForecast = function(data, indices, forecaster, pjump=NULL,
41 memory=Inf, horizon=data$getStdHorizon(), ...)
42 {
43 # (basic) Arguments sanity checks
44 horizon = as.integer(horizon)[1]
45 if (horizon<=0 || horizon>length(data$getCenteredSerie(2)))
46 stop("Horizon too short or too long")
47 indices = sapply( seq_along(indices), function(i) dateIndexToInteger(indices[i], data) )
48 if (any(indices<=0 | indices>data$getSize()))
49 stop("Indices out of range")
50 indices = sapply(indices, dateIndexToInteger, data)
51 if (!is.character(forecaster))
52 stop("forecaster (name) should be of class character") #pjump could be NULL
53
54 pred = list()
55 forecaster = new(paste(forecaster,"Forecaster",sep=""), data=data,
56 pjump =
57 if (is.null(pjump))
58 function() {}
59 else
60 getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
61 for (today in indices)
62 {
63 pred[[length(pred)+1]] = list(
64 "serie" = forecaster$predict(today, memory, horizon, ...),
65 "params" = forecaster$getParameters(),
66 "index" = today
67 )
68 }
69 new("Forecast",pred=pred)
70 }