File: loo.R

package info (click to toggle)
r-cran-rsample 1.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,932 kB
  • sloc: sh: 13; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#' Leave-One-Out Cross-Validation
#'
#' Leave-one-out (LOO) cross-validation uses one data point in the original
#'  set as the assessment data and all other data points as the analysis set. A
#'  LOO resampling set has as many resamples as rows in the original data set.
#' @inheritParams vfold_cv
#' @return An tibble with classes `loo_cv`, `rset`, `tbl_df`, `tbl`, and
#'  `data.frame`. The results include a column for the data split objects and
#'  one column called `id` that has a character string with the resample
#'  identifier.
#' @examples
#' loo_cv(mtcars)
#' @export
loo_cv <- function(data, ...) {
  check_dots_empty()
  split_objs <- vfold_splits(data = data, v = nrow(data))
  split_objs <-
    list(
      splits = map(split_objs$splits, change_class),
      id = paste0("Resample", seq_along(split_objs$id))
    )

  ## We remove the holdout indices since it will save space and we can
  ## derive them later when they are needed.

  split_objs$splits <- map(split_objs$splits, rm_out)

  new_rset(
    splits = split_objs$splits,
    ids = split_objs$id,
    subclass = c("loo_cv", "rset")
  )
}

change_class <- function(x) {
  class(x) <- c("rsplit", "loo_split")
  x
}