Fix R6 classes
authorBenjamin Auder <benjamin.auder@somewhere>
Mon, 27 Feb 2017 16:43:56 +0000 (17:43 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Mon, 27 Feb 2017 16:43:56 +0000 (17:43 +0100)
pkg/R/Data.R
pkg/R/Forecast.R
pkg/R/Forecaster.R

index 677d906..e33e480 100644 (file)
@@ -30,7 +30,7 @@
 #' \item{\code{getExo(index)}}
 #'   {Get exogenous variables at specified index.}
 #' \item{\code{getExoHat(index)}}
-#'   {Get estimated exogenous variables at specified index.} }
+#'   {Get estimated exogenous variables at specified index.}}
 Data = R6::R6Class("Data",
        private = list(
                .data = list()
@@ -42,7 +42,7 @@ Data = R6::R6Class("Data",
                getStdHorizon = function()
                        24 - as.POSIXlt( private$.data[[1]]$time[1] )$hour + 1
                ,
-               append = function(
+               append = function(new_time, new_centered_serie, new_level, new_exo, new_exo_hat)
                {
                        private$.data[[length(private$.data)+1]] <- list(
                                "time"=new_time, "centered_serie"=new_centered_serie, "level"=new_level,
index f1b92ce..783e352 100644 (file)
 #' @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.} }
+#' \item{\code{getSize()}}
+#'   {Return number of individual forecasts.}
+#' \item{\code{append(new_serie, new_params, new_index_in_data)}}
+#'   {Acquire a new individual forecast, with its (optimized) parameters and the corresponding
+#'   index in the dataset.}
+#' \item{\code{getDates()}}
+#'   {Get dates where forecast occurs.}
+#' \item{\code{getSerie(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.}}
 Forecast = R6::R6Class("Forecast",
        private = list(
                .pred = list(),
@@ -29,90 +34,38 @@ Forecast = R6::R6Class("Forecast",
        ),
        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(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)
+               },
                getDates = function()
-                       getDatesForecast(private)
+                       private$.dates
                ,
                getSerie = function(index)
-                       getSerieForecast(private, index)
-               ,
+               {
+                       if (is(index,"Date"))
+                               index = match(index, private$.dates)
+                       private$.pred[[index]]$serie
+               },
                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
-}
index 2bd2e4e..47160b5 100644 (file)
@@ -19,7 +19,7 @@
 #'   {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.} }
+#'   {Return (internal) parameters.}}
 Forecaster = R6::R6Class("Forecaster",
        private = list(
                .params = list(),
@@ -36,13 +36,13 @@ Forecaster = R6::R6Class("Forecaster",
                predictSerie = function(today, memory, horizon, ...)
                {
                        # Parameters (potentially) computed during shape prediction stage
-                       predicted_shape = o$predictShape(today, memory, horizon, ...)
+                       predicted_shape = self$predictShape(today, memory, horizon, ...)
                        predicted_delta = private$.pjump(private$.data,today,memory,horizon,private$.params,...)
                        # Predicted shape is aligned it on the end of current day + jump
                        predicted_shape+tail(private$.data$getSerie(today),1)-predicted_shape[1]+predicted_delta
                },
                predictShape = function(today, memory, horizon, ...)
-                       #empty default implementation: to implement in inherited classes
+                       NULL #empty default implementation: to implement in inherited classes
                ,
                getParameters = function()
                        private$.params