update following 23/05 TODOs
[talweg.git] / pkg / R / computeForecast.R
... / ...
CommitLineData
1#' Compute forecast
2#'
3#' Predict time-series curves ("today" from predict_from to horizon) at the selected days
4#' indices ("today" from 1am to predict_from-1). This function just runs a loop over all
5#' requested indices, and stores the individual forecasts into a Forecast object.
6#'
7#' @param data Object of class 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) day
13#' \item Neighbors : weighted similar days
14#' \item Average : average curve 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) day
21#' \item Neighbors: re-use the weights from F_Neighbors
22#' \item LastValue: start serie with last observed value
23#' \item Zero: no adjustment => use shape prediction only
24#' }
25#' @param predict_from First time step to predict.
26#' @param memory Data depth (in days) to be used for prediction.
27#' @param horizon Last time step to predict.
28#' @param ncores Number of cores for parallel execution (1 to disable).
29#' @param verbose TRUE to print basic traces (runs beginnings)
30#' @param ... Additional parameters for the forecasting models.
31#'
32#' @return An object of class Forecast
33#'
34#' @examples
35#' ts_data <- system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
36#' exo_data <- system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
37#' data <- getData(ts_data, exo_data, limit=200)
38#' pred <- computeForecast(data, 100:130, "Persistence", "LastValue",
39#' predict_from=8, memory=50, horizon=12, ncores=1)
40#' \dontrun{
41#' #Sketch for real-time mode:
42#' data <- Data$new()
43#' forecaster <- MyForecaster$new(myJumpPredictFunc)
44#' repeat {
45#' # As soon as daily predictions are available:
46#' data$append(
47#' level_hat=predicted_level,
48#' exo_hat=predicted_exogenous)
49#' # When a day ends:
50#' data$append(
51#' level=observed_level,
52#' exo=observed_exogenous)
53#' # And, at every hour:
54#' data$append(
55#' time=current_hour,
56#' value=current_PM10)
57#' # Finally, a bit before predict_from hour:
58#' pred <- forecaster$predictSerie(data, data$getSize(), ...)
59#' #do_something_with_pred
60#' } }
61#' @export
62computeForecast = function(data, indices, forecaster, pjump, predict_from,
63 memory=Inf, horizon=length(data$getSerie(1)), ncores=3, verbose=FALSE, ...)
64{
65 # (basic) Arguments sanity checks
66 predict_from = as.integer(predict_from)[1]
67 if (! predict_from %in% 1:length(data$getSerie(1)))
68 stop("predict_from in [1,24] (hours)")
69 if (hasArg("opera") && !list(...)$opera && memory < Inf)
70 memory <- Inf #finite memory in training mode makes no sense
71 horizon = as.integer(horizon)[1]
72 if (horizon<=predict_from || horizon>length(data$getSerie(1)))
73 stop("Horizon in [predict_from+1,24] (hours)")
74 integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
75 if (any(integer_indices<=0 | integer_indices>data$getSize()))
76 stop("Indices out of range")
77 if (!is.character(forecaster))
78 stop("forecaster (name): character")
79 if (!is.character(pjump))
80 stop("pjump (function): character")
81
82 pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
83 forecaster_class_name = getFromNamespace(
84 paste(forecaster,"Forecaster",sep=""), "talweg")
85
86 pjump <- getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg")
87 forecaster = forecaster_class_name$new(pjump)
88
89 computeOneForecast <- function(i)
90 {
91 if (verbose)
92 print(paste("Index",i))
93 list(
94 "forecast" = forecaster$predictSerie(data,i,memory,predict_from,horizon,...),
95 "params" = forecaster$getParameters(),
96 "index" = i )
97 }
98
99 p <-
100 if (ncores > 1 && requireNamespace("parallel",quietly=TRUE))
101 parallel::mclapply(integer_indices, computeOneForecast, mc.cores=ncores)
102 else
103 lapply(integer_indices, computeOneForecast)
104
105 # TODO: find a way to fill pred in //...
106 for (i in seq_along(integer_indices))
107 {
108 pred$append(
109 forecast = p[[i]]$forecast,
110 params = p[[i]]$params,
111 index_in_data = p[[i]]$index
112 )
113 }
114 pred
115}