Skip to contents

A wrapper function that encapsulates various algorithms for detecting changepoint sets in univariate time series.

Usage

segment(x, method = "null", ...)

# S3 method for class 'tbl_ts'
segment(x, method = "null", ...)

# S3 method for class 'xts'
segment(x, method = "null", ...)

# S3 method for class 'numeric'
segment(x, method = "null", ...)

# S3 method for class 'ts'
segment(x, method = "null", ...)

Arguments

x

a numeric vector coercible into a stats::ts object

method

a character string indicating the algorithm to use. See Details.

...

arguments passed to methods

Value

An object of class tidycpt.

Details

Currently, segment() can use the following algorithms, depending on the value of the method argument:

  • pelt: Uses the PELT algorithm as implemented in segment_pelt(), which wraps either changepoint::cpt.mean() or changepoint::cpt.meanvar(). The segmenter is of class cpt.

  • binseg: Uses the Binary Segmentation algorithm as implemented by changepoint::cpt.meanvar(). The segmenter is of class cpt.

  • segneigh: Uses the Segmented Neighborhood algorithm as implemented by changepoint::cpt.meanvar(). The segmenter is of class cpt.

  • single-best: Uses the AMOC criteria as implemented by changepoint::cpt.meanvar(). The segmenter is of class cpt.

  • wbs: Uses the Wild Binary Segmentation algorithm as implemented by wbs::wbs(). The segmenter is of class wbs.

  • segmented: Uses the segmented algorithm as implemented by segmented::segmented(). The segmenter is of class segmented.

  • cga: Uses the Genetic algorithm implemented by segment_cga(), which wraps changepointGA::GA(). The segmenter is of class cga.

  • ga: Uses the Genetic algorithm implemented by segment_ga(), which wraps GA::ga(). The segmenter is of class tidyga.

  • ga-shi: Uses the genetic algorithm implemented by segment_ga_shi(), which wraps segment_ga(). The segmenter is of class tidyga.

  • ga-coen: Uses Coen's heuristic as implemented by segment_ga_coen(). The segmenter is of class tidyga. This implementation supersedes the following one.

  • coen: Uses Coen's heuristic as implemented by segment_coen(). The segmenter is of class seg_basket(). Note that this function is deprecated.

  • random: Uses a random basket of changepoints as implemented by segment_ga_random(). The segmenter is of class tidyga.

  • manual: Uses the vector of changepoints in the tau argument. The segmenter is of class seg_cpt`.

  • null: The default. Uses no changepoints. The segmenter is of class seg_cpt.

Examples

# Segment a time series using PELT
segment(DataCPSim, method = "pelt")
#>  A tidycpt object. Segmenter ↓
#> Class 'cpt' : Changepoint Object
#>        ~~   : S4 class containing 12 slots with names
#>               cpttype date version data.set method test.stat pen.type pen.value minseglen cpts ncpts.max param.est 
#> 
#> Created on  : Mon Jun 23 16:08:07 2025 
#> 
#> summary(.)  :
#> ----------
#> Created Using changepoint version 2.3 
#> Changepoint type      : Change in mean and variance 
#> Method of analysis    : PELT 
#> Test Statistic  : Normal 
#> Type of penalty       : MBIC with value, 27.99769 
#> Minimum Segment Length : 2 
#> Maximum no. of cpts   : Inf 
#> Changepoint Locations : 547 822 972 
#>  Model: A meanvar  model with 4 region(s).
#> → Each region has 2 parameter(s).
#> → The model has 0 global parameter(s).

# Segment a time series using PELT and the BIC penalty
segment(DataCPSim, method = "pelt", penalty = "BIC")
#>  A tidycpt object. Segmenter ↓
#> Class 'cpt' : Changepoint Object
#>        ~~   : S4 class containing 12 slots with names
#>               cpttype date version data.set method test.stat pen.type pen.value minseglen cpts ncpts.max param.est 
#> 
#> Created on  : Mon Jun 23 16:08:07 2025 
#> 
#> summary(.)  :
#> ----------
#> Created Using changepoint version 2.3 
#> Changepoint type      : Change in mean and variance 
#> Method of analysis    : PELT 
#> Test Statistic  : Normal 
#> Type of penalty       : BIC with value, 20.99827 
#> Minimum Segment Length : 2 
#> Maximum no. of cpts   : Inf 
#> Changepoint Locations : 547 822 972 
#>  Model: A meanvar  model with 4 region(s).
#> → Each region has 2 parameter(s).
#> → The model has 0 global parameter(s).

# Segment a time series using Binary Segmentation
segment(DataCPSim, method = "binseg", penalty = "BIC")
#>  A tidycpt object. Segmenter ↓
#> Class 'cpt' : Changepoint Object
#>        ~~   : S4 class containing 14 slots with names
#>               cpts.full pen.value.full data.set cpttype method test.stat pen.type pen.value minseglen cpts ncpts.max param.est date version 
#> 
#> Created on  : Mon Jun 23 16:08:07 2025 
#> 
#> summary(.)  :
#> ----------
#> Created Using changepoint version 2.3 
#> Changepoint type      : Change in mean and variance 
#> Method of analysis    : BinSeg 
#> Test Statistic  : Normal 
#> Type of penalty       : BIC with value, 20.99827 
#> Minimum Segment Length : 2 
#> Maximum no. of cpts   : 5 
#> Changepoint Locations : 547 809 972 
#> Range of segmentations:
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]  809   NA   NA   NA   NA
#> [2,]  809  547   NA   NA   NA
#> [3,]  809  547  972   NA   NA
#> [4,]  809  547  972  822   NA
#> [5,]  809  547  972  822  813
#> 
#>  For penalty values: 1485.679 462.0479 160.3649 15.04514 15.04514 
#>  Model: A meanvar  model with 4 region(s).
#> → Each region has 2 parameter(s).
#> → The model has 0 global parameter(s).

# Segment a time series using a random changepoint set
segment(DataCPSim, method = "random")
#> Seeding initial population with probability: 0.0063863343681642
#>  A tidycpt object. Segmenter ↓
#> An object of class "ga"
#> 
#> Call:
#> GA::ga(type = "binary", fitness = obj_fun, nBits = n, population = ..1,     maxiter = 1)
#> 
#> Available slots:
#>  [1] "data"          "model_fn_args" "call"          "type"         
#>  [5] "lower"         "upper"         "nBits"         "names"        
#>  [9] "popSize"       "iter"          "run"           "maxiter"      
#> [13] "suggestions"   "population"    "elitism"       "pcrossover"   
#> [17] "pmutation"     "optim"         "fitness"       "summary"      
#> [21] "bestSol"       "fitnessValue"  "solution"     
#>  Model: A meanshift_norm  model with 7 region(s).
#> → Each region has 1 parameter(s).
#> → The model has 1 global parameter(s).

# Segment a time series using a manually-specified changepoint set
segment(DataCPSim, method = "manual", tau = c(826))
#>  A tidycpt object. Segmenter ↓
#> List of 9
#>  $ data        : Time-Series [1:1096] from 1 to 1096: 35.5 29 35.6 33 29.5 ...
#>  $ pkg         : chr "tidychangepoint"
#>  $ base_class  : chr(0) 
#>  $ algorithm   : chr "manual"
#>  $ changepoints: num 826
#>  $ fitness     : Named num 10571
#>   ..- attr(*, "names")= chr "BIC"
#>  $ seg_params  : list()
#>  $ model_name  : chr "meanshift_norm"
#>  $ penalty     : chr "BIC"
#>  - attr(*, "class")= chr "seg_cpt"
#>  Model: A meanshift_norm  model with 2 region(s).
#> → Each region has 1 parameter(s).
#> → The model has 1 global parameter(s).

# Segment a time series using a null changepoint set
segment(DataCPSim)
#>  A tidycpt object. Segmenter ↓
#> List of 9
#>  $ data        : Time-Series [1:1096] from 1 to 1096: 35.5 29 35.6 33 29.5 ...
#>  $ pkg         : chr "tidychangepoint"
#>  $ base_class  : chr(0) 
#>  $ algorithm   : chr "manual"
#>  $ changepoints: NULL
#>  $ fitness     : Named num 11503
#>   ..- attr(*, "names")= chr "BIC"
#>  $ seg_params  : list()
#>  $ model_name  : chr "meanshift_norm"
#>  $ penalty     : chr "BIC"
#>  - attr(*, "class")= chr "seg_cpt"
#>  Model: A meanshift_norm  model with 1 region(s).
#> → Each region has 1 parameter(s).
#> → The model has 1 global parameter(s).