VfoldCV {crossvalidationCP} | R Documentation |
V-fold cross-validation
Description
Selects the number of change-points by minimizing a V-fold cross-validation criterion. The criterion, the estimator, and the number of folds can be specified by the user.
Usage
VfoldCV(Y, V = 5L, Kmax = 8L, adaptiveKmax = TRUE, tolKmax = 3L, estimator = leastSquares,
criterion = criterionL1loss, output = c("param", "fit", "detailed"), ...)
Arguments
Y |
the observations, can be any data type that supports the function |
V |
a single integer giving the number of folds. Ordered folds will automatically be created, i.e. fold |
Kmax |
a single integer giving maximal number of change-points |
adaptiveKmax |
a single logical indicating whether |
tolKmax |
a single integer specifiying how much the estimated number of change-points have to be smaller than |
estimator |
a function providing a local estimate. For pre-implemented estimators see estimators. The function must have the arguments |
criterion |
a function providing the cross-validation criterion. For pre-implemented criteria see criteria. The function must have the arguments |
output |
a string specifying the output, either |
... |
additional parameters that are passed to |
Value
if output == "param"
, the selected number of change-points, i.e. an integer between 0
and Kmax
. If output == "fit"
, a list with the entries param
, giving the selected number of change-points, and fit
. The named entry fit
is a list giving the returned fit obtained by applying estimator
to the whole data Y
with the selected tuning parameter. The returned value is transformed to a list with an entry cps
giving the estimated change-points and, if provided by estimator
, an entry value
giving the estimated local values. If output == "detailed"
, the same as for output == "fit"
, but additionally an entry CP
giving all calculated cross-validation criteria. Those values are summed over all folds
References
Pein, F., and Shah, R. D. (2021) Cross-validation for change-point regression: pitfalls and solutions. arXiv:2112.03220.
See Also
estimators, criteria, convertSingleParam
Examples
# call with default parameters:
# 5-fold cross-validation with absolute error loss, least squares estimation,
# and 0 to 5 change-points
VfoldCV(Y = rnorm(100))
# more interesting data and more detailed output
set.seed(1L)
Y <- c(rnorm(50), rnorm(50, 5), rnorm(50), rnorm(50, 5))
VfoldCV(Y = Y, output = "detailed")
# finds the correct change-points at 50, 100, 150
# (plus the start and end points 0 and 200)
# reducing the number of folds to 3
VfoldCV(Y = Y, V = 3L, output = "detailed")
# reducing the maximal number of change-points to 2
VfoldCV(Y = Y, Kmax = 2)
# different criterion: modified error loss
VfoldCV(Y = Y, output = "detailed", criterion = criterionMod)
# manually given criterion; identical to criterionL1loss()
testCriterion <- function(testset, estset, value = NULL, ...) {
if (!is.null(value)) {
return(sum(abs(testset - value)))
}
sum(abs(testset - mean(estset)))
}
identical(VfoldCV(Y = Y, criterion = testCriterion, output = "detailed"),
VfoldCV(Y = Y, output = "detailed"))