#' Forecast
#'
-#' Forecast encapsulation
+#' Forecast encapsulation as a list (days where prediction occur) of lists (components).
#'
-#' @docType class
-#' @importFrom R6 R6Class
+#' The private field .pred is a list where each cell contains the predicted variables for
+#' a period of time of H-P+1<=24 hours, from hour P until H, where P == predict_from.
+#' \code{forecast$getForecast(i)} output forecasts for
+#' \code{data$getSerie(forecast$getIndexInData(i))}.
+#'
+#' @usage # Forecast$new(dates)
#'
-#' @field .pred List with \itemize{
-#' \item serie: forecasted serie
+#' @field .pred List with
+#' \itemize{
+#' \item serie: the forecasted serie
#' \item params: corresponding list of parameters (weights, neighbors...)
-#' \item index: corresponding index in data object}
-#' @field .dates vector of day indices where forcast occurs
+#' \item index_in_data: corresponding index in data object
+#' }
+#' @field .dates vector of (integer) day indices where forecast occurs
+#'
+#' @section Methods:
+#' \describe{
+#' \item{\code{initialize(dates)}}{
+#' Initialize a Forecast object with a series of date indices.}
+#' \item{\code{getSize()}}{
+#' Return number of individual forecasts.}
+#' \item{\code{append(forecast, params, index_in_data)}}{
+#' Acquire an individual forecast, with its (optimized) parameters and the
+#' corresponding index in the dataset.}
+#' \item{\code{getDates()}}{
+#' Get dates where forecast occurs.}
+#' \item{\code{getForecast(index)}}{
+#' Get forecasted serie at specified index.}
+#' \item{\code{getParams(index)}}{
+#' Get parameters at specified index (for 'Neighbors' method).}
+#' \item{\code{getIndexInData(index)}}{
+#' Get index in data which corresponds to current forecast.}
+#' }
+#'
+#' @docType class
+#' @format R6 class
#'
-#' @section Methods: \describe{
-#' \item{\code{initialize(dates)}}
-#' {Initialize a Forecast object with a series of date indices.}
-#' \item{\code{predictSerie(today,memory,horizon,...)}}
-#' {Predict a new serie of \code{horizon} values at day index \code{today} using \code{memory}
-#' days in the past.} TODO: continue #######################################
-#' \item{\code{predictShape(today,memory,horizon,...)}}
-#' {Predict a new shape of \code{horizon} values at day index \code{today} using \code{memory}
-#' days in the past.}
-#' \item{\code{getParameters()}}
-#' {Return (internal) parameters.} }
Forecast = R6::R6Class("Forecast",
private = list(
.pred = list(),
- .dates = c()
+ .dates = integer(0) #store dates as integers (from 1970-01-01)
),
public = list(
initialize = function(dates)
- initializeForecast(self, private, dates)
- ,
+ {
+ private$.dates <- dates
+ invisible(self)
+ },
getSize = function()
- getSizeForecast(private)
- ,
- append = function(new_serie, new_params, new_index)
- appendForecast(private, new_serie, new_params, new_index)
+ length(private$.pred)
,
+ append = function(forecast, params, index_in_data)
+ {
+ private$.pred[[length(private$.pred)+1]] <-
+ list("forecast"=forecast, "params"=params, "index_in_data"=index_in_data)
+ },
getDates = function()
- getDatesForecast(private)
- ,
- getSerie = function(index)
- getSerieForecast(private, index)
+ as.Date( private$.dates, origin="1970-01-01" )
,
+ getForecast = function(index)
+ {
+ if (is(index,"Date"))
+ index = match(index, private$.dates)
+ private$.pred[[index]]$forecast
+ },
getParams = function(index)
- getParamsForecast(private, index)
- ,
+ {
+ if (is(index,"Date"))
+ index = match(index, private$.dates)
+ private$.pred[[index]]$params
+ },
getIndexInData = function(index)
- getIndexInDataForecast(private, index)
+ {
+ if (is(index,"Date"))
+ index = match(index, private$.dates)
+ private$.pred[[index]]$index_in_data
+ }
)
)
-
-#' Initialize empty Forecast object
-#'
-#' @param o Object of class Forecast
-#' @param private List of private members in o
-#' @param dates vector of dates where forecast occurs
-initializeForecast = function(o, private, dates)
-{
- private$.dates <- dates
- invisible(o)
-}
-
-#' Number of individual forecasts"
-#'
-#' @inheritParams initializeForecast
-getSizeForecast = function(private)
- length(private$.pred)
-
-#' Obtain a new pair (serie, params)"
-#'
-#' @inheritParams initializeForecast
-#' @param new_serie Values of a new serie
-#' @param new_params Associated (optimized) parameters
-#' @param new_index_in_data Corresponding index in data
-appendForecast = function(private, new_serie, new_params, new_index_in_data)
-{
- private$.pred[[length(private$.pred)+1]] <-
- list("serie"=new_serie, "params"=new_params, "index_in_data"=new_index_in_data)
-}
-
-#' Dates where prediction occurs
-#'
-#' inheritParams initializeForecast
-getDatesForecast = function(private)
- private$.dates
-
-#' Serie values at specified index"
-#'
-#' @inheritParams initializeForecast
-#' @param index Return value at this index
-getSerieForecast = function(index)
-{
- if (is(index,"Date"))
- index = match(index, private$.dates)
- private$.pred[[index]]$serie
-}
-
-#' Params at specified index"
-#'
-#' @inheritParams getSerieForecast
-getParamsForecast = function(index)
-{
- if (is(index,"Date"))
- index = match(index, private$.dates)
- private$.pred[[index]]$params
-}
-
-#' (day) Index in data where prediction took place"
-#'
-#' @inheritParams getSerieForecast
-getIndexInDataForecast = function(index)
-{
- if (is(index,"Date"))
- index = match(index, private$.dates)
- private$.pred[[index]]$index_in_data
-}