R/twk.randomly.R

Defines functions randomly

Documented in randomly

#' @importFrom purrr map
NULL


#' randomly
#'
#' Folding strategie
#'
#' @param data Training data
#' @param sample_method Name of folding strategie.
#' \itemize{
#'   \item \strong{cv} Cross-Validation. k can be choosen (Default: 5)
#' }
#' @param k Number of folds.
#' @param ... further arguments depends on the method.
#'
#' @return List of indices in train data.
#'
#' @examples
#' set.seed(123)
#' folds <- randomly(iris, 'cv', k=5)
#'
#' @export
randomly <- function(data, sample_method="cv", k=5, ...) {

  if(k == 1)
    return(1:nrow(data))

  if (sample_method=="cv") {

    n <- nrow(data)
    in_test <- split(seq_len(n), sample(rep(1:k, length.out = n)))
    return(map(in_test, ~(1:n)[-.x]))

  }

  stop("no valid sample method")
}
illoRocks/tweakr documentation built on July 23, 2019, 3:54 p.m.