ras_detect: First-Pass Changepoint Detection via Sliding Window

View source: R/changepoint.R

ras_detectR Documentation

First-Pass Changepoint Detection via Sliding Window

Description

Scans a -\log_{10}(p)-value sequence using a sliding window to detect positions where the slope changes significantly from positive to negative.

Usage

ras_detect(
  x,
  y,
  p.values.threshold = 0.01,
  min.length = 10,
  skip = 1,
  window_size = 3000,
  slope_check_window_size = 30,
  slope.p.values.threshold = 1e-08,
  slope.p.values.threshold.left = 1e-10,
  slope.p.values.threshold.right = 1e-20
)

Arguments

x

Numeric vector. Predictor sequence (e.g., SNP position indices).

y

Numeric vector. Response sequence (e.g., -\log_{10}(p) values from ras_scan). Must be the same length as x.

p.values.threshold

Numeric. Davies test p-value threshold for nominating a candidate changepoint. Default 0.01.

min.length

Integer. Minimum number of observations required on each side of a candidate changepoint. Default 10.

skip

Integer. Step size when iterating the sliding window start position. Default 1.

window_size

Integer. Number of observations in each sliding window. Default 3000.

slope_check_window_size

Integer. Half-width of the local region used to verify slope direction via slope_test. Default 30.

slope.p.values.threshold

Numeric. Reserved combined slope threshold (currently unused in filtering). Default 1e-8.

slope.p.values.threshold.left

Numeric. One-tailed p-value threshold for the left-side slope test. A candidate is accepted only if the slope to its left is significantly positive (p < this value). Default 1e-10.

slope.p.values.threshold.right

Numeric. One-tailed p-value threshold for the right-side slope test. A candidate is accepted only if the slope to its right is significantly negative (p < this value). Default 1e-20.

Details

At each window start position the function calls get_break_points on the window to estimate and test a single breakpoint. A candidate is retained when three conditions are all met:

  1. The Davies test p-value is below p.values.threshold.

  2. The left-side slope (estimated by segmented regression) is positive.

  3. The right-side slope is negative.

Candidates passing these three filters are then subjected to one-tailed slope tests (slope_test) using centred sub-sequences of half-width slope_check_window_size. Only candidates that also pass both slope p-value thresholds are recorded.

After the sliding-window loop, each accepted changepoint is refined to the nearest local peak in y via get_local_maximum.

The function records all candidates examined (all.changepoints, all.p.values) in addition to the accepted ones, so that downstream functions can display the full candidate landscape.

Value

A named list with eight elements:

tau_hats

Integer vector. Accepted changepoint indices (refined to local peaks).

p.values

Numeric vector. Davies test p-values at accepted changepoints.

slope.left

Numeric vector. Left-side slopes at accepted changepoints.

slope.right

Numeric vector. Right-side slopes at accepted changepoints.

all.changepoints

Integer vector. All candidate positions examined, including those that failed the slope tests.

all.p.values

Numeric vector. Davies p-values for all examined candidates; set to 1 for candidates that failed the slope direction or slope significance filters.

slope.angle

Numeric vector. Interior angle (degrees) at each accepted changepoint, computed from the left and right slope estimates.

previous_tau_hats

Integer vector. Copy of tau_hats before any downstream modification; used by ras_validate.

See Also

ras_validate for the second-pass validation step. get_break_points for the per-window segmented regression. slope_test for the one-tailed slope verification. get_local_maximum for the peak-refinement step. ras for the recommended end-to-end entry point.

Examples


set.seed(42)
x <- 1:300
y <- c(seq(0, 8, length.out = 150),
       seq(8, 1, length.out = 150)) + rnorm(300, sd = 0.5)
result <- ras_detect(
  x, y,
  window_size             = 150,
  slope_check_window_size = 20,
  slope.p.values.threshold.left  = 1e-3,
  slope.p.values.threshold.right = 1e-3
)
cat("Detected changepoints:", result$tau_hats, "\n")


RAS documentation built on July 24, 2026, 5:06 p.m.