R/make_folds.R

Defines functions make_folds

Documented in make_folds

#' @rdname cforward
#' @export
make_folds = function(data,
                      event_status = "mortstat",
                      n_folds = 10,
                      verbose = TRUE){
  fold_number = NULL
  rm(list="fold_number")
  if (!"fold_number" %in% colnames(data)) {
    if (verbose) {
      message(paste0("Creating ", n_folds, " Folds"))
    }
    data = as.data.frame(data)
    ss = split(data, data[[event_status]])
    ss = lapply(ss, function(x) {
      folds = rep(1:n_folds, length.out = ceiling(nrow(x)/n_folds)*n_folds)
      folds = sample(folds)
      x$fold_number = folds[seq(nrow(x))]
      x
    })
    data = unsplit(ss, data[[event_status]])
    data = tibble::as_tibble(data)
  } else {
    msg = "fold_number in data, using these folds"
    if (verbose) {
      message(msg)
    }
  }
  stopifnot(!anyNA(data$fold_number))
  if (verbose) {
    data %>%
      dplyr::count(fold_number) %>%
      print()
  }
  data
}

Try the cforward package in your browser

Any scripts or data that you put into this service are public.

cforward documentation built on March 29, 2021, 5:07 p.m.