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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
rsplit <- function(data, in_id, out_id) {
if (!is.data.frame(data) & !is.matrix(data)) {
rlang::abort("`data` must be a data frame.")
}
if (!is.integer(in_id) | any(in_id < 1)) {
rlang::abort("`in_id` must be a positive integer vector.")
}
if (!all(is.na(out_id))) {
if (!is.integer(out_id) | any(out_id < 1)) {
rlang::abort("`out_id` must be a positive integer vector.")
}
}
if (length(in_id) == 0) {
rlang::abort("At least one row should be selected for the analysis set.")
}
structure(
list(
data = data,
in_id = in_id,
out_id = out_id
),
class = "rsplit"
)
}
#' @export
print.rsplit <- function(x, ...) {
out_char <-
if (is_missing_out_id(x)) {
paste(length(complement(x)))
} else {
paste(length(x$out_id))
}
cat("<Analysis/Assess/Total>\n")
cat("<",
length(x$in_id), "/",
out_char, "/",
nrow(x$data), ">\n",
sep = ""
)
}
#' @export
as.integer.rsplit <-
function(x, data = c("analysis", "assessment"), ...) {
data <- match.arg(data)
if (data == "analysis") {
out <- x$in_id
} else {
out <- if (is_missing_out_id(x)) {
complement(x)
} else {
x$out_id
}
}
out
}
#' Convert an `rsplit` object to a data frame
#'
#' The analysis or assessment code can be returned as a data
#' frame (as dictated by the `data` argument) using
#' `as.data.frame.rsplit`. `analysis` and
#' `assessment` are shortcuts.
#' @param x An `rsplit` object.
#' @param row.names `NULL` or a character vector giving the row names for the data frame. Missing values are not allowed.
#' @param optional A logical: should the column names of the data be checked for legality?
#' @param data Either "analysis" or "assessment" to specify which data are returned.
#' @param ... Additional arguments to be passed to or from methods. Not currently used.
#' @examples
#' library(dplyr)
#' set.seed(104)
#' folds <- vfold_cv(mtcars)
#'
#' model_data_1 <- folds$splits[[1]] %>% analysis()
#' holdout_data_1 <- folds$splits[[1]] %>% assessment()
#' @export
as.data.frame.rsplit <-
function(x,
row.names = NULL,
optional = FALSE,
data = "analysis",
...) {
if (!is.null(row.names)) {
rlang::warn(paste0(
"`row.names` is kept for consistency with the underlying class but ",
"non-NULL values will be ignored."
))
}
if (optional) {
rlang::warn(paste0(
"`optional` is kept for consistency with the underlying class but ",
"TRUE values will be ignored."
))
}
if (!is.null(x$col_id)) {
if (identical(data, "assessment")) {
rsplit_class <- class(x)[[1]]
msg <- paste0(
"There is no assessment data set for an `rsplit` object",
" with class `", rsplit_class, "`."
)
rlang::abort(msg)
}
permuted_col <-
x$data[as.integer(x, data = data, ...), x$col_id, drop = FALSE]
x$data[, x$col_id] <- permuted_col
return(x$data)
}
x$data[as.integer(x, data = data, ...), , drop = FALSE]
}
#' @rdname as.data.frame.rsplit
#' @export
analysis <- function(x, ...) {
if (!inherits(x, "rsplit")) {
rlang::abort("`x` should be an `rsplit` object")
}
as.data.frame(x, data = "analysis", ...)
}
#' @rdname as.data.frame.rsplit
#' @export
assessment <- function(x, ...) {
if (!inherits(x, "rsplit")) {
rlang::abort("`x` should be an `rsplit` object")
}
as.data.frame(x, data = "assessment", ...)
}
#' @export
dim.rsplit <- function(x, ...) {
c(
analysis = length(x$in_id),
assessment = length(complement(x)),
n = nrow(x$data),
p = ncol(x$data)
)
}
#' @method obj_sum rsplit
#' @export
obj_sum.rsplit <- function(x, ...) {
out_char <-
if (is_missing_out_id(x)) {
paste(length(complement(x)))
} else {
paste(length(x$out_id))
}
paste0(
"split [",
length(x$in_id), "/",
out_char, "]"
)
}
#' @method type_sum rsplit
#' @export
type_sum.rsplit <- function(x, ...) {
out_char <-
if (is_missing_out_id(x)) {
format_n(length(complement(x)))
} else {
format_n(length(x$out_id))
}
paste0(
"split [",
format_n(length(x$in_id)), "/",
out_char, "]"
)
}
format_n <- function(x, digits = 1) {
case_when(
log10(x) < 3 ~ paste(x),
log10(x) >= 3 & log10(x) < 6 ~ paste0(round(x / 1000, digits = digits), "K"),
TRUE ~ paste0(round(x / 1000000, digits = digits), "M"),
)
}
is_rsplit <- function(x) {
inherits(x, "rsplit")
}
|