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 #' Note: in training stage ts_hat(day+1) = f(ts(day), exo(day+1)),
7 #' and in production ts_hat(day+1) = f(ts(day), exo_hat(day+1))
9 #' @param data Object of class Data, output of \code{getData()}.
10 #' @param indices Indices where to forecast (the day after); integers relative to the
11 #' beginning of data, or (convertible to) Date objects.
12 #' @param forecaster Name of the main forecaster; more details: ?F_<forecastername>
14 #' \item Persistence : use last (similar) day
15 #' \item Neighbors : weighted similar days
16 #' \item Average : average curve of all same day-in-week
17 #' \item Zero : just output 0 (benchmarking purpose)
19 #' @param pjump Function to predict the jump at the interface between two days;
20 #' more details: ?J_<functionname>
22 #' \item Persistence : use last (similar) day
23 #' \item Neighbors: re-use the weights from F_Neighbors
24 #' \item LastValue: start serie with last observed value
25 #' \item Zero: no adjustment => use shape prediction only
27 #' @param predict_from First time step to predict.
28 #' @param memory Data depth (in days) to be used for prediction.
29 #' @param horizon Last time step to predict.
30 #' @param ncores Number of cores for parallel execution (1 to disable).
31 #' @param verbose TRUE to print basic traces (runs beginnings)
32 #' @param ... Additional parameters for the forecasting models.
34 #' @return An object of class Forecast
37 #' ts_data <- system.file("extdata","intraday_measures.csv",package="talweg")
38 #' exo_data <- system.file("extdata","daily_exogenous.csv",package="talweg")
39 #' data <- getData(ts_data, exo_data, date_format="%Y-%m-%d %H:%M:%S", limit=200)
40 #' pred <- computeForecast(data, 100:130, "Persistence", "LastValue",
41 #' predict_from=8, memory=50, horizon=12, ncores=1)
43 #' #Sketch for real-time mode:
45 #' forecaster <- MyForecaster$new(myJumpPredictFunc)
47 #' # As soon as daily predictions are available:
49 #' level_hat=predicted_level,
50 #' exo_hat=predicted_exogenous)
53 #' level=observed_level,
54 #' exo=observed_exogenous)
55 #' # And, at every hour:
58 #' value=current_PM10)
59 #' # Finally, a bit before predict_from hour:
60 #' pred <- forecaster$predictSerie(data, data$getSize(), ...)
61 #' #do_something_with_pred
64 computeForecast = function(data, indices, forecaster, pjump, predict_from,
65 memory=Inf, horizon=length(data$getSerie(1)), ncores=3, verbose=FALSE, ...)
67 # (basic) Arguments sanity checks
68 predict_from = as.integer(predict_from)[1]
69 if (! predict_from %in% 1:length(data$getSerie(1)))
70 stop("predict_from in [1,24] (hours)")
71 if (hasArg("opera") && !list(...)$opera && memory < Inf)
72 memory <- Inf #finite memory in training mode makes no sense
73 horizon = as.integer(horizon)[1]
74 if (horizon<=predict_from || horizon>length(data$getSerie(1)))
75 stop("Horizon in [predict_from+1,24] (hours)")
76 integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
77 if (any(integer_indices<=0 | integer_indices>data$getSize()))
78 stop("Indices out of range")
79 if (!is.character(forecaster))
80 stop("forecaster (name): character")
81 if (!is.character(pjump))
82 stop("pjump (function): character")
84 pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
85 forecaster_class_name = getFromNamespace(
86 paste(forecaster,"Forecaster",sep=""), "talweg")
88 pjump <- getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg")
89 forecaster = forecaster_class_name$new(pjump)
91 computeOneForecast <- function(i)
94 print(paste("Index",i))
96 "forecast" = forecaster$predictSerie(data,i,memory,predict_from,horizon,...),
97 "params" = forecaster$getParameters(),
102 if (ncores > 1 && requireNamespace("parallel",quietly=TRUE))
103 parallel::mclapply(integer_indices, computeOneForecast, mc.cores=ncores)
105 lapply(integer_indices, computeOneForecast)
107 # TODO: find a way to fill pred in //...
108 for (i in seq_along(integer_indices))
111 forecast = p[[i]]$forecast,
112 params = p[[i]]$params,
113 index_in_data = p[[i]]$index