Title: | Change-Point Detection by Sample-Splitting Methods |
---|---|
Description: | Implements multiple change searching algorithms for a variety of frequently considered parametric change-point models. In particular, it integrates a criterion proposed by Zou, Wang and Li (2020) <doi:10.1214/19-AOS1814> to select the number of change-points in a data-driven fashion. Moreover, it also provides interfaces for user-customized change-point models with one's own cost function and parameter estimation routine. It is easy to get started with the cpss.* set of functions by accessing their documentation pages (e.g., ?cpss). |
Authors: | Guanghui Wang [aut, cre], Changliang Zou [aut] |
Maintainer: | Guanghui Wang <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.0.3 |
Built: | 2024-11-16 06:45:49 UTC |
Source: | CRAN |
Generic functions and methods: algo
algo(x) algo(x) <- value ## S4 method for signature 'cpss' algo(x) ## S4 replacement method for signature 'cpss' algo(x) <- value
algo(x) algo(x) <- value ## S4 method for signature 'cpss' algo(x) ## S4 replacement method for signature 'cpss' algo(x) <- value
x |
object from cpss |
value |
value assigned to x |
Generic functions and methods: algo_param_dim
algo_param_dim(x) algo_param_dim(x) <- value ## S4 method for signature 'cpss' algo_param_dim(x) ## S4 replacement method for signature 'cpss' algo_param_dim(x) <- value
algo_param_dim(x) algo_param_dim(x) <- value ## S4 method for signature 'cpss' algo_param_dim(x) ## S4 replacement method for signature 'cpss' algo_param_dim(x) <- value
x |
object from cpss |
value |
value assigned to x |
coef method
## S4 method for signature 'cpss' coef(object)
## S4 method for signature 'cpss' coef(object)
object |
object from cpss |
cpss |
cpss class |
Generic functions and methods: cps
cps(x) cps(x) <- value ## S4 method for signature 'cpss' cps(x) ## S4 replacement method for signature 'cpss' cps(x) <- value
cps(x) cps(x) <- value ## S4 method for signature 'cpss' cps(x) ## S4 replacement method for signature 'cpss' cps(x) <- value
x |
object from cpss |
value |
value assigned to x |
Implements multiple change searching algorithms for a variety of frequently considered parametric change-point models. In particular, it integrates a criterion proposed by Zou, Wang and Li (2020) doi:10.1214/19-AOS1814 to select the number of change-points in a data-driven fashion. Moreover, it also provides interfaces for user-customized change-point models with one's own cost function and parameter estimation routine.
Easy to get started with the cpss.* set of functions by accessing their documentation pages
library(cpss)
?cpss.mean
?cpss.var
?cpss.meanvar
?cpss.glm
?cpss.lm
?cpss.em
?cpss.custom
cpss: an S4 class which collects data and information required for further change-point analyses and summaries
dat
ANY.
mdl
character.
algo
character.
algo_param_dim
numeric.
SC
character.
ncps
integer.
pelt_pen
numeric.
cps
numeric.
params
list.
S_vals
numeric.
SC_vals
matrix.
call
list.
update_inputs
list.
Detecting changes in uers-customized models
cpss.custom( dataset, n, g_subdat, g_param, g_cost, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2, model = NULL, g_smry = NULL, easy_cost = NULL, param.opt = NULL )
cpss.custom( dataset, n, g_subdat, g_param, g_cost, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2, model = NULL, g_smry = NULL, easy_cost = NULL, param.opt = NULL )
dataset |
an |
n |
an integer indicating the sample size of the data |
g_subdat |
a customized R function of two arguments |
g_param |
a customized R function of two arguments |
g_cost |
a customized R function of two arguments |
algorithm |
a character string specifying the change-point searching algorithm, one of the following choices: "SN" (segment neighborhood), "BS" (binary segmentation), "WBS" (wild binary segmentation) and "PELT" (pruned exact linear time) algorithms. |
dist_min |
an integer specifying minimum searching distance (length of feasible segments). |
ncps_max |
an integer specifying an upper bound of the number of true change-points. |
pelt_pen_val |
a numeric vector specifying candidate values of the penalty only if |
pelt_K |
a numeric value for pruning adjustment only if |
wbs_nintervals |
an integer specifying the number of random intervals drawn only if |
criterion |
a character string specifying the model selection criterion, "CV" ("cross-validation") or "MS" ("multiple-splitting"). |
times |
an integer specifying how many times of sample-splitting should be performed; It should be 2 if |
model |
a character string indicating the considered change model. |
g_smry |
a customized R function of two arguments |
easy_cost |
a customized R function of three arguments |
param.opt |
an |
cpss.custom
returns an object of an S4 class, called "cpss
", which collects data and information required for further change-point analyses and summaries.
dat
data set
mdl
considered change-point model
algo
change-point searching algorithm
algo_param_dim
user-specified upper bound of the number of true change-points if algorithm = "SN"/"BS"/"WBS"
, or user-specified candidate values of the penalty only if algorithm = "PELT"
SC
model selection criterion
ncps
estimated number of change-points
pelt_pen
selected value of the penalty only if algorithm = "PELT"
cps
a vector of estimated locations of change-points
params
a list object, each member is a list containing estimated parameters in the associated data segment
S_vals
a numeric vector of candidate model dimensions in terms of a sequence of numbers of change-points or values of the penalty
SC_vals
a numeric matrix, each column records the values of the criterion based on the validation data split under the corresponding model dimension (S_vals
), and each row represents a splitting at each time
Killick, R., Fearnhead, P., and Eckley, I. A. (2012). Optimal Detection of Changepoints With a Linear Computational Cost. Journal of the American Statistical Association, 107(500): 1590–1598.
Fryzlewicz, P. (2014). Wild binary segmentation for multiple change-point detection. The Annals of Statistics, 42(6): 2243–2281.
library("cpss") g_subdat_l1 <- function(dat, indices) { dat[indices] } g_param_l1 <- function(dat, param.opt = NULL) { return(median(dat)) } g_cost_l1 <- function(dat, param) { return(sum(abs(dat - param))) } res <- cpss.custom( dataset = well, n = length(well), g_subdat = g_subdat_l1, g_param = g_param_l1, g_cost = g_cost_l1, ncps_max = 11 ) summary(res) plot(well) abline(v = res@cps, col = "red")
library("cpss") g_subdat_l1 <- function(dat, indices) { dat[indices] } g_param_l1 <- function(dat, param.opt = NULL) { return(median(dat)) } g_cost_l1 <- function(dat, param) { return(sum(abs(dat - param))) } res <- cpss.custom( dataset = well, n = length(well), g_subdat = g_subdat_l1, g_param = g_param_l1, g_cost = g_cost_l1, ncps_max = 11 ) summary(res) plot(well) abline(v = res@cps, col = "red")
Detecting changes in exponential family
cpss.em( dataset, family, size = NULL, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
cpss.em( dataset, family, size = NULL, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
dataset |
a numeric matrix of dimension |
family |
a character string specifying the underlying distribution. In the current version, detecting changes in binomial (" |
size |
an integer indicating the number of trials only if |
algorithm |
a character string specifying the change-point searching algorithm, one of the following choices: "SN" (segment neighborhood), "BS" (binary segmentation), "WBS" (wild binary segmentation) and "PELT" (pruned exact linear time) algorithms. |
dist_min |
an integer specifying minimum searching distance (length of feasible segments). |
ncps_max |
an integer specifying an upper bound of the number of true change-points. |
pelt_pen_val |
a numeric vector specifying candidate values of the penalty only if |
pelt_K |
a numeric value for pruning adjustment only if |
wbs_nintervals |
an integer specifying the number of random intervals drawn only if |
criterion |
a character string specifying the model selection criterion, "CV" ("cross-validation") or "MS" ("multiple-splitting"). |
times |
an integer specifying how many times of sample-splitting should be performed; It should be 2 if |
cpss.em
returns an object of an S4 class, called "cpss
", which collects data and information required for further change-point analyses and summaries. See cpss.custom
.
Killick, R., Fearnhead, P., and Eckley, I. A. (2012). Optimal Detection of Changepoints With a Linear Computational Cost. Journal of the American Statistical Association, 107(500):1590–1598.
Fryzlewicz, P. (2014). Wild binary segmentation for multiple change-point detection. The Annals of Statistics, 42(6): 2243–2281.
cpss.meanvar
cpss.mean
cpss.var
library("cpss") set.seed(666) n <- 1000 tau <- c(100, 300, 700, 900) tau_ext <- c(0, tau, n) theta <- c(1, 0.2, 1, 0.2, 1) seg_len <- diff(c(0, tau, n)) y <- unlist(lapply(seq(1, length(tau) + 1), function(k) { rexp(seg_len[k], theta[k]) })) res <- cpss.em( y, family = "exp", algorithm = "WBS", ncps_max = 10, criterion = "MS", times = 10 ) cps(res) # [1] 100 299 705 901
library("cpss") set.seed(666) n <- 1000 tau <- c(100, 300, 700, 900) tau_ext <- c(0, tau, n) theta <- c(1, 0.2, 1, 0.2, 1) seg_len <- diff(c(0, tau, n)) y <- unlist(lapply(seq(1, length(tau) + 1), function(k) { rexp(seg_len[k], theta[k]) })) res <- cpss.em( y, family = "exp", algorithm = "WBS", ncps_max = 10, criterion = "MS", times = 10 ) cps(res) # [1] 100 299 705 901
Detecting changes in GLMs
cpss.glm( formula, family, data = NULL, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
cpss.glm( formula, family, data = NULL, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
formula |
a |
family |
a description of the error distribution and link function to be used in the model, which can be a character string naming a family function or a family function. |
data |
an optional data frame containing the variables in the model. |
algorithm |
a character string specifying the change-point searching algorithm, one of the following choices: "SN" (segment neighborhood), "BS" (binary segmentation), "WBS" (wild binary segmentation) and "PELT" (pruned exact linear time) algorithms. |
dist_min |
an integer specifying minimum searching distance (length of feasible segments). |
ncps_max |
an integer specifying an upper bound of the number of true change-points. |
pelt_pen_val |
a numeric vector specifying candidate values of the penalty only if |
pelt_K |
a numeric value for pruning adjustment only if |
wbs_nintervals |
an integer specifying the number of random intervals drawn only if |
criterion |
a character string specifying the model selection criterion, "CV" ("cross-validation") or "MS" ("multiple-splitting"). |
times |
an integer specifying how many times of sample-splitting should be performed; It should be 2 if |
cpss.glm
returns an object of an S4 class, called "cpss
", which collects data and information required for further change-point analyses and summaries. See cpss.custom
.
Killick, R., Fearnhead, P., and Eckley, I. A. (2012). Optimal Detection of Changepoints With a Linear Computational Cost. Journal of the American Statistical Association, 107(500):1590–1598.
Fryzlewicz, P. (2014). Wild binary segmentation for multiple change-point detection. The Annals of Statistics, 42(6): 2243–2281.
library("cpss") set.seed(666) n <- 200 size <- rpois(n, 20 - 1) + 1 tau <- c(75, 100, 175) tau_ext <- c(0, tau, n) be <- list(c(0, 0.5), c(0, -0.5), c(0.5, -0.5), c(-0.5, -0.5)) seg_len <- diff(c(0, tau, n)) x <- rnorm(n) eta <- lapply(seq(1, length(tau) + 1), function(k) { be[[k]][1] + be[[k]][2] * x[(tau_ext[k] + 1):tau_ext[k + 1]] }) eta <- do.call(c, eta) p <- 1 / (1 + exp(-eta)) y <- rbinom(n, size = size, prob = p) pelt_pen_val <- (log(n))^seq(0.5, 2, by = 0.1) res <- cpss.glm( formula = cbind(y, size - y) ~ x, family = binomial(), algorithm = "PELT", pelt_pen_val = pelt_pen_val, ncps_max = 10 ) summary(res) # 75 105 175 coef(res) # [1,] 0.02540872 0.08389551 0.5284425 -0.4980768 # [2,] 0.57222684 -0.45430385 -0.5203319 -0.4581678
library("cpss") set.seed(666) n <- 200 size <- rpois(n, 20 - 1) + 1 tau <- c(75, 100, 175) tau_ext <- c(0, tau, n) be <- list(c(0, 0.5), c(0, -0.5), c(0.5, -0.5), c(-0.5, -0.5)) seg_len <- diff(c(0, tau, n)) x <- rnorm(n) eta <- lapply(seq(1, length(tau) + 1), function(k) { be[[k]][1] + be[[k]][2] * x[(tau_ext[k] + 1):tau_ext[k + 1]] }) eta <- do.call(c, eta) p <- 1 / (1 + exp(-eta)) y <- rbinom(n, size = size, prob = p) pelt_pen_val <- (log(n))^seq(0.5, 2, by = 0.1) res <- cpss.glm( formula = cbind(y, size - y) ~ x, family = binomial(), algorithm = "PELT", pelt_pen_val = pelt_pen_val, ncps_max = 10 ) summary(res) # 75 105 175 coef(res) # [1,] 0.02540872 0.08389551 0.5284425 -0.4980768 # [2,] 0.57222684 -0.45430385 -0.5203319 -0.4581678
Detecting changes in linear models
cpss.lm( formula, data = NULL, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
cpss.lm( formula, data = NULL, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
formula |
a |
data |
an optional data frame containing the variables in the model. |
algorithm |
a character string specifying the change-point searching algorithm, one of the following choices: "SN" (segment neighborhood), "BS" (binary segmentation), "WBS" (wild binary segmentation) and "PELT" (pruned exact linear time) algorithms. |
dist_min |
an integer specifying minimum searching distance (length of feasible segments). |
ncps_max |
an integer specifying an upper bound of the number of true change-points. |
pelt_pen_val |
a numeric vector specifying candidate values of the penalty only if |
pelt_K |
a numeric value for pruning adjustment only if |
wbs_nintervals |
an integer specifying the number of random intervals drawn only if |
criterion |
a character string specifying the model selection criterion, "CV" ("cross-validation") or "MS" ("multiple-splitting"). |
times |
an integer specifying how many times of sample-splitting should be performed; It should be 2 if |
cpss.lm
returns an object of an S4 class, called "cpss
", which collects data and information required for further change-point analyses and summaries. See cpss.custom
.
Killick, R., Fearnhead, P., and Eckley, I. A. (2012). Optimal Detection of Changepoints With a Linear Computational Cost. Journal of the American Statistical Association, 107(500):1590–1598.
Fryzlewicz, P. (2014). Wild binary segmentation for multiple change-point detection. The Annals of Statistics, 42(6): 2243–2281.
library("cpss") set.seed(666) n <- 400 tau <- c(80, 200, 300) tau_ext <- c(0, tau, n) be <- list(c(0, 1), c(1, 0.5), c(0, 1), c(-1, 0.5)) seg_len <- diff(c(0, tau, n)) x <- rnorm(n) mu <- lapply(seq(1, length(tau) + 1), function(k) { be[[k]][1] + be[[k]][2] * x[(tau_ext[k] + 1):tau_ext[k + 1]] }) mu <- do.call(c, mu) sig <- unlist(lapply(seq(1, length(tau) + 1), function(k) { rep(be[[k]][2], seg_len[k]) })) y <- rnorm(n, mu, sig) res <- cpss.lm( formula = y ~ x, algorithm = "BS", ncps_max = 10 ) summary(res) # 80 202 291 coef(res) # $coef # [,1] [,2] [,3] [,4] # [1,] -0.00188792 1.0457718 -0.03963209 -0.9444813 # [2,] 0.91061557 0.6291965 1.20694409 0.4410036 # # $sigma # [1] 0.8732233 0.4753216 0.9566516 0.4782329
library("cpss") set.seed(666) n <- 400 tau <- c(80, 200, 300) tau_ext <- c(0, tau, n) be <- list(c(0, 1), c(1, 0.5), c(0, 1), c(-1, 0.5)) seg_len <- diff(c(0, tau, n)) x <- rnorm(n) mu <- lapply(seq(1, length(tau) + 1), function(k) { be[[k]][1] + be[[k]][2] * x[(tau_ext[k] + 1):tau_ext[k + 1]] }) mu <- do.call(c, mu) sig <- unlist(lapply(seq(1, length(tau) + 1), function(k) { rep(be[[k]][2], seg_len[k]) })) y <- rnorm(n, mu, sig) res <- cpss.lm( formula = y ~ x, algorithm = "BS", ncps_max = 10 ) summary(res) # 80 202 291 coef(res) # $coef # [,1] [,2] [,3] [,4] # [1,] -0.00188792 1.0457718 -0.03963209 -0.9444813 # [2,] 0.91061557 0.6291965 1.20694409 0.4410036 # # $sigma # [1] 0.8732233 0.4753216 0.9566516 0.4782329
Detecting changes in mean
cpss.mean( dataset, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2, Sigma = NULL )
cpss.mean( dataset, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2, Sigma = NULL )
dataset |
a numeric matrix of dimension |
algorithm |
a character string specifying the change-point searching algorithm, one of the following choices: "SN" (segment neighborhood), "BS" (binary segmentation), "WBS" (wild binary segmentation) and "PELT" (pruned exact linear time) algorithms. |
dist_min |
an integer specifying minimum searching distance (length of feasible segments). |
ncps_max |
an integer specifying an upper bound of the number of true change-points. |
pelt_pen_val |
a numeric vector specifying candidate values of the penalty only if |
pelt_K |
a numeric value for pruning adjustment only if |
wbs_nintervals |
an integer specifying the number of random intervals drawn only if |
criterion |
a character string specifying the model selection criterion, "CV" ("cross-validation") or "MS" ("multiple-splitting"). |
times |
an integer specifying how many times of sample-splitting should be performed; It should be 2 if |
Sigma |
if a numeric matrix (or constant) is supplied, it will be taken as the value of the common covariance (or variance). By default it is
|
cpss.mean
returns an object of an S4 class, called "cpss
", which collects data and information required for further change-point analyses and summaries. See cpss.custom
.
Killick, R., Fearnhead, P., and Eckley, I. A. (2012). Optimal Detection of Changepoints With a Linear Computational Cost. Journal of the American Statistical Association, 107(500): 1590–1598. Fryzlewicz, P. (2014). Wild binary segmentation for multiple change-point detection. The Annals of Statistics, 42(6): 2243–2281.
library("cpss") set.seed(666) n <- 2048 tau <- c(205, 267, 308, 472, 512, 820, 902, 1332, 1557, 1598, 1659) seg_len <- diff(c(0, tau, n)) mu <- rep(c(0, 14.64, -3.66, 7.32, -7.32, 10.98, -4.39, 3.29, 19.03, 7.68, 15.37, 0), seg_len) ep <- 7 * rnorm(n) y <- mu + ep res <- cpss.mean(y, algorithm = "SN", ncps_max = 20) summary(res) # 205 267 307 471 512 820 897 1332 1557 1601 1659 plot(res, type = "scatter") plot(res, type = "path") out <- update(res, dim_update = 12) out@cps # 205 267 307 471 512 820 897 1332 1557 1601 1659 1769 # coef(out)
library("cpss") set.seed(666) n <- 2048 tau <- c(205, 267, 308, 472, 512, 820, 902, 1332, 1557, 1598, 1659) seg_len <- diff(c(0, tau, n)) mu <- rep(c(0, 14.64, -3.66, 7.32, -7.32, 10.98, -4.39, 3.29, 19.03, 7.68, 15.37, 0), seg_len) ep <- 7 * rnorm(n) y <- mu + ep res <- cpss.mean(y, algorithm = "SN", ncps_max = 20) summary(res) # 205 267 307 471 512 820 897 1332 1557 1601 1659 plot(res, type = "scatter") plot(res, type = "path") out <- update(res, dim_update = 12) out@cps # 205 267 307 471 512 820 897 1332 1557 1601 1659 1769 # coef(out)
Detecting changes in mean and (co)variance
cpss.meanvar( dataset, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
cpss.meanvar( dataset, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2 )
dataset |
a numeric matrix of dimension |
algorithm |
a character string specifying the change-point searching algorithm, one of the following choices: "SN" (segment neighborhood), "BS" (binary segmentation), "WBS" (wild binary segmentation) and "PELT" (pruned exact linear time) algorithms. |
dist_min |
an integer specifying minimum searching distance (length of feasible segments). |
ncps_max |
an integer specifying an upper bound of the number of true change-points. |
pelt_pen_val |
a numeric vector specifying candidate values of the penalty only if |
pelt_K |
a numeric value for pruning adjustment only if |
wbs_nintervals |
an integer specifying the number of random intervals drawn only if |
criterion |
a character string specifying the model selection criterion, "CV" ("cross-validation") or "MS" ("multiple-splitting"). |
times |
an integer specifying how many times of sample-splitting should be performed; It should be 2 if |
cpss.meanvar
returns an object of an S4 class, called "cpss
", which collects data and information required for further change-point analyses and summaries. See cpss.custom
.
Killick, R., Fearnhead, P., and Eckley, I. A. (2012). Optimal Detection of Changepoints With a Linear Computational Cost. Journal of the American Statistical Association, 107(500):1590–1598. Fryzlewicz, P. (2014). Wild binary segmentation for multiple change-point detection. The Annals of Statistics, 42(6): 2243–2281.
library("cpss") if (!requireNamespace("MASS", quietly = TRUE)) { stop("Please install the package \"MASS\".") } set.seed(666) n <- 1000 tau <- c(200, 400, 600, 800) mu <- list(rep(0, 2), rep(1, 2), rep(1, 2), rep(0, 2), rep(0, 2)) Sigma <- list(diag(2), diag(2), matrix(c(1,-1,-1, 4), 2), matrix(c(1, 0.5, 0.5, 1), 2), diag(2)) seg_len <- diff(c(0, tau, n)) y <- lapply(seq(1, length(tau) + 1), function(k) { MASS::mvrnorm(n = seg_len[k], mu = mu[[k]], Sigma = Sigma[[k]]) }) y <- do.call(rbind, y) res <- cpss.meanvar(y, algorithm = "BS", dist_min = 20) cps(res) # [1] 211 402 598 804 plot(res, type = "coef")
library("cpss") if (!requireNamespace("MASS", quietly = TRUE)) { stop("Please install the package \"MASS\".") } set.seed(666) n <- 1000 tau <- c(200, 400, 600, 800) mu <- list(rep(0, 2), rep(1, 2), rep(1, 2), rep(0, 2), rep(0, 2)) Sigma <- list(diag(2), diag(2), matrix(c(1,-1,-1, 4), 2), matrix(c(1, 0.5, 0.5, 1), 2), diag(2)) seg_len <- diff(c(0, tau, n)) y <- lapply(seq(1, length(tau) + 1), function(k) { MASS::mvrnorm(n = seg_len[k], mu = mu[[k]], Sigma = Sigma[[k]]) }) y <- do.call(rbind, y) res <- cpss.meanvar(y, algorithm = "BS", dist_min = 20) cps(res) # [1] 211 402 598 804 plot(res, type = "coef")
Detecting changes in (co)variance
cpss.var( dataset, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2, mu = NULL )
cpss.var( dataset, algorithm = "BS", dist_min = floor(log(n)), ncps_max = ceiling(n^0.4), pelt_pen_val = NULL, pelt_K = 0, wbs_nintervals = 500, criterion = "CV", times = 2, mu = NULL )
dataset |
a numeric matrix of dimension |
algorithm |
a character string specifying the change-point searching algorithm, one of the following choices: "SN" (segment neighborhood), "BS" (binary segmentation), "WBS" (wild binary segmentation) and "PELT" (pruned exact linear time) algorithms. |
dist_min |
an integer specifying minimum searching distance (length of feasible segments). |
ncps_max |
an integer specifying an upper bound of the number of true change-points. |
pelt_pen_val |
a numeric vector specifying candidate values of the penalty only if |
pelt_K |
a numeric value for pruning adjustment only if |
wbs_nintervals |
an integer specifying the number of random intervals drawn only if |
criterion |
a character string specifying the model selection criterion, "CV" ("cross-validation") or "MS" ("multiple-splitting"). |
times |
an integer specifying how many times of sample-splitting should be performed; It should be 2 if |
mu |
If a numeric vector or constant is supplied, it will be taken as the value of the common mean. By default it is |
cpss.var
returns an object of an S4 class, called "cpss
", which collects data and information required for further change-point analyses and summaries. See cpss.custom
.
Killick, R., Fearnhead, P., and Eckley, I. A. (2012). Optimal Detection of Changepoints With a Linear Computational Cost. Journal of the American Statistical Association, 107(500): 1590–1598. Fryzlewicz, P. (2014). Wild binary segmentation for multiple change-point detection. The Annals of Statistics, 42(6): 2243–2281.
library("cpss") if (!requireNamespace("MASS", quietly = TRUE)) { stop("Please install the package \"MASS\".") } set.seed(666) n <- 1000 tau <- c(200, 500, 750) mu <- list(rep(0, 2), rep(0, 2), rep(0, 2), rep(0, 2)) Sigma <- list(diag(2), matrix(c(1, 0, 0, 4), 2), matrix(c(1, -0.5, -0.5, 4), 2), diag(2)) seg_len <- diff(c(0, tau, n)) y <- lapply(seq(1, length(tau) + 1), function(k) { MASS::mvrnorm(n = seg_len[k], mu = mu[[k]], Sigma = Sigma[[k]]) }) y <- do.call(rbind, y) res <- cpss.var(y, algorithm = "BS", dist_min = 20) cps(res) # [1] 215 515 751
library("cpss") if (!requireNamespace("MASS", quietly = TRUE)) { stop("Please install the package \"MASS\".") } set.seed(666) n <- 1000 tau <- c(200, 500, 750) mu <- list(rep(0, 2), rep(0, 2), rep(0, 2), rep(0, 2)) Sigma <- list(diag(2), matrix(c(1, 0, 0, 4), 2), matrix(c(1, -0.5, -0.5, 4), 2), diag(2)) seg_len <- diff(c(0, tau, n)) y <- lapply(seq(1, length(tau) + 1), function(k) { MASS::mvrnorm(n = seg_len[k], mu = mu[[k]], Sigma = Sigma[[k]]) }) y <- do.call(rbind, y) res <- cpss.var(y, algorithm = "BS", dist_min = 20) cps(res) # [1] 215 515 751
Generic functions and methods: dat
dat(x) dat(x) <- value ## S4 method for signature 'cpss' dat(x) ## S4 replacement method for signature 'cpss' dat(x) <- value
dat(x) dat(x) <- value ## S4 method for signature 'cpss' dat(x) ## S4 replacement method for signature 'cpss' dat(x) <- value
x |
object from cpss |
value |
value assigned to x |
cpss |
cpss class |
Generic functions and methods: mdl
mdl(x) mdl(x) <- value ## S4 method for signature 'cpss' mdl(x) ## S4 replacement method for signature 'cpss' mdl(x) <- value
mdl(x) mdl(x) <- value ## S4 method for signature 'cpss' mdl(x) ## S4 replacement method for signature 'cpss' mdl(x) <- value
x |
object from cpss |
value |
value assigned to x |
Generic functions and methods: ncps
ncps(x) ncps(x) <- value ## S4 method for signature 'cpss' ncps(x) ## S4 replacement method for signature 'cpss' ncps(x) <- value
ncps(x) ncps(x) <- value ## S4 method for signature 'cpss' ncps(x) ## S4 replacement method for signature 'cpss' ncps(x) <- value
x |
object from cpss |
value |
value assigned to x |
Generic functions and methods: params
params(x) params(x) <- value ## S4 method for signature 'cpss' params(x) ## S4 replacement method for signature 'cpss' params(x) <- value
params(x) params(x) <- value ## S4 method for signature 'cpss' params(x) ## S4 replacement method for signature 'cpss' params(x) <- value
x |
object from cpss |
value |
value assigned to x |
Generic functions and methods: pelt_pen
pelt_pen(x) pelt_pen(x) <- value ## S4 method for signature 'cpss' pelt_pen(x) ## S4 replacement method for signature 'cpss' pelt_pen(x) <- value
pelt_pen(x) pelt_pen(x) <- value ## S4 method for signature 'cpss' pelt_pen(x) ## S4 replacement method for signature 'cpss' pelt_pen(x) <- value
x |
object from cpss |
value |
value assigned to x |
plot method
## S4 method for signature 'cpss' plot(obj, type, x = c(), y = c(), ...)
## S4 method for signature 'cpss' plot(obj, type, x = c(), y = c(), ...)
obj |
object from cpss |
type |
type of visualization |
x |
x |
y |
y |
... |
... |
cpss |
cpss class |
Generic functions and methods: S_vals
S_vals(x) S_vals(x) <- value ## S4 method for signature 'cpss' S_vals(x) ## S4 replacement method for signature 'cpss' S_vals(x) <- value
S_vals(x) S_vals(x) <- value ## S4 method for signature 'cpss' S_vals(x) ## S4 replacement method for signature 'cpss' S_vals(x) <- value
x |
object from cpss |
value |
value assigned to x |
Generic functions and methods: SC
SC(x) SC(x) <- value ## S4 method for signature 'cpss' SC(x) ## S4 replacement method for signature 'cpss' SC(x) <- value
SC(x) SC(x) <- value ## S4 method for signature 'cpss' SC(x) ## S4 replacement method for signature 'cpss' SC(x) <- value
x |
object from cpss |
value |
value assigned to x |
Generic functions and methods: SC_vals
SC_vals(x) SC_vals(x) <- value ## S4 method for signature 'cpss' SC_vals(x) ## S4 replacement method for signature 'cpss' SC_vals(x) <- value
SC_vals(x) SC_vals(x) <- value ## S4 method for signature 'cpss' SC_vals(x) ## S4 replacement method for signature 'cpss' SC_vals(x) <- value
x |
object from cpss |
value |
value assigned to x |
summary method
## S4 method for signature 'cpss' summary(object)
## S4 method for signature 'cpss' summary(object)
object |
object from cpss |
cpss |
cpss class |
Generic functions and methods: update_inputs
update_inputs(x) update_inputs(x) <- value ## S4 method for signature 'cpss' update_inputs(x) ## S4 replacement method for signature 'cpss' update_inputs(x) <- value
update_inputs(x) update_inputs(x) <- value ## S4 method for signature 'cpss' update_inputs(x) ## S4 replacement method for signature 'cpss' update_inputs(x) <- value
x |
object from cpss |
value |
value assigned to x |
update method
## S4 method for signature 'cpss' update(object, dim_update)
## S4 method for signature 'cpss' update(object, dim_update)
object |
object from cpss |
dim_update |
model dimension to update |
cpss |
cpss class |
Measurements of the nuclear magnetic response of underground rocks.
well
well
A vector of 4,050 measurements:
Measurements.