Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 40 additions & 37 deletions R/spatial_vfold_cv.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,25 @@
#' ames_neighborhoods <- spatial_leave_location_out_cv(ames_sf, Neighborhood)
#'
#' @export
spatial_buffer_vfold_cv <- function(data,
radius,
buffer,
v = 10,
repeats = 1,
strata = NULL,
breaks = 4,
pool = 0.1,
...) {
spatial_buffer_vfold_cv <- function(
data,
radius,
buffer,
v = 10,
repeats = 1,
strata = NULL,
breaks = 4,
pool = 0.1,
...
) {
standard_checks(data, "`spatial_buffer_vfold_cv()`")

if (missing(radius) || missing(buffer)) {
use_vfold <- NULL
if (missing(radius) && missing(buffer)) {
use_vfold <- c(i = "Or use `rsample::vfold_cv() to use a non-spatial V-fold.")
use_vfold <- c(
i = "Or use `rsample::vfold_cv() to use a non-spatial V-fold."
)
}
rlang::abort(
c(
Expand All @@ -88,15 +92,6 @@ spatial_buffer_vfold_cv <- function(data,

n <- nrow(data)
v <- check_v(v, n, "rows")
if (v == n && repeats > 1) {
rlang::abort(
c(
"Repeated cross-validation doesn't make sense when performing leave-one-out cross-validation.",
i = "Set `v` to a lower value.",
i = "Or set `repeats = 1`."
)
)
}

rset <- rsample::vfold_cv(
data = data,
Expand All @@ -113,7 +108,9 @@ spatial_buffer_vfold_cv <- function(data,
if (length(strata) == 0) strata <- NULL
}

if (!is.null(strata)) names(strata) <- NULL
if (!is.null(strata)) {
names(strata) <- NULL
}
cv_att <- list(
v = v,
repeats = repeats,
Expand Down Expand Up @@ -151,21 +148,25 @@ spatial_buffer_vfold_cv <- function(data,
#' @rdname spatial_vfold
#'
#' @export
spatial_leave_location_out_cv <- function(data,
group,
v = NULL,
radius = NULL,
buffer = NULL,
...,
repeats = 1) {
spatial_leave_location_out_cv <- function(
data,
group,
v = NULL,
radius = NULL,
buffer = NULL,
...,
repeats = 1
) {
if (!missing(group)) {
group <- tidyselect::eval_select(rlang::enquo(group), data)
}

if (missing(group) || length(group) == 0) {
group <- NULL
} else {
if (is.null(v)) v <- length(unique(data[[group]]))
if (is.null(v)) {
v <- length(unique(data[[group]]))
}
v <- check_v(v, length(unique(data[[group]])), "locations")
n <- nrow(data)
if (v == n && repeats > 1) {
Expand Down Expand Up @@ -215,15 +216,17 @@ spatial_leave_location_out_cv <- function(data,
)
}

posthoc_buffer_rset <- function(data,
rset,
rsplit_class,
rset_class,
radius,
buffer,
n,
v,
cv_att) {
posthoc_buffer_rset <- function(
data,
rset,
rsplit_class,
rset_class,
radius,
buffer,
n,
v,
cv_att
) {
# This basically undoes everything post-`split_unnamed` for us
# so we're back to an unnamed list of assessment-set indices
indices <- purrr::map(rset$splits, as.integer, "assessment")
Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/_snaps/autoplot/block-plots-with-grid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/autoplot/buffered-rsample-plot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading