X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=pkg%2FR%2FF_Neighbors2.R;h=787dd2b62b69af25c01cfca2de541a6939955f30;hb=ee8b1b4e3c13f8dcf13a2c8da6a3bef1520c8252;hp=fb63e4087f1b2aa4b426712d140132a2f2e83193;hpb=5e838b3e17465c376ca075b766cf2543c82e9864;p=talweg.git diff --git a/pkg/R/F_Neighbors2.R b/pkg/R/F_Neighbors2.R index fb63e40..787dd2b 100644 --- a/pkg/R/F_Neighbors2.R +++ b/pkg/R/F_Neighbors2.R @@ -11,12 +11,8 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", 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 + # 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, ...) { @@ -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) @@ -105,18 +101,31 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", 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) < 3) #TODO: 3 == magic number + if (sum(same_pollution) == 0) { same_pollution = (distances <= 5) - if (sum(same_pollution) < 3) + if (sum(same_pollution) == 0) return (NA) } indices = indices[same_pollution] + if (length(indices) == 1) + { + if (final_call) + { + private$.params$weights <- 1 + private$.params$indices <- indices + private$.params$window <- 1 + } + return ( data$getSerie(indices[1])[1:horizon] ) #what else?! + } if (simtype != "exo") { @@ -157,9 +166,13 @@ 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) -#if (final_call) browser() + # 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) { delta = M[1,] - M[i+1,] @@ -200,7 +213,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", if (final_call) { private$.params$weights <- similarities - private$.params$indices <- fdays + private$.params$indices <- indices private$.params$window <- if (simtype=="endo") h_endo