Performs a series of offline and/or online change-point detection algorithms for
Users must have a (C++) compiler installed on their machine that is compatible with R (e.g. Clang). The development version of changepoints
from GitHub can be installed with:
## if not installed
## Install dependencies
install.packages(c("devtools","glmnet","gglasso","ks","data.tree"))
## install.packages("devtools")
devtools::install_github("HaotianXu/changepoints")
This is an example for offline univariate mean change point detection by l0 penalization:
library(changepoints)
## simulate data with true change points being 50, 100 and 150
set.seed(0)
y = c(rep(0, 50), rep(2, 50), rep(0, 50), rep(-2, 50)) + rnorm(200, mean = 0, sd = 1)
## estimate change points by l_0 penalization
gamma_set = c(0.01, 0.5, 1, 5, 10, 50) # possible value of tuning parameter
## perform cross-validation
DP_result = CV.search.DP.univar(y, gamma_set, delta = 5)
## estimate change points and perform local refinement
min_idx = which.min(DP_result$test_error)
cpt_DP_hat = unlist(DP_result$cpt_hat[[min_idx]])
cpt_DP_LR = local.refine.univar(cpt_DP_hat, y)
Alternatively, wild binary segmentation
can also be performed:
## generate random intervals for WBS
intervals = WBS.intervals(M = 100, lower = 1, upper = 200)
## perform WBS
WBS_result = WBS.univar(y, 1, 200, intervals$Alpha, intervals$Beta, delta = 5)
WBS_result
## trim binary tree with threshold being 3
WBS_trimmed = thresholdBS(WBS_result, tau = 3)
## print the trimmed binary tree
print(WBS_trimmed$BS_tree_trimmed, "value")
## estimate change points and perform local refinement
cpt_WBS_hat = sort(WBS_trimmed$cpt_hat[,1])
cpt_BS_LR = local.refine.univar(cpt_WBS_hat, y)
wild binary segmentation
with tuning parameter selected by information criteria :