This function performs a complete RR interval processing pipeline, including artifact detection, physiological plausibility checks, and moving average validation.

rr_full_phase_processing(
  rr_segment,
  min_rr = 272,
  max_rr = 2000,
  window_size = 7,
  threshold = 0.2,
  centered_window = FALSE
)

Arguments

rr_segment

A numeric vector of RR intervals (in milliseconds).

min_rr

The minimum acceptable RR interval (in milliseconds). Defaults to 272 ms (corresponding to a maximum heart rate of ~220 bpm).

max_rr

The maximum acceptable RR interval (in milliseconds). Defaults to 2000 ms (corresponding to a minimum heart rate of 30 bpm).

window_size

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

threshold

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

centered_window

A logical value indicating whether to use centered window for moving average validation. Defaults to FALSE.

Value

A list containing two elements:

is_valid

A logical vector of the same length as rr_segment, where TRUE indicates a valid RR interval and FALSE indicates an invalid interval after all processing steps.

cleaned_rr

A numeric vector containing only the valid RR intervals after all processing steps.

Details

This function sequentially applies the following validation steps:

  1. Artifact Detection: rr_validate_measure_artifacts is used to flag common measurement artifacts (e.g., values of 65.535 from some Garmin watches).

  2. Physiological Validation: rr_validate_physiological is used to flag intervals outside the specified min_rr and max_rr limits.

  3. Moving Average Validation: moving_average_rr_validation is used to flag intervals that deviate significantly from the local moving median, using the specified window_size and threshold. The function uses a 'running result' approach, where the is_valid vector from each step is passed as input to the next. This ensures that invalid intervals identified in earlier steps are not considered in subsequent validation steps.

Examples

# Sample RR interval data (in milliseconds)
rr_data <- c(
  65.535, 500, 250, 750, 800, 1500, 820, 780,
  850, 3000, 800, 830, 790
)

# Process the RR intervals
result <- rr_full_phase_processing(rr_data)

# Print the results
print(result$is_valid)
#>  [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
#> [13] FALSE
print(result$cleaned_rr)
#> [1] 500 800

# Example with custom parameters
rr_data2 <- c(
  65535, 600, 300, 700, 900,
  2200, 750
)
result2 <- rr_full_phase_processing(
  rr_data2,
  min_rr = 400,
  max_rr = 1800,
  window_size = 5,
  threshold = 0.15
)
print(result2)
#> $is_valid
#> [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
#> 
#> $cleaned_rr
#> [1] 600
#>