Validates RR intervals using a moving average filter, flagging potentially erroneous intervals.

moving_average_rr_validation(
  rr_segment,
  is_valid = rep(TRUE, length(rr_segment)),
  window_size = 7,
  threshold = 0.2,
  centered_window = FALSE
)

Arguments

rr_segment

A numeric vector of RR intervals (in milliseconds).

is_valid

A logical vector indicating which intervals are valid initially. Defaults to all TRUE.

window_size

An integer specifying the size of the moving window (must be odd). Defaults to 7.

threshold

A numeric value representing the maximum allowed proportional deviation from the moving median. Defaults to 0.2 (20%).

centered_window

A logical value indicating whether the moving window should be centered. Defaults to FALSE.

Value

A list containing two elements:

rr_segment

A numeric vector containing only the valid RR intervals.

Details

Moving Average Filter: A moving median is calculated for each RR interval using a window of size window_size. The current RR interval is excluded from the median calculation. The proportional deviation is calculated. If this deviation exceeds the threshold, the interval is marked as invalid. Only a priori valid intervals within the window are used. If all intervals within the window (excluding current) are invalid, the validity remains unchanged.

The moving window is centered. At the boundaries, the window is truncated.

Examples

# Sample RR interval data (in seconds)
rr_data <- c(
  0.8, 0.9, 0.7, 1.5, 0.85, 0.95, 0.88, 0.92, 0.8,
  4.0
)

# Validate the RR intervals
result <- moving_average_rr_validation(rr_data)

# Print the results
print(result$is_valid)
#>  [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
print(result$cleaned_rr)
#> [1] 0.80 0.90 0.70 0.85 0.95 0.88 0.92 0.80

# Example with pre-existing invalid intervals
is_valid_initial <- c(
  TRUE, TRUE, FALSE, TRUE, TRUE, TRUE,
  TRUE, TRUE, TRUE, TRUE
)
result2 <- moving_average_rr_validation(rr_data,
  is_valid = is_valid_initial,
  window_size = 5
)
print(result2)
#> $is_valid
#>  [1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
#> 
#> $cleaned_rr
#> [1] 0.80 0.90 0.85 0.95 0.88 0.92 0.80
#>