RegimeChange is a comprehensive R package for detecting regime changes (changepoints) in time series data. It provides a unified interface to both frequentist and Bayesian methods, supports both offline and online detection modes, and offers rigorous uncertainty quantification.
library(RegimeChange)
# Generate example data with a changepoint at t=100
set.seed(123)
data <- c(rnorm(100, mean = 0, sd = 1), rnorm(100, mean = 3, sd = 1))
# Detect changepoints using PELT (default)
result <- detect_regimes(data)
# Print results
print(result)
#>
#> Regime Change Detection Results
#> ================================
#>
#> Method: pelt
#> Change type: both
#> Mode: offline
#>
#> Data: n = 200 observations
#>
#> Changepoints detected: 1
#> Locations: 100
#>
#> 95% Confidence Intervals:
#> CP 1: 100 [80, 120]
#>
#> Segments:
#> Segment 1: [1, 100] (n=100) | mean=0.090, sd=0.913
#> Segment 2: [101, 200] (n=100) | mean=2.892, sd=0.967RegimeChange supports multiple detection algorithms:
# PELT - Pruned Exact Linear Time (default)
result_pelt <- detect_regimes(data, method = "pelt")
# Binary Segmentation
result_binseg <- detect_regimes(data, method = "binseg")
# Wild Binary Segmentation
result_wbs <- detect_regimes(data, method = "wbs")
# CUSUM
result_cusum <- detect_regimes(data, method = "cusum")Detect different types of statistical changes:
# Normal-Gamma prior for unknown mean and variance
prior <- normal_gamma(mu0 = 0, kappa0 = 1, alpha0 = 1, beta0 = 1)
result <- detect_regimes(data, method = "bocpd", prior = prior)
# Geometric hazard prior
hazard <- geometric_hazard(lambda = 0.01)
result <- detect_regimes(data, method = "bocpd", hazard = hazard)Evaluate detection performance against known changepoints:
# True changepoint is at position 100
evaluation <- evaluate(result, true_changepoints = 100, tolerance = 5)
print(evaluation)
#>
#> Changepoint Detection Evaluation
#> =================================
#>
#> Detection Performance:
#> True changepoints: 1
#> Detected: 1
#> Matched: 1
#> Precision: 1.000
#> Recall: 1.000
#> F1 Score: 1.000
#>
#> Localization Accuracy:
#> Hausdorff Distance: 0.00
#> Mean Absolute Error: 0.00
#> RMSE: 0.00
#>
#> Segmentation Quality:
#> Rand Index: 1.000
#> Adjusted Rand Index: 1.000
#> Covering Metric: 1.000Compare multiple methods:
For real-time monitoring:
# Create online detector
detector <- regime_detector(method = "bocpd", prior = normal_gamma())
# Process observations one at a time
for (x in data) {
result <- update(detector, x)
detector <- result$detector
if (result$alarm) {
cat("Changepoint detected at observation", length(detector$history), "\n")
}
}Get confidence intervals for changepoint locations:
vignette("offline-detection") for detailed offline
analysisvignette("online-detection") for real-time
monitoringvignette("bayesian-methods") for Bayesian
approachesvignette("multivariate") for multivariate time
series