get_break_points: Detect a Single Changepoint via Segmented Regression

View source: R/changepoint.R

get_break_pointsR Documentation

Detect a Single Changepoint via Segmented Regression

Description

Fits a segmented linear model to detect one breakpoint in the relationship between x and y up to index t, and tests its significance using the Davies test.

Usage

get_break_points(x, y, t)

Arguments

x

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

y

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

t

Integer. Number of observations to use from the start of x and y. Must satisfy t >= 4 for segmented regression to be identifiable.

Details

The function first fits an ordinary linear model y ~ x over the first t observations, then calls segmented to estimate a single breakpoint. If the segmented fit succeeds, the Davies test (davies.test) is applied to the base linear model to assess whether the slope change is significant.

Both the segmented call and the Davies test are wrapped in try(..., silent = TRUE): if either fails (e.g., due to collinear data or a degenerate window), the function returns p.values = 1 and NULL for the breakpoint and slopes rather than propagating an error.

The breakpoint position is returned as an index into the full x vector (not just the first t elements), found by locating the element of x[1:t] closest to the estimated breakpoint coordinate.

Value

A named list with four elements:

break.points

Integer. Index of the estimated breakpoint in x[1:t], or NULL if no breakpoint was found.

p.values

Numeric. Davies test p-value for the breakpoint, or 1 if the fit failed or no breakpoint was found.

slope.left

Numeric. Estimated slope to the left of the breakpoint, or NULL if not found.

slope.right

Numeric. Estimated slope to the right of the breakpoint, or NULL if not found.

See Also

ras_detect which calls this function repeatedly in a sliding-window loop. slope_test for the one-tailed slope verification step. segmented, davies.test for the underlying segmented-regression routines.

Examples

set.seed(1)
x <- 1:60
y <- c(seq(0, 6, length.out = 30),
       seq(6, 2, length.out = 30)) + rnorm(60, sd = 0.4)
result <- get_break_points(x, y, t = 60)
cat("Breakpoint index:", result$break.points, "\n")
cat("Davies p-value:  ", result$p.values,    "\n")
cat("Left slope:      ", result$slope.left,   "\n")
cat("Right slope:     ", result$slope.right,  "\n")

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