tidychangepoint

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Tidy methods for changepoint analysis

library(tidychangepoint)

The tidychangepoint package allows you to use any number of algorithms for detecting changepoint sets in univariate time series with a common, tidyverse-compliant interface. It also provides model-fitting procedures for commonly-used parametric models, tools for computing various penalty functions, and graphical diagnostic displays.

Changepoint sets are computed using the segment() function, which takes a numeric vector that is coercible into a ts object, and a string indicating the algorithm you wish you use. segment() always returns a tidycpt object.

x <- segment(DataCPSim, method = "pelt")
class(x)

Various methods are available for tidycpt objects. For example, as.ts() returns the original data as ts object, and changepoints() returns the set of changepoint indices.

changepoints(x)

Retrieving information using the broom interface

tidychangepoint follows the design interface of the broom package. Therefore, augment(), tidy() and glance() methods exists for tidycpt objects.

augment(x)
tidy(x)
glance(x)

Other methods

The plot() method leverages ggplot2 to provide an informative plot, with the regions defined by the changepoint set clearly demarcated, and the means within each region also indicated.

plot(x)

Other generic functions defined for tidycpt objects include fitness(), as.model(), and exceedances(). For example, fitness() returns a named vector with the value of the penalized objective function used.

fitness(x)

Structure

Every tidycpt objects contains two main children:

Both segmenters and models implement methods for the generic functions changepoints(), as.ts(), nobs(), logLik(), model_name(), and glance(). However, it is important to note that while tidychangepoint does its best to match the model used by the segmenter to its corresponding model-fitting function, exact matches do not always exist. Thus, the logLik() of the segmenter may not always match the logLik() of the model. Nevertheless, squaring these values is the focus of ongoing work.

Segmenters

In the example above, the segmenter is of class cpt, because segment() simply wraps the cpt.meanvar() function from the changepoint package.

x |>
  as.segmenter() |>
  str()

In addition to the generic functions listed above, segmenters implement methods for the generic functions fitness(), model_args(), and seg_params().

Models

The model object in this case is created by fit_meanvar(), and is of class mod_cpt.

x |>
  as.model() |>
  str()

In addition to the generic functions listed above, models implement methods for the generic functions fitted(), residuals(), coef(), augment(), tidy(), and plot().



Try the tidychangepoint package in your browser

Any scripts or data that you put into this service are public.

tidychangepoint documentation built on April 4, 2025, 4:31 a.m.