fixes and improvements
[talweg.git] / R / S_Persistence.R
index 017402f..08647e3 100644 (file)
@@ -2,8 +2,8 @@
 #'
 #' @title Persistence Shape Forecaster
 #'
-#' @description Return the last centered last (similar) day curve (may be a lot of NAs,
-#'   but we cannot do better). Inherits \code{\link{ShapeForecaster}}
+#' @description Return the last centered last (similar) day curve.
+#'   Inherits \code{\link{ShapeForecaster}}
 PersistenceShapeForecaster = setRefClass(
        Class = "PersistenceShapeForecaster",
        contains = "ShapeForecaster",
@@ -15,11 +15,20 @@ PersistenceShapeForecaster = setRefClass(
                },
                predict = function(today, memory, horizon, ...)
                {
-                       #return centered last (similar) day curve (may be a lot of NAs, but we cannot do better)
-                       last_similar_serie = data$getCenteredSerie(today-6)[1:horizon]
-                       if (any(is.na(last_similar_serie))) #TODO:
-                               return (NA)
-                       last_similar_serie
+                       #return centered last (similar) day curve, avoiding NAs until memory is run
+                       first_day = max(1, today-memory)
+                       index = today-7 + 1
+                       repeat
+                       {
+                               {
+                                       last_similar_serie = data$getCenteredSerie(index)[1:horizon]
+                                       index = index - 7
+                               };
+                               if (!any(is.na(last_similar_serie)))
+                                       return (last_similar_serie);
+                               if (index < first_day)
+                                       return (NA)
+                       }
                }
        )
 )