Commit | Line | Data |
---|---|---|
25b75559 | 1 | #' Forecast |
3d69ff21 | 2 | #' |
25b75559 | 3 | #' Forecast encapsulation |
3d69ff21 | 4 | #' |
25b75559 BA |
5 | #' @docType class |
6 | #' @importFrom R6 R6Class | |
7 | #' | |
a66a84b5 BA |
8 | #' @field .pred List with \itemize{ |
9 | #' \item serie: forecasted serie | |
10 | #' \item params: corresponding list of parameters (weights, neighbors...) | |
11 | #' \item index: corresponding index in data object} | |
12 | #' @field .dates vector of day indices where forcast occurs | |
13 | #' | |
14 | #' @section Methods: \describe{ | |
15 | #' \item{\code{initialize(dates)}} | |
16 | #' {Initialize a Forecast object with a series of date indices.} | |
17 | #' \item{\code{predictSerie(today,memory,horizon,...)}} | |
18 | #' {Predict a new serie of \code{horizon} values at day index \code{today} using \code{memory} | |
19 | #' days in the past.} TODO: continue ####################################### | |
20 | #' \item{\code{predictShape(today,memory,horizon,...)}} | |
21 | #' {Predict a new shape of \code{horizon} values at day index \code{today} using \code{memory} | |
22 | #' days in the past.} | |
23 | #' \item{\code{getParameters()}} | |
24 | #' {Return (internal) parameters.} } | |
25b75559 BA |
25 | Forecast = R6::R6Class("Forecast", |
26 | private = list( | |
a66a84b5 BA |
27 | .pred = list(), |
28 | .dates = c() | |
3d69ff21 | 29 | ), |
25b75559 BA |
30 | public = list( |
31 | initialize = function(dates) | |
a66a84b5 | 32 | initializeForecast(self, private, dates) |
25b75559 | 33 | , |
1e20780e | 34 | getSize = function() |
a66a84b5 | 35 | getSizeForecast(private) |
25b75559 | 36 | , |
3d69ff21 | 37 | append = function(new_serie, new_params, new_index) |
a66a84b5 | 38 | appendForecast(private, new_serie, new_params, new_index) |
25b75559 BA |
39 | , |
40 | getDates = function() | |
a66a84b5 | 41 | getDatesForecast(private) |
25b75559 | 42 | , |
3d69ff21 | 43 | getSerie = function(index) |
a66a84b5 | 44 | getSerieForecast(private, index) |
25b75559 | 45 | , |
3d69ff21 | 46 | getParams = function(index) |
a66a84b5 | 47 | getParamsForecast(private, index) |
25b75559 | 48 | , |
3d69ff21 | 49 | getIndexInData = function(index) |
a66a84b5 | 50 | getIndexInDataForecast(private, index) |
3d69ff21 BA |
51 | ) |
52 | ) | |
25b75559 BA |
53 | |
54 | #' Initialize empty Forecast object | |
55 | #' | |
56 | #' @param o Object of class Forecast | |
57 | #' @param private List of private members in o | |
58 | #' @param dates vector of dates where forecast occurs | |
a66a84b5 | 59 | initializeForecast = function(o, private, dates) |
25b75559 | 60 | { |
a66a84b5 | 61 | private$.dates <- dates |
25b75559 BA |
62 | invisible(o) |
63 | } | |
64 | ||
65 | #' Number of individual forecasts" | |
66 | #' | |
a66a84b5 BA |
67 | #' @inheritParams initializeForecast |
68 | getSizeForecast = function(private) | |
25b75559 BA |
69 | length(private$.pred) |
70 | ||
71 | #' Obtain a new pair (serie, params)" | |
72 | #' | |
a66a84b5 BA |
73 | #' @inheritParams initializeForecast |
74 | #' @param new_serie Values of a new serie | |
75 | #' @param new_params Associated (optimized) parameters | |
76 | #' @param new_index_in_data Corresponding index in data | |
77 | appendForecast = function(private, new_serie, new_params, new_index_in_data) | |
25b75559 | 78 | { |
a66a84b5 | 79 | private$.pred[[length(private$.pred)+1]] <- |
25b75559 BA |
80 | list("serie"=new_serie, "params"=new_params, "index_in_data"=new_index_in_data) |
81 | } | |
82 | ||
83 | #' Dates where prediction occurs | |
84 | #' | |
a66a84b5 BA |
85 | #' inheritParams initializeForecast |
86 | getDatesForecast = function(private) | |
25b75559 BA |
87 | private$.dates |
88 | ||
89 | #' Serie values at specified index" | |
90 | #' | |
a66a84b5 | 91 | #' @inheritParams initializeForecast |
25b75559 | 92 | #' @param index Return value at this index |
a66a84b5 | 93 | getSerieForecast = function(index) |
25b75559 BA |
94 | { |
95 | if (is(index,"Date")) | |
96 | index = match(index, private$.dates) | |
97 | private$.pred[[index]]$serie | |
98 | } | |
99 | ||
100 | #' Params at specified index" | |
101 | #' | |
a66a84b5 BA |
102 | #' @inheritParams getSerieForecast |
103 | getParamsForecast = function(index) | |
25b75559 BA |
104 | { |
105 | if (is(index,"Date")) | |
106 | index = match(index, private$.dates) | |
107 | private$.pred[[index]]$params | |
108 | } | |
109 | ||
110 | #' (day) Index in data where prediction took place" | |
111 | #' | |
a66a84b5 BA |
112 | #' @inheritParams getSerieForecast |
113 | getIndexInDataForecast = function(index) | |
25b75559 BA |
114 | { |
115 | if (is(index,"Date")) | |
116 | index = match(index, private$.dates) | |
117 | private$.pred[[index]]$index_in_data | |
118 | } |