From 546b0cb65870355a2a2c3705c91418570499d3a6 Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Thu, 16 Mar 2017 14:47:53 +0100 Subject: [PATCH] storage by-rows --- pkg/DESCRIPTION | 2 +- pkg/R/Data.R | 1 + pkg/R/F_Average.R | 1 + pkg/R/F_Neighbors.R | 28 ++++++++++++++++++++-------- pkg/R/F_Persistence.R | 1 + pkg/R/F_Zero.R | 1 + pkg/R/Forecast.R | 1 + pkg/R/Forecaster.R | 4 +++- pkg/R/J_Neighbors.R | 1 + pkg/R/J_Persistence.R | 1 + pkg/R/J_Zero.R | 1 + pkg/R/utils.R | 11 ----------- 12 files changed, 32 insertions(+), 21 deletions(-) diff --git a/pkg/DESCRIPTION b/pkg/DESCRIPTION index e83e31e..7fb20cc 100644 --- a/pkg/DESCRIPTION +++ b/pkg/DESCRIPTION @@ -21,7 +21,7 @@ Suggests: LazyData: yes URL: http://git.auder.net/?p=talweg.git License: MIT + file LICENSE -RoxygenNote: 5.0.1 +RoxygenNote: 6.0.1 Collate: 'Data.R' 'Forecaster.R' diff --git a/pkg/R/Data.R b/pkg/R/Data.R index 42e8c69..697da05 100644 --- a/pkg/R/Data.R +++ b/pkg/R/Data.R @@ -37,6 +37,7 @@ #' \item{\code{getExoHat(index)}}{ #' Get estimated exogenous variables at specified index.} #' } +#' Data = R6::R6Class("Data", private = list( .data = list() diff --git a/pkg/R/F_Average.R b/pkg/R/F_Average.R index 7ec4e90..c28125e 100644 --- a/pkg/R/F_Average.R +++ b/pkg/R/F_Average.R @@ -4,6 +4,7 @@ #' #' Return the (pointwise) average of the all the (similar) centered day curves #' in the past. Inherits \code{\link{Forecaster}} +#' AverageForecaster = R6::R6Class("AverageForecaster", inherit = Forecaster, diff --git a/pkg/R/F_Neighbors.R b/pkg/R/F_Neighbors.R index 202b7e2..a3d44a3 100644 --- a/pkg/R/F_Neighbors.R +++ b/pkg/R/F_Neighbors.R @@ -4,6 +4,7 @@ #' #' Predict tomorrow as a weighted combination of "futures of the past" days. #' Inherits \code{\link{Forecaster}} +#' NeighborsForecaster = R6::R6Class("NeighborsForecaster", inherit = Forecaster, @@ -100,17 +101,22 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster", { delta = data$getCenteredSerie(today) - data$getCenteredSerie(fdays[i]) # Require at least half of non-NA common values to compute the distance - if (sum(is.na(delta)) <= 0) #length(delta)/2) - distances2[i] = mean(delta^2) #, na.rm=TRUE) + if ( !any( is.na(delta) ) ) + distances2[i] = mean(delta^2) } sd_dist = sd(distances2) if (sd_dist < .Machine$double.eps) + { + warning("All computed distances are very close: stdev too small") sd_dist = 1 #mostly for tests... FIXME: + } simils_endo = if (kernel=="Gauss") exp(-distances2/(sd_dist*h_endo^2)) - else { #Epanechnikov + else + { + # Epanechnikov u = 1 - distances2/(sd_dist*h_endo^2) u[abs(u)>1] = 0. u @@ -138,10 +144,17 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster", } sd_dist = sd(distances2) + if (sd_dist < .Machine$double.eps) + { + warning("All computed distances are very close: stdev too small") + sd_dist = 1 #mostly for tests... FIXME: + } simils_exo = if (kernel=="Gauss") exp(-distances2/(sd_dist*h_exo^2)) - else { #Epanechnikov + else + { + # Epanechnikov u = 1 - distances2/(sd_dist*h_exo^2) u[abs(u)>1] = 0. u @@ -166,13 +179,12 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster", private$.params$weights <- similarities private$.params$indices <- fdays private$.params$window <- - if (simtype=="endo") { + if (simtype=="endo") h_endo - } else if (simtype=="exo") { + else if (simtype=="exo") h_exo - } else { #mix + else #mix c(h_endo,h_exo) - } } return (prediction) diff --git a/pkg/R/F_Persistence.R b/pkg/R/F_Persistence.R index 220e04c..1d9fd19 100644 --- a/pkg/R/F_Persistence.R +++ b/pkg/R/F_Persistence.R @@ -4,6 +4,7 @@ #' #' Return the last centered (similar) day curve. #' Inherits \code{\link{Forecaster}} +#' PersistenceForecaster = R6::R6Class("PersistenceForecaster", inherit = Forecaster, diff --git a/pkg/R/F_Zero.R b/pkg/R/F_Zero.R index 4f57ba2..f5d6b89 100644 --- a/pkg/R/F_Zero.R +++ b/pkg/R/F_Zero.R @@ -3,6 +3,7 @@ #' Zero Forecaster #' #' Return 0 (and then adjust). Inherits \code{\link{Forecaster}} +#' ZeroForecaster = R6::R6Class("ZeroForecaster", inherit = Forecaster, diff --git a/pkg/R/Forecast.R b/pkg/R/Forecast.R index bcafd3a..c64d24f 100644 --- a/pkg/R/Forecast.R +++ b/pkg/R/Forecast.R @@ -29,6 +29,7 @@ #' \item{\code{getIndexInData(index)}}{ #' Get index in data which corresponds to current forecast.} #' } +#' Forecast = R6::R6Class("Forecast", private = list( .pred = list(), diff --git a/pkg/R/Forecaster.R b/pkg/R/Forecaster.R index da8579b..cedb2b6 100644 --- a/pkg/R/Forecaster.R +++ b/pkg/R/Forecaster.R @@ -19,7 +19,9 @@ #' 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(), diff --git a/pkg/R/J_Neighbors.R b/pkg/R/J_Neighbors.R index 33ba00d..3c9bc30 100644 --- a/pkg/R/J_Neighbors.R +++ b/pkg/R/J_Neighbors.R @@ -2,6 +2,7 @@ #' #' @inheritParams computeForecast #' @inheritParams getZeroJumpPredict +#' getNeighborsJumpPredict = function(data, today, memory, horizon, params, ...) { first_day = max(1, today-memory) diff --git a/pkg/R/J_Persistence.R b/pkg/R/J_Persistence.R index 7a7daef..5d156cc 100644 --- a/pkg/R/J_Persistence.R +++ b/pkg/R/J_Persistence.R @@ -2,6 +2,7 @@ #' #' @inheritParams computeForecast #' @inheritParams getZeroJumpPredict +#' getPersistenceJumpPredict = function(data, today, memory, horizon, params, ...) { #return gap between end of similar day curve and first day of tomorrow (in the past) diff --git a/pkg/R/J_Zero.R b/pkg/R/J_Zero.R index 3c6af47..d9140a5 100644 --- a/pkg/R/J_Zero.R +++ b/pkg/R/J_Zero.R @@ -3,6 +3,7 @@ #' @inheritParams computeForecast #' @param today Index of the current day (predict tomorrow) #' @param params Optional parameters computed by the main forecaster +#' getZeroJumpPredict = function(data, today, memory, horizon, params, ...) { 0 diff --git a/pkg/R/utils.R b/pkg/R/utils.R index 64c3c0a..3486ee9 100644 --- a/pkg/R/utils.R +++ b/pkg/R/utils.R @@ -98,17 +98,6 @@ getSimilarDaysIndices = function(index, limit, same_season) return ( days[1:min(limit,length(days))] ) } -#' getSerie -#' -#' Return a time-serie from its centered version + level -#' -#' @param data A list as returned by \code{getData} -#' @param index The index to return -#' -#' @export -getSerie = function(data, index) - data[[index]]$centered_serie + data[[index]]$level - #' getNoNA2 #' #' Get indices in data of no-NA series followed by no-NA, within [first,last] range. -- 2.44.0