trendsegment: Detecting linear trend changes for univariate time series

View source: R/trendsegment.R

trendsegmentR Documentation

Detecting linear trend changes for univariate time series

Description

The main function of the package trendsegmentR. This function estimates the number and locations of change-points in linear trend of noisy data. The estimated change-points may contain point anomalies (segments including only one data point) if any. It also returns the estimated signal, the best linear fit for each segment between a pair of adjacent change-points. The algorithm includes three steps, Tail-Greedy Unbalanced Wavelet (TGUW) transform (TGUW), thresholding (thresholding) and inverse TGUW transform (invTGUW).

Usage

trendsegment(
  x,
  indep = FALSE,
  th.const = krt.hvt(x)$thr,
  p = 0.04,
  bal = 0,
  minsegL = floor(0.9 * log(length(x))),
  continuous = FALSE,
  connected = FALSE
)

Arguments

x

A data vector to be examined for change-point detection.

indep

If x is known to be independent over time, let indep=TRUE, otherwise the default is indep=FALSE.

th.const

Robust thresholding parameter used in thresholding. The default is obtained by considering sample kurtosis and long run standard deviation. The exact magnitude of the threshold also depends on sigma which is estimated by Median Absolute Deviation (MAD) method under the i.i.d. Gaussian noise assumption.

p

Proportion of all possible remaining merges which specifies the number of merges allowed in a single pass over the data. This is used in TGUW and the default is 0.04.

bal

The minimum ratio of the length of the shorter region to the length of the entire merging region especially when the merges of Type 2 (merging one initial and a paired smooth coefficient) or of Type 3 (merging two sets of (paired) smooth coefficients) are performed. The default is set to 0.

minsegL

The minimum segment length of estimated signal returned by trendsegment. The default is set to sigma * log(n) for the noise which is possibly dependent and/or non-Gaussian.

continuous

If continuous=TRUE, the estimated signal returned by trendsegment is continuous at change-points, otherwise discontinuous at change-points. The default is set to FALSE.

connected

If connected=TRUE, the trendsegment algorithm puts the connected rule above the minsegL, otherwise it makes keeping the minsegL a priority. The default is set to FALSE.

Details

The algorithm is described in H. Maeng and P. Fryzlewicz (2023), Detecting linear trend changes in data sequences.

Value

A list with the following.

x

The original input vector x.

est

The estimated piecewise-linear signal of x.

no.of.cpt

The estimated number of change-points.

cpt

The estimated locations of change-points.

Author(s)

Hyeyoung Maeng hyeyoung.maeng@durham.ac.uk, Piotr Fryzlewicz p.fryzlewicz@lse.ac.uk

See Also

TGUW, thresholding, invTGUW

Examples

x <- c(rep(0,100), seq(0, 4, length.out = 100), rep(3, 100), seq(3, -1, length.out=99))
n <- length(x)
x <- x + rnorm(n)
tsfit <- trendsegment(x = x)
tsfit

plot(x, type = "b", ylim = range(x, tsfit$est))
lines(tsfit$est, col=2, lwd=2)
abline(v=tsfit$cpt, col=3, lty=2, lwd=2)

trendsegmentR documentation built on Aug. 24, 2023, 5:07 p.m.