intermediate: R6, too slow
[talweg.git] / pkg / R / Forecast.R
index 274f50e..b68b6c9 100644 (file)
@@ -1,6 +1,6 @@
-#' @title Forecast
+#' Forecast
 #'
-#' @description Forecast encapsulation
+#' Forecast encapsulation
 #'
 #' @field pred List with
 #' \itemize{
@@ -9,51 +9,99 @@
 #'   \item index: corresponding index in data object
 #' }
 #'
-#' @exportClass Forecast
-#' @export Forecast
-Forecast = setRefClass(
-       Class = "Forecast",
-
-       fields = list(
-               pred = "list"
+#' @docType class
+#' @importFrom R6 R6Class
+#'
+#' @export
+Forecast = R6::R6Class("Forecast",
+       private = list(
+               .pred = "list",
+               .dates = "Date"
        ),
-
-       methods = list(
-               initialize = function(...)
-               {
-                       "Initialize empty Forecast object"
-
-                       callSuper(...)
-               },
+       public = list(
+               initialize = function(dates)
+                       initialize(self, private, dates)
+               ,
                getSize = function()
-               {
-                       "Number of individual forecasts"
-
-                       length(pred)
-               },
+                       getSize(private)
+               ,
                append = function(new_serie, new_params, new_index)
-               {
-                       "Obtain a new pair (serie, params)"
-
-                       pred[[length(pred)+1]] <<- list("serie"=new_serie, "params"=new_params, "index"=new_index)
-               },
+                       append(private, new_serie, new_params, new_index)
+               ,
+               getDates = function()
+                       getDates(private)
+               ,
                getSerie = function(index)
-               {
-                       "Serie values at specified index"
-
-                       pred[[index]]$serie
-               },
+                       getSerie(private, index)
+               ,
                getParams = function(index)
-               {
-                       "Params at specified index"
-
-                       pred[[index]]$params
-               },
+                       getParams(private, index)
+               ,
                getIndexInData = function(index)
-               {
-                       "(day) Index in data where prediction took place"
-
-                       pred[[index]]$index
-               }
+                       getIndexInData(private, index)
        )
 )
+
+#' 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
+initialize = function(o, private, dates)
+{
+       private$.dates <<- dates
+       private$.pred <<- list()
+       invisible(o)
+}
+
+#' Number of individual forecasts"
+#'
+#' @inheritParams initialize
+getSize = function(private)
+       length(private$.pred)
+
+#' Obtain a new pair (serie, params)"
+#'
+#' @inheritParams initialize
+append = function(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 initialize
+getDates = function(private)
+       private$.dates
+
+#' Serie values at specified index"
+#'
+#' @inheritParams initialize
+#' @param index Return value at this index
+getSerie = function(index)
+{
+       if (is(index,"Date"))
+               index = match(index, private$.dates)
+       private$.pred[[index]]$serie
+}
+
+#' Params at specified index"
+#'
+#' @inheritParams getSerie
+getParams = function(index)
+{
+       if (is(index,"Date"))
+               index = match(index, private$.dates)
+       private$.pred[[index]]$params
+}
+
+#' (day) Index in data where prediction took place"
+#'
+#' @inheritParams getSerie
+getIndexInData = function(index)
+{
+       if (is(index,"Date"))
+               index = match(index, private$.dates)
+       private$.pred[[index]]$index_in_data
+}