X-Git-Url: https://git.auder.net/?p=talweg.git;a=blobdiff_plain;f=pkg%2FR%2FF_Neighbors2.R;h=83ef453093ed5b4430c1ef1cbbf5ace2ebc95902;hp=fb63e4087f1b2aa4b426712d140132a2f2e83193;hb=a866acb3c0ae138b22df9dae9ec576b866794417;hpb=5e838b3e17465c376ca075b766cf2543c82e9864 diff --git a/pkg/R/F_Neighbors2.R b/pkg/R/F_Neighbors2.R index fb63e40..83ef453 100644 --- a/pkg/R/F_Neighbors2.R +++ b/pkg/R/F_Neighbors2.R @@ -9,15 +9,15 @@ 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, ...) +# { +# # 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 +# }, predictShape = function(data, today, memory, horizon, ...) { # (re)initialize computed parameters @@ -101,7 +101,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", { fdays = fdays[ fdays < today ] # TODO: 3 = magic number - if (length(fdays) < 3) + if (length(fdays) < 1) return (NA) # Neighbors: days in "same season" @@ -110,14 +110,26 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", levelToday = data$getLevel(today) distances = sapply(indices, function(i) abs(data$getLevel(i)-levelToday)) same_pollution = (distances <= 2) - if (sum(same_pollution) < 3) #TODO: 3 == magic number + if (sum(same_pollution) < 1) #TODO: 3 == magic number { same_pollution = (distances <= 5) - if (sum(same_pollution) < 3) + if (sum(same_pollution) < 1) return (NA) } indices = indices[same_pollution] + #TODO: we shouldn't need that block + 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") { h_endo = ifelse(simtype=="mix", h[1], h) @@ -159,7 +171,7 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", 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() + # 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 +212,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