| get_break_points | R Documentation |
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.
get_break_points(x, y, t)
x |
Numeric vector. Predictor (e.g., SNP position indices). |
y |
Numeric vector. Response (e.g., |
t |
Integer. Number of observations to use from the start of |
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.
A named list with four elements:
break.pointsInteger. Index of the estimated breakpoint in
x[1:t], or NULL if no breakpoint was found.
p.valuesNumeric. Davies test p-value for the breakpoint,
or 1 if the fit failed or no breakpoint was found.
slope.leftNumeric. Estimated slope to the left of the
breakpoint, or NULL if not found.
slope.rightNumeric. Estimated slope to the right of the
breakpoint, or NULL if not found.
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.
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.