X-Git-Url: https://git.auder.net/?p=talweg.git;a=blobdiff_plain;f=pkg%2FR%2FF_Neighbors2.R;h=787dd2b62b69af25c01cfca2de541a6939955f30;hp=83ef453093ed5b4430c1ef1cbbf5ace2ebc95902;hb=ee8b1b4e3c13f8dcf13a2c8da6a3bef1520c8252;hpb=a866acb3c0ae138b22df9dae9ec576b866794417 diff --git a/pkg/R/F_Neighbors2.R b/pkg/R/F_Neighbors2.R index 83ef453..787dd2b 100644 --- a/pkg/R/F_Neighbors2.R +++ b/pkg/R/F_Neighbors2.R @@ -9,15 +9,11 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", inherit = Forecaster, public = list( -# predictSerie = function(data, today, memory, horizon, ...) -# { -# # Parameters (potentially) computed during shape prediction stage -# predicted_shape = self$predictShape(data, today, memory, horizon, ...) -## predicted_delta = private$.pjump(data,today,memory,horizon,private$.params,...) -# # Predicted shape is aligned it on the end of current day + jump -## predicted_shape+tail(data$getSerie(today),1)-predicted_shape[1]+predicted_delta -# predicted_shape -# }, + predictSerie = function(data, today, memory, horizon, ...) + { + # This method predict shape + level at the same time, all in next call + self$predictShape(data, today, memory, horizon, ...) + }, predictShape = function(data, today, memory, horizon, ...) { # (re)initialize computed parameters @@ -40,7 +36,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", } # Indices of similar days for cross-validation; TODO: 45 = magic number - sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE) + sdays = getSimilarDaysIndices(today, data, limit=45, same_season=FALSE) cv_days = intersect(fdays,sdays) # Limit to 20 most recent matching days (TODO: 20 == magic number) @@ -101,24 +97,25 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", { fdays = fdays[ fdays < today ] # TODO: 3 = magic number - if (length(fdays) < 1) + if (length(fdays) < 3) return (NA) # Neighbors: days in "same season" - sdays = getSimilarDaysIndices(today, limit=45, same_season=TRUE, data) + sdays = getSimilarDaysIndices(today, data, limit=45, same_season=TRUE) indices = intersect(fdays,sdays) + if (length(indices) <= 1) + return (NA) levelToday = data$getLevel(today) distances = sapply(indices, function(i) abs(data$getLevel(i)-levelToday)) + # 2 and 5 below == magic numbers (determined by Bruno & Michel) same_pollution = (distances <= 2) - if (sum(same_pollution) < 1) #TODO: 3 == magic number + if (sum(same_pollution) == 0) { same_pollution = (distances <= 5) - if (sum(same_pollution) < 1) + if (sum(same_pollution) == 0) return (NA) } indices = indices[same_pollution] - - #TODO: we shouldn't need that block if (length(indices) == 1) { if (final_call) @@ -169,8 +166,12 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", M[i+1,] = c( data$getLevel(indices[i]), as.double(data$getExo(indices[i])) ) sigma = cov(M) #NOTE: robust covariance is way too slow -# sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed? - sigma_inv = MASS::ginv(sigma) + # TODO: 10 == magic number; more robust way == det, or always ginv() + sigma_inv = + if (length(indices) > 10) + solve(sigma) + else + MASS::ginv(sigma) # Distances from last observed day to days in the past distances2 = sapply(seq_along(indices), function(i) {