# 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"))
Run the code above in your browser using DataLab