Title: | Combines Changepoint Analysis with 'ggplot2' |
---|---|
Description: | R provides fantastic tools for changepoint analysis, but plots generated by the tools do not have the 'ggplot2' style. This tool, however, combines 'changepoint', 'changepoint.np' and 'ecp' together, and uses 'ggplot2' to visualize changepoints. |
Authors: | Youzhi Yu [aut, cre] |
Maintainer: | Youzhi Yu <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.0 |
Built: | 2025-03-11 03:46:20 UTC |
Source: | https://github.com/pursuitofdatascience/ggchangepoint |
This function wraps a number of cpt
functions from the changepoint
package and the cpt.np()
function from the changepoint.np package.
It is handy that users can use this function to get the same changepoint
results as these functions output individually. Moreover, it returns a tibble
that inherits the tidyverse sytle. Functions from the changepoint package do
require data normality assumption by default, yet changepoint.np is a
non-parametric way to detect changepoints and let data speak by itself.
If user sets change_in
as cpt_np
, a seed should be set before
using the function for the sake of reproducibility. For more details on the
changepoint and changepoint.np packages, please refer to their documentation.
cpt_wrapper(data, change_in = "mean_var", cp_method = "PELT", ...)
cpt_wrapper(data, change_in = "mean_var", cp_method = "PELT", ...)
data |
A vector. |
change_in |
Choice of |
cp_method |
A wide range of choices (i.e., |
... |
Extra arguments for each |
A tibble includes which point(s) is/are the changepoint along with raw changepoint value corresponding to that changepoint.
Killick R, Eckley I (2014). “changepoint: An R package for changepoint analysis.” Journal of statistical software, 58(3), 1–19.
set.seed(2022) cpt_wrapper(c(rnorm(100,0,1),rnorm(100,0,10))) cpt_wrapper(c(rnorm(100,0,1),rnorm(100,10,1)))
set.seed(2022) cpt_wrapper(c(rnorm(100,0,1),rnorm(100,0,10))) cpt_wrapper(c(rnorm(100,0,1),rnorm(100,10,1)))
The ecp package provides a non-parametric way to detect changepoints. Unlike
the changepoint package, it does not assume raw data to have any formal
distribution. This wrapper function wraps two functions from the ecp package,
i.e., e.divisive()
and e.agglo()
. Users can use either function
by switching the algorithm
argument. Before using the wrapper function
, seed should be set for the sake of reproducibility.
ecp_wrapper(data, algorithm = "divisive", min_size = 2, ...)
ecp_wrapper(data, algorithm = "divisive", min_size = 2, ...)
data |
A vector. |
algorithm |
Either |
min_size |
Minimum number of observations between change points. By
default is 2. This argument is only applied when |
... |
Extra arguments to pass on either from |
A tibble includes which point(s) is/are the changepoint along with raw changepoint value corresponding to that changepoint.
James NA, Matteson DS (2013). “ecp: An R package for nonparametric multiple change point analysis of multivariate data.” arXiv preprint arXiv:1309.3295.
set.seed(2022) ecp_wrapper(c(rnorm(100,0,1),rnorm(100,0,10))) ecp_wrapper(c(rnorm(100,0,1),rnorm(100,10,1)))
set.seed(2022) ecp_wrapper(c(rnorm(100,0,1),rnorm(100,0,10))) ecp_wrapper(c(rnorm(100,0,1),rnorm(100,10,1)))
ggchangepoint
packageCombines Changepoint Anaysis with 'ggplot2'.
ggchangepoint tries to offer several changepoint R packages in a tidy format and output the ggplot2 plots so that the tidyverse users can gain some familiarity to work with the changepoint analysis. For the moment, I only include three changepoint packages ('changepoint', 'changepoint.np' and 'ecp' ). More changepoint packages will be included as time progresses.
The plot for changepoints detected by the changepoint package is a line plot for the raw data and the vertical lines representing each changepoint. The x-axis is the row number of the raw data in the original data vector. The plot inherits ggplot2, meaning users can add ggplot2 functions on top the changepoint plot for customization.
ggcptplot( data, change_in = "mean_var", cp_method = "PELT", ..., cptline_alpha = 1, cptline_color = "blue", cptline_type = "solid", cptline_size = 0.5 )
ggcptplot( data, change_in = "mean_var", cp_method = "PELT", ..., cptline_alpha = 1, cptline_color = "blue", cptline_type = "solid", cptline_size = 0.5 )
data |
A vector. |
change_in |
Choice of |
cp_method |
A wide range of choices (i.e., |
... |
Extra arguments for each |
cptline_alpha |
The value of alpha for the vertical changepoint line(s), default is 1, meaning no transparency. |
cptline_color |
The color for the vertical changepoint line(s), default
is |
cptline_type |
The linetype for the vertical changepoint line(s),
default is |
cptline_size |
The size for the vertical changepoint line(s), default is
|
A line plot with data points along with the vertical lines representing changepoints.
ggcptplot(c(rnorm(100,0,1),rnorm(100,0,10))) ggcptplot(c(rnorm(100,0,1),rnorm(100,10,1)))
ggcptplot(c(rnorm(100,0,1),rnorm(100,0,10))) ggcptplot(c(rnorm(100,0,1),rnorm(100,10,1)))
The plot for changepoints detected by the ecp package is a line plot for the raw data and the vertical lines representing each changepoint. The x-axis is the row number of the raw data in the original data vector. The plot inherits ggplot2, meaning users can add ggplot2 functions on top the changepoint plot for customization.
ggecpplot( data, algorithm = "divisive", min_size = 2, ..., cptline_alpha = 1, cptline_color = "blue", cptline_type = "solid", cptline_size = 0.5 )
ggecpplot( data, algorithm = "divisive", min_size = 2, ..., cptline_alpha = 1, cptline_color = "blue", cptline_type = "solid", cptline_size = 0.5 )
data |
A vector. |
algorithm |
Either |
min_size |
Minimum number of observations between change points. By
default is 2. This argument is only applied when |
... |
Extra arguments to pass on either from |
cptline_alpha |
The value of alpha for the vertical changepoint line(s), default is 1, meaning no transparency. |
cptline_color |
The color for the vertical changepoint line(s), default
is |
cptline_type |
The linetype for the vertical changepoint line(s),
default is |
cptline_size |
The size for the vertical changepoint line(s), default is
|
A line plot with data points along with the vertical lines representing changepoints.
ggecpplot(c(rnorm(100,0,1),rnorm(100,0,10))) ggecpplot(c(rnorm(100,0,1),rnorm(100,0,10)))
ggecpplot(c(rnorm(100,0,1),rnorm(100,0,10))) ggecpplot(c(rnorm(100,0,1),rnorm(100,0,10)))