From 0f496a449aee90a70d8d0fa4f5074e38a051b3a4 Mon Sep 17 00:00:00 2001 From: Norbert Kuder Date: Sat, 18 Jan 2025 12:19:06 +0100 Subject: [PATCH 1/2] blockstrapping --- R/RSTR.R | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/R/RSTR.R b/R/RSTR.R index aeaf0e1..09d79c5 100644 --- a/R/RSTR.R +++ b/R/RSTR.R @@ -75,6 +75,24 @@ getLowerUpperRSTR <- function(m, confidence) { #' @author Alexander Dokumentov #' @export +block_bootstrap <- function(residuals, n = length(residuals), block_length = NULL) { + if (is.null(block_length)) { + block_length <- max(floor(n^(1/3)), 1) + } + num_blocks <- ceiling(n / block_length) + + start_indices <- sample(seq_len(n), size = num_blocks, replace = TRUE) + block_offsets <- rep(seq_len(block_length) - 1, times = num_blocks) + expanded_starts <- rep(start_indices, each = block_length) + indices <- ((expanded_starts + block_offsets) - 1) %% n + 1 + + result <- residuals[indices] + if (length(result) > n) { + result <- result[1:n] + } + return(result) +} + RSTRmodel <- function(data, predictors = NULL, strDesign = NULL, lambdas = NULL, confidence = NULL, # confidence = c(0.8, 0.95) nMCIter = 100, @@ -130,7 +148,7 @@ RSTRmodel <- function(data, predictors = NULL, strDesign = NULL, lambdas = NULL, cat(i) } - rand <- sample(res) # TODO: Autocorrelation is lost here + rand <- block_bootstrap(res, n = length(res), block_length = NULL) dy <- rand - res suppressWarnings({ dFit <- rq.fit.sfn(X.csr, y = c(dy, rep(0, nrow(X) - length(dy))), control = control) From 05dd52a16366f616e8aa23df6e2eed44ad433747 Mon Sep 17 00:00:00 2001 From: Norbert Kuder Date: Sun, 19 Jan 2025 21:12:55 +0100 Subject: [PATCH 2/2] block_bootstrap moved --- R/RSTR.R | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/R/RSTR.R b/R/RSTR.R index 09d79c5..4346963 100644 --- a/R/RSTR.R +++ b/R/RSTR.R @@ -16,6 +16,24 @@ getLowerUpperRSTR <- function(m, confidence) { return(list(lower = lu[, 1:(ncol(lu) / 2), drop = FALSE], upper = lu[, (ncol(lu) / 2 + 1):ncol(lu), drop = FALSE])) } +block_bootstrap <- function(residuals, n = length(residuals), block_length = NULL) { + if (is.null(block_length)) { + block_length <- max(floor(n^(1/3)), 1) + } + num_blocks <- ceiling(n / block_length) + + start_indices <- sample(seq_len(n), size = num_blocks, replace = TRUE) + block_offsets <- rep(seq_len(block_length) - 1, times = num_blocks) + expanded_starts <- rep(start_indices, each = block_length) + indices <- ((expanded_starts + block_offsets) - 1) %% n + 1 + + result <- residuals[indices] + if (length(result) > n) { + result <- result[1:n] + } + return(result) +} + #' @title Robust STR decomposition #' @description Robust Seasonal-Trend decomposition of time series data using Regression (robust version of \code{\link{STRmodel}}). #' @seealso \code{\link{STRmodel}} \code{\link{STR}} @@ -75,24 +93,6 @@ getLowerUpperRSTR <- function(m, confidence) { #' @author Alexander Dokumentov #' @export -block_bootstrap <- function(residuals, n = length(residuals), block_length = NULL) { - if (is.null(block_length)) { - block_length <- max(floor(n^(1/3)), 1) - } - num_blocks <- ceiling(n / block_length) - - start_indices <- sample(seq_len(n), size = num_blocks, replace = TRUE) - block_offsets <- rep(seq_len(block_length) - 1, times = num_blocks) - expanded_starts <- rep(start_indices, each = block_length) - indices <- ((expanded_starts + block_offsets) - 1) %% n + 1 - - result <- residuals[indices] - if (length(result) > n) { - result <- result[1:n] - } - return(result) -} - RSTRmodel <- function(data, predictors = NULL, strDesign = NULL, lambdas = NULL, confidence = NULL, # confidence = c(0.8, 0.95) nMCIter = 100,