Learn R Programming

RegimeChange (version 0.1.1)

detect_regimes: Detect Regime Changes in Time Series

Description

Main function for detecting regime changes (changepoints) in time series data. Supports multiple detection methods, both frequentist and Bayesian, and can operate in offline (retrospective) or online (sequential) modes.

Usage

detect_regimes(
  data,
  method = c("pelt", "bocpd", "cusum", "binseg", "wbs", "shiryaev", "ensemble"),
  type = c("both", "mean", "variance", "trend", "distribution"),
  mode = c("offline", "online"),
  n_changepoints = "multiple",
  penalty = "BIC",
  min_segment = 2,
  prior = NULL,
  hazard = NULL,
  threshold = NULL,
  uncertainty = TRUE,
  bootstrap_reps = 200,
  ...
)

Value

An object of class "regime_result" containing:

  • changepoints: Vector of detected changepoint locations

  • n_changepoints: Number of changepoints detected

  • segments: List of segment information (start, end, parameters)

  • confidence_intervals: Confidence intervals for changepoint locations

  • existence_probability: Probability that each changepoint exists

  • posterior: Posterior distribution (Bayesian methods)

  • information_criterion: BIC/AIC values

  • method: Method used

  • call: The function call

Arguments

data

Numeric vector, time series (ts object), or matrix for multivariate data. For matrices, rows are observations and columns are variables.

method

Detection method. One of:

  • "pelt": Pruned Exact Linear Time (default for offline)

  • "bocpd": Bayesian Online Changepoint Detection

  • "cusum": Cumulative Sum

  • "binseg": Binary Segmentation

  • "wbs": Wild Binary Segmentation

  • "shiryaev": Shiryaev-Roberts procedure

  • "ensemble": Combination of multiple methods

type

Type of change to detect:

  • "mean": Changes in mean only

  • "variance": Changes in variance only

  • "both": Changes in mean and/or variance (default)

  • "trend": Changes in linear trend

  • "distribution": Non-parametric distributional changes

mode

Operation mode:

  • "offline": Retrospective analysis with full data (default)

  • "online": Sequential analysis for monitoring

n_changepoints

Expected number of changepoints:

  • "single": Detect at most one changepoint

  • "multiple": Detect multiple changepoints (default)

  • An integer: Detect exactly this many changepoints

penalty

Penalty for model complexity (offline methods):

  • "BIC": Bayesian Information Criterion (default)

  • "AIC": Akaike Information Criterion

  • "MBIC": Modified BIC

  • "MDL": Minimum Description Length

  • A number: Manual penalty value

min_segment

Minimum segment length (number of observations)

prior

Prior specification for Bayesian methods (from prior functions)

hazard

Hazard prior for changepoint occurrence (Bayesian methods)

threshold

Detection threshold (for online/CUSUM methods)

uncertainty

Logical; if TRUE, compute confidence intervals

bootstrap_reps

Number of bootstrap replicates for uncertainty (if uncertainty = TRUE)

...

Additional arguments passed to specific methods

Examples

Run this code

set.seed(123)
data <- c(rnorm(100, mean = 0), rnorm(100, mean = 3))


result <- detect_regimes(data)
print(result)
plot(result)


result <- detect_regimes(data, method = "bocpd",
                         prior = normal_gamma())


result <- detect_regimes(data, method = "cusum",
                         mode = "online", threshold = 5)

Run the code above in your browser using DataLab