gseg2: Graph-Based Detection of a Changed Interval

View source: R/canonical-interface.R

gseg2R Documentation

Graph-Based Detection of a Changed Interval

Description

Detects an interval whose distribution differs from the remainder of a sequence. Given observations or dissimilarities, the function constructs a k-MST and applies the max-type edge-count scan by default. A user-supplied similarity graph is also supported.

Usage

gseg2(x = NULL, dissimilarity = NULL, statistics = "m",
  l0 = NULL, l1 = NULL, pval.appr = TRUE, skew.corr = TRUE,
  pval.perm = FALSE, B = 100, k = NULL, n = NULL, E = NULL)

Arguments

x

A numeric matrix with observations in rows. For compatibility, the first positional argument may instead be the number of observations when the second positional argument is an edge matrix.

dissimilarity

A square symmetric dissimilarity matrix or a dist object. For compatibility, the second positional argument may instead be a two-column edge matrix. Supply exactly one of x and dissimilarity for a data-level call.

statistics

The scan statistic or statistics to compute. The default "m" reports only the max-type edge-count test (MET). Use "g" for GET, "o" for the original edge-count test, "w" for the weighted edge-count test, or "all" for all four. Long-form names are also accepted.

l0

Minimum candidate interval length. The default is 0.05 * N.

l1

Maximum candidate interval length. The default is 0.95 * N.

pval.appr

Whether to calculate the analytic p-value approximation. This approximation requires at least nine observations.

skew.corr

Whether to include the skewness correction in the analytic p-value approximation.

pval.perm

Whether to calculate a permutation p-value.

B

Number of permutations.

k

Number of MST layers for a data-level call. The default is floor(sqrt(N)); the largest allowed value is floor(N/2).

n

Number of observations in a graph-level call. Both named and positional legacy graph arguments are supported.

E

Two-column edge matrix in a graph-level call. Do not supply k together with a supplied graph.

Details

An interval with boundaries (i, j) contains observations i + 1, ..., j. When there are r tied maximizing intervals, tauhat (or tauhat_a/tauhat_u) retains the earlier layout: its first r entries are the left boundaries and its last r entries are the corresponding right boundaries. All returned intervals satisfy the requested length range after the documented endpoint adjustments. The analytic approximation requires a nonzero length range; use permutation calibration for a single permitted interval length.

Value

A list with scanZ and any requested p-values. Each requested component of scanZ contains the estimated interval tauhat, maximum scan statistic Zmax, and scan surface. Data-level calls additionally return the constructed graph and k.

Note

Analytic integrations are checked for convergence and finite probability outputs in [0,1]. An unavailable approximation raises an error rather than returning an invalid probability. Setting pval.appr = FALSE allows justified permutation calibration to be used instead. Scan components must be well-defined on the requested domain; undefined components are not replaced by zero.

See Also

gseg1, gseg2_graph, gseg2_discrete, gseg_kmst

Examples

set.seed(2)
x <- matrix(rnorm(80), nrow = 20)
x[8:13, ] <- x[8:13, ] + 1

# Main interface: build the default k-MST and report MET.
ans <- gseg2(x, pval.appr = FALSE)
names(ans$scanZ)
ans$k

# Earlier graph-level calls remain supported.
ans.graph <- gseg2(nrow(x), ans$graph, pval.appr = FALSE)

gSeg documentation built on Nov. 14, 2026, 9:08 a.m.