1
|
|
#' Leave-One-Out Cross-Validation
|
2
|
|
#'
|
3
|
|
#' Leave-one-out (LOO) cross-validation uses one data point in the original
|
4
|
|
#' set as the assessment data and all other data points as the analysis set. A
|
5
|
|
#' LOO resampling set has as many resamples as rows in the original data set.
|
6
|
|
#' @inheritParams vfold_cv
|
7
|
|
#' @return An tibble with classes `loo_cv`, `rset`, `tbl_df`, `tbl`, and
|
8
|
|
#' `data.frame`. The results include a column for the data split objects and
|
9
|
|
#' one column called `id` that has a character string with the resample
|
10
|
|
#' identifier.
|
11
|
|
#' @examples
|
12
|
|
#' loo_cv(mtcars)
|
13
|
|
#' @export
|
14
|
|
loo_cv <- function(data, ...) {
|
15
|
1
|
split_objs <- vfold_splits(data = data, v = nrow(data))
|
16
|
1
|
split_objs <-
|
17
|
1
|
list(splits = map(split_objs$splits, change_class),
|
18
|
1
|
id = paste0("Resample", seq_along(split_objs$id)))
|
19
|
|
|
20
|
|
## We remove the holdout indices since it will save space and we can
|
21
|
|
## derive them later when they are needed.
|
22
|
|
|
23
|
1
|
split_objs$splits <- map(split_objs$splits, rm_out)
|
24
|
|
|
25
|
1
|
new_rset(splits = split_objs$splits,
|
26
|
1
|
ids = split_objs$id,
|
27
|
1
|
subclass = c("loo_cv", "rset"))
|
28
|
|
}
|
29
|
|
|
30
|
|
#' @export
|
31
|
|
print.loo_cv <- function(x, ...) {
|
32
|
1
|
cat("#", pretty(x), "\n")
|
33
|
1
|
class(x) <- class(x)[!(class(x) %in% c("loo_cv", "rset"))]
|
34
|
1
|
print(x, ...)
|
35
|
|
}
|
36
|
|
|
37
|
|
change_class <- function(x) {
|
38
|
1
|
class(x) <- c("rsplit", "loo_split")
|
39
|
1
|
x
|
40
|
|
}
|
41
|
|
|