From c798813495722b5d21b449c49d7dff800a4c2076 Mon Sep 17 00:00:00 2001 From: Patrick Schratz Date: Sun, 19 Jan 2020 21:20:22 +0100 Subject: [PATCH] Add ResamplingRepeatedSpCVEnv (#32) --- .gitignore | 1 + DESCRIPTION | 2 + NAMESPACE | 2 + R/ResamplingRepeatedSpCVCoords.R | 10 + R/ResamplingRepeatedSpCVEnv.R | 153 + R/ResamplingSpCVEnv.R | 20 +- R/autoplot.R | 33 + R/zzz.R | 2 + README.Rmd | 3 +- README.md | 1 + man/ResamplingRepeatedSpCVCoords.Rd | 27 + man/ResamplingRepeatedSpCVEnv.Rd | 148 + man/ResamplingSpCVEnv.Rd | 13 +- man/autoplot_spatial_resampling.Rd | 24 + .../autoplot/repeatedspcvcoords-fold-1-4.svg | 4 +- .../autoplot/repeatedspcvcoords-fold-1.svg | 4 +- .../repeatedspcvenv-all-test-sets.svg | 882 +++++ .../autoplot/repeatedspcvenv-fold-1-4.svg | 3432 +++++++++++++++++ .../figs/autoplot/repeatedspcvenv-fold-1.svg | 882 +++++ tests/figs/autoplot/spcvblock-fold-1-4.svg | 4 +- tests/figs/autoplot/spcvblock-fold-1.svg | 4 +- tests/figs/autoplot/spcvbuffer-fold-1-4.svg | 4 +- tests/figs/autoplot/spcvbuffer-fold-1.svg | 4 +- tests/figs/autoplot/spcvcoords-fold-1-4.svg | 4 +- tests/figs/autoplot/spcvcoords-fold-1.svg | 4 +- tests/figs/autoplot/spcvenv-fold-1-4.svg | 4 +- tests/figs/autoplot/spcvenv-fold-1.svg | 4 +- .../testthat/test-ResamplingRepeatedSpCVEnv.R | 15 + tests/testthat/test-autoplot.R | 23 + tests/testthat/test_mlr_spcv_generic.R | 7 +- tests/testthat/test_spcv_generic.R | 9 +- 31 files changed, 5685 insertions(+), 44 deletions(-) create mode 100644 R/ResamplingRepeatedSpCVEnv.R create mode 100644 man/ResamplingRepeatedSpCVEnv.Rd create mode 100644 tests/figs/autoplot/repeatedspcvenv-all-test-sets.svg create mode 100644 tests/figs/autoplot/repeatedspcvenv-fold-1-4.svg create mode 100644 tests/figs/autoplot/repeatedspcvenv-fold-1.svg create mode 100644 tests/testthat/test-ResamplingRepeatedSpCVEnv.R diff --git a/.gitignore b/.gitignore index 557faab14..f7069779d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ renv renv.lock inst/doc +man/figures/README* diff --git a/DESCRIPTION b/DESCRIPTION index 43b199c3e..0032f19ef 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,6 +42,7 @@ VignetteBuilder: RdMacros: mlr3misc Remotes: + mlr-org/mlr3misc, rvalavi/blockCV Encoding: UTF-8 LazyData: true @@ -56,6 +57,7 @@ Collate: 'ResamplingSpCVCoords.R' 'ResamplingSpCVEnv.R' 'ResamplingRepeatedSpCVCoords.R' + 'ResamplingRepeatedSpCVEnv.R' 'TaskClassifST.R' 'TaskRegrST.R' 'helper.R' diff --git a/NAMESPACE b/NAMESPACE index 0351b5b34..a66b05410 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,11 +1,13 @@ # Generated by roxygen2: do not edit by hand S3method(autoplot,ResamplingRepeatedSpCVCoords) +S3method(autoplot,ResamplingRepeatedSpCVEnv) S3method(autoplot,ResamplingSpCVBlock) S3method(autoplot,ResamplingSpCVBuffer) S3method(autoplot,ResamplingSpCVCoords) S3method(autoplot,ResamplingSpCVEnv) export(ResamplingRepeatedSpCVCoords) +export(ResamplingRepeatedSpCVEnv) export(ResamplingSpCVBlock) export(ResamplingSpCVBuffer) export(ResamplingSpCVCoords) diff --git a/R/ResamplingRepeatedSpCVCoords.R b/R/ResamplingRepeatedSpCVCoords.R index 896fd9864..7876100aa 100644 --- a/R/ResamplingRepeatedSpCVCoords.R +++ b/R/ResamplingRepeatedSpCVCoords.R @@ -52,11 +52,17 @@ ResamplingRepeatedSpCVCoords = R6Class("ResamplingRepeatedSpCVCoords", }, + #' @description Translates iteration numbers to fold number. + #' @param iters `integer()`\cr + #' Iteration number. folds = function(iters) { iters = assert_integerish(iters, any.missing = FALSE, coerce = TRUE) ((iters - 1L) %% as.integer(self$param_set$values$repeats)) + 1L }, + #' @description Translates iteration numbers to repetition number. + #' @param iters `integer()`\cr + #' Iteration number. repeats = function(iters) { iters = assert_integerish(iters, any.missing = FALSE, coerce = TRUE) ((iters - 1L) %/% as.integer(self$param_set$values$folds)) + 1L @@ -83,6 +89,10 @@ ResamplingRepeatedSpCVCoords = R6Class("ResamplingRepeatedSpCVCoords", ), active = list( + + #' @field iters `integer(1)`\cr + #' Returns the number of resampling iterations, depending on the + #' values stored in the `param_set`. iters = function() { pv = self$param_set$values as.integer(pv$repeats) * as.integer(pv$folds) diff --git a/R/ResamplingRepeatedSpCVEnv.R b/R/ResamplingRepeatedSpCVEnv.R new file mode 100644 index 000000000..d81c3c007 --- /dev/null +++ b/R/ResamplingRepeatedSpCVEnv.R @@ -0,0 +1,153 @@ +#' @title Repeated Environmental Block Cross Validation Resampling +#' +#' @import mlr3 +#' +#' @description Environmental Block Cross Validation. This strategy uses k-means +#' clustering to specify blocks of similar environmental conditions. Only +#' numeric features can be used. The `features` used for building blocks can +#' be specified in the `param_set`. By default, all numeric features are used. +#' +#' @references +#' \cite{mlr3spatiotempcv}{valavi2018} +#' +#' @export +#' @examples +#' library(mlr3) +#' task = tsk("ecuador") +#' +#' # Instantiate Resampling +#' rrcv = rsmp("repeated-spcv-env") +#' rrcv$param_set$values = list(folds = 4, repeats = 2) +#' rrcv$instantiate(task) +#' +#' # Individual sets: +#' rrcv$train_set(1) +#' rrcv$test_set(1) +#' intersect(rrcv$train_set(1), rrcv$test_set(1)) +#' +#' # Internal storage: +#' rrcv$instance +ResamplingRepeatedSpCVEnv = R6Class("ResamplingRepeatedSpCVEnv", + inherit = mlr3::Resampling, + + public = list( + #' @description + #' Create an "coordinate-based" repeated resampling instance. + #' @param id `character(1)`\cr + #' Identifier for the resampling strategy. + initialize = function(id = "repeated-spcv-env") { + ps = ParamSet$new(params = list( + ParamInt$new("repeats", lower = 1), + ParamInt$new("folds", lower = 1L, tags = "required") + )) + ps$values = list(folds = 10L) + super$initialize( + id = id, + param_set = ps, + man = "mlr3spatiotempcv::mlr_resamplings_repeated_spcv_env" + ) + + }, + + #' @description Translates iteration numbers to fold number. + #' @param iters `integer()`\cr + #' Iteration number. + folds = function(iters) { + iters = assert_integerish(iters, any.missing = FALSE, coerce = TRUE) + ((iters - 1L) %% as.integer(self$param_set$values$repeats)) + 1L + }, + + #' @description Translates iteration numbers to repetition number. + #' @param iters `integer()`\cr + #' Iteration number. + repeats = function(iters) { + iters = assert_integerish(iters, any.missing = FALSE, coerce = TRUE) + ((iters - 1L) %/% as.integer(self$param_set$values$folds)) + 1L + }, + + #' @description + #' Materializes fixed training and test splits for a given task. + #' @param task [Task]\cr + #' A task to instantiate. + instantiate = function(task) { + + assert_task(task) + pv = self$param_set$values + + # Set values to default if missing + if (is.null(pv$rows)) { + pv$rows = self$param_set$default[["rows"]] + } + if (is.null(pv$cols)) { + pv$cols = self$param_set$default[["cols"]] + } + if (is.null(pv$features)) { + pv$features = task$feature_names + } + + # Remove non-numeric features, target and coordinates + columns = task$col_info[!id %in% + c(task$target_names, "x", "y")][type == "numeric"] + + # Check for selected features that are not in task + diff = setdiff(pv$features, columns[, id]) + if (length(diff) > 0) { + stopf("'spcv-env' requires numeric features for clustering. + Feature '%s' is either non-numeric or does not exist in the data.", + diff, wrap = TRUE) + } + columns = columns[id %in% pv$features] + columns = columns[, id] + + data = task$data()[, columns, with = FALSE] + + instance = private$.sample(task$row_ids, data) + + self$instance = instance + self$task_hash = task$hash + invisible(self) + } + ), + + active = list( + + #' @field iters `integer(1)`\cr + #' Returns the number of resampling iterations, depending on the + #' values stored in the `param_set`. + iters = function() { + pv = self$param_set$values + as.integer(pv$repeats) * as.integer(pv$folds) + } + ), + + private = list( + .sample = function(ids, coords) { + pv = self$param_set$values + folds = as.integer(pv$folds) + + map_dtr(seq_len(pv$repeats), function(i) { + data.table(row_id = ids, rep = i, + fold = kmeans(coords, centers = folds)$cluster + ) + }) + }, + + .get_train = function(i) { + i = as.integer(i) - 1L + folds = as.integer(self$param_set$values$folds) + rep = i %/% folds + 1L + fold = i %% folds + 1L + ii = data.table(rep = rep, fold = seq_len(folds)[-fold]) + self$instance[ii, "row_id", on = names(ii), nomatch = 0L][[1L]] + }, + + .get_test = function(i) { + i = as.integer(i) - 1L + folds = as.integer(self$param_set$values$folds) + rep = i %/% folds + 1L + fold = i %% folds + 1L + ii = data.table(rep = rep, fold = fold) + self$instance[ii, "row_id", on = names(ii), nomatch = 0L][[1L]] + } + ) +) diff --git a/R/ResamplingSpCVEnv.R b/R/ResamplingSpCVEnv.R index 247ad40cf..67bc2f504 100644 --- a/R/ResamplingSpCVEnv.R +++ b/R/ResamplingSpCVEnv.R @@ -2,15 +2,14 @@ #' #' @import mlr3 #' -#' @description Environmental Block Cross Validation. This strategy uses k-means -#' clustering to specify blocks of smilar environmental conditions. Only numeric -#' features can be used. The `features` used for building blocks can be -#' specified in the `param_set`. By default, all numeric features are used. +#' @description +#' Environmental Block Cross Validation. This strategy uses k-means clustering +#' to specify blocks of similar environmental conditions. Only numeric features +#' can be used. The `features` used for building blocks can be specified in the +#' `param_set`. By default, all numeric features are used. #' -#' @references Valavi R, Elith J, Lahoz-Monfort JJ, Guillera-Arroita G. blockCV: -#' An r package for generating spatially or environmentally separated folds for -#' k-fold cross-validation of species distribution models. Methods Ecol Evol. -#' 2019; 10:225–232. https://doi.org/10.1111/2041-210X.13107 +#' @references +#' \cite{mlr3spatiotempcv}{valavi2018} #' #' @export #' @examples @@ -80,8 +79,9 @@ ResamplingSpCVEnv = R6Class("ResamplingSpCVEnv", inherit = mlr3::Resampling, # Check for selected features that are not in task diff = setdiff(pv$features, columns[, id]) if (length(diff) > 0) { - stop(sprintf("'spcv-env' requires numeric features for clustering. Feature '%s' is either non-numeric or does not exist in the data", - diff)) + stopf("'spcv-env' requires numeric features for clustering. + Feature '%s' is either non-numeric or does not exist in the data.", + diff, wrap = TRUE) } columns = columns[id %in% pv$features] columns = columns[, id] diff --git a/R/autoplot.R b/R/autoplot.R index d8e938b49..0b616e633 100644 --- a/R/autoplot.R +++ b/R/autoplot.R @@ -199,6 +199,39 @@ autoplot.ResamplingRepeatedSpCVCoords = function( grid = grid) } +#' @title Plot for Repeated Spatial Resampling +#' +#' @rdname autoplot_spatial_resampling +#' @export +#' @examples +#' ##### +#' # RepeatedSpCVEnv +#' ##### +#' \donttest{ +#' task = tsk("ecuador") +#' resampling = rsmp("repeated-spcv-env", folds = 10, repeats = 2) +#' resampling$instantiate(task) +#' autoplot(resampling, task) +#' autoplot(resampling, task, 1) +#' autoplot(resampling, task, fold_id = 2, repeats_id = 2) +#' autoplot(resampling, task, c(1, 2, 3, 4)) +#' } +autoplot.ResamplingRepeatedSpCVEnv = function( + object, + task, + fold_id = NULL, + repeats_id = 1, + grid = TRUE, + train_color = "#0072B5", + test_color = "#E18727", + ...) { + autoplot_spatial(resampling = object, + task = task, + fold_id = fold_id, + repeats_id = repeats_id, + grid = grid) +} + autoplot_spatial = function( resampling = NULL, task = NULL, diff --git a/R/zzz.R b/R/zzz.R index 08090e27e..3af92de3a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -43,7 +43,9 @@ register_mlr3 = function() { mlr_resamplings$add("spcv-buffer", ResamplingSpCVBuffer) mlr_resamplings$add("spcv-coords", ResamplingSpCVCoords) mlr_resamplings$add("spcv-env", ResamplingSpCVEnv) + mlr_resamplings$add("repeated-spcv-coords", ResamplingRepeatedSpCVCoords) + mlr_resamplings$add("repeated-spcv-env", ResamplingRepeatedSpCVEnv) } } diff --git a/README.Rmd b/README.Rmd index fc6288f6d..ab28fb5a8 100644 --- a/README.Rmd +++ b/README.Rmd @@ -30,7 +30,8 @@ Currently, the following ones are implemented: | Spatial CV | [sperrorest](https://github.com/giscience-fsu/sperrorest) | Brenning 2012 | `ResamplingSpCVCoords` | `rsmp("spcv-coords")` | | Environmental Blocking | [blockCV](https://github.com/rvalavi/blockCV) | Valavi 2019 | `ResamplingSpCVEnv` | `rsmp("spcv-env")` | | --- | --- | --- | --- | --- | -| Repeated Spatial CV | [sperrorest](https://github.com/giscience-fsu/sperrorest) | Brenning 2012 | `RepeatedResamplingSpCVCoords` | `rsmp("repeated-spcv-coords")` | +| Repeated Spatial CV | [sperrorest](https://github.com/giscience-fsu/sperrorest) | Brenning 2012 | `RepeatedResamplingSpCVCoords` | `rsmp("repeated-spcv-coords")` | +| Repeated Env Blocking | [blockCV](https://github.com/rvalavi/blockCV) | Valavi 2019 | `RepeatedResamplingSpCVEnv` | `rsmp("repeated-spcv-env")` | ## Spatial tasks diff --git a/README.md b/README.md index 7bd80aff9..d01d9e1e0 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Currently, the following ones are implemented: | Environmental Blocking | [blockCV](https://github.com/rvalavi/blockCV) | Valavi 2019 | `ResamplingSpCVEnv` | `rsmp("spcv-env")` | | — | — | — | — | — | | Repeated Spatial CV | [sperrorest](https://github.com/giscience-fsu/sperrorest) | Brenning 2012 | `RepeatedResamplingSpCVCoords` | `rsmp("repeated-spcv-coords")` | +| Repeated Env Blocking | [blockCV](https://github.com/rvalavi/blockCV) | Valavi 2019 | `RepeatedResamplingSpCVEnv` | `rsmp("repeated-spcv-env")` | ## Spatial tasks diff --git a/man/ResamplingRepeatedSpCVCoords.Rd b/man/ResamplingRepeatedSpCVCoords.Rd index db0b920f5..f5aa1ca88 100644 --- a/man/ResamplingRepeatedSpCVCoords.Rd +++ b/man/ResamplingRepeatedSpCVCoords.Rd @@ -35,6 +35,15 @@ rrcv$instance # table \section{Super class}{ \code{\link[mlr3:Resampling]{mlr3::Resampling}} -> \code{ResamplingRepeatedSpCVCoords} } +\section{Active bindings}{ +\if{html}{\out{
}} +\describe{ +\item{\code{iters}}{\code{integer(1)}\cr +Returns the number of resampling iterations, depending on the +values stored in the \code{param_set}.} +} +\if{html}{\out{
}} +} \section{Methods}{ \subsection{Public methods}{ \itemize{ @@ -75,18 +84,36 @@ Identifier for the resampling strategy.} \if{html}{\out{
}} \if{html}{\out{}} \subsection{Method \code{folds()}}{ +Translates iteration numbers to fold number. \subsection{Usage}{ \if{html}{\out{
}}\preformatted{ResamplingRepeatedSpCVCoords$folds(iters)}\if{html}{\out{
}} } +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{iters}}{\code{integer()}\cr +Iteration number.} +} +\if{html}{\out{
}} +} } \if{html}{\out{
}} \if{html}{\out{}} \subsection{Method \code{repeats()}}{ +Translates iteration numbers to repetition number. \subsection{Usage}{ \if{html}{\out{
}}\preformatted{ResamplingRepeatedSpCVCoords$repeats(iters)}\if{html}{\out{
}} } +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{iters}}{\code{integer()}\cr +Iteration number.} +} +\if{html}{\out{
}} +} } \if{html}{\out{
}} \if{html}{\out{}} diff --git a/man/ResamplingRepeatedSpCVEnv.Rd b/man/ResamplingRepeatedSpCVEnv.Rd new file mode 100644 index 000000000..e736bb0e0 --- /dev/null +++ b/man/ResamplingRepeatedSpCVEnv.Rd @@ -0,0 +1,148 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ResamplingRepeatedSpCVEnv.R +\name{ResamplingRepeatedSpCVEnv} +\alias{ResamplingRepeatedSpCVEnv} +\title{Repeated Environmental Block Cross Validation Resampling} +\description{ +Environmental Block Cross Validation. This strategy uses k-means +clustering to specify blocks of similar environmental conditions. Only +numeric features can be used. The \code{features} used for building blocks can +be specified in the \code{param_set}. By default, all numeric features are used. +} +\examples{ +library(mlr3) +task = tsk("ecuador") + +# Instantiate Resampling +rrcv = rsmp("repeated-spcv-env") +rrcv$param_set$values = list(folds = 4, repeats = 2) +rrcv$instantiate(task) + +# Individual sets: +rrcv$train_set(1) +rrcv$test_set(1) +intersect(rrcv$train_set(1), rrcv$test_set(1)) + +# Internal storage: +rrcv$instance +} +\references{ +\cite{mlr3spatiotempcv}{valavi2018} +} +\section{Super class}{ +\code{\link[mlr3:Resampling]{mlr3::Resampling}} -> \code{ResamplingRepeatedSpCVEnv} +} +\section{Active bindings}{ +\if{html}{\out{
}} +\describe{ +\item{\code{iters}}{\code{integer(1)}\cr +Returns the number of resampling iterations, depending on the +values stored in the \code{param_set}.} +} +\if{html}{\out{
}} +} +\section{Methods}{ +\subsection{Public methods}{ +\itemize{ +\item \href{#method-new}{\code{ResamplingRepeatedSpCVEnv$new()}} +\item \href{#method-folds}{\code{ResamplingRepeatedSpCVEnv$folds()}} +\item \href{#method-repeats}{\code{ResamplingRepeatedSpCVEnv$repeats()}} +\item \href{#method-instantiate}{\code{ResamplingRepeatedSpCVEnv$instantiate()}} +\item \href{#method-clone}{\code{ResamplingRepeatedSpCVEnv$clone()}} +} +} +\if{html}{ +\out{
Inherited methods} +\itemize{ +\item \out{}\href{../../mlr3/html/Resampling.html#method-format}{\code{mlr3::Resampling$format()}}\out{} +\item \out{}\href{../../mlr3/html/Resampling.html#method-print}{\code{mlr3::Resampling$print()}}\out{} +\item \out{}\href{../../mlr3/html/Resampling.html#method-test_set}{\code{mlr3::Resampling$test_set()}}\out{} +\item \out{}\href{../../mlr3/html/Resampling.html#method-train_set}{\code{mlr3::Resampling$train_set()}}\out{} +} +\out{
} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\subsection{Method \code{new()}}{ +Create an "coordinate-based" repeated resampling instance. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{ResamplingRepeatedSpCVEnv$new(id = "repeated-spcv-env")}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{id}}{\code{character(1)}\cr +Identifier for the resampling strategy.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\subsection{Method \code{folds()}}{ +Translates iteration numbers to fold number. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{ResamplingRepeatedSpCVEnv$folds(iters)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{iters}}{\code{integer()}\cr +Iteration number.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\subsection{Method \code{repeats()}}{ +Translates iteration numbers to repetition number. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{ResamplingRepeatedSpCVEnv$repeats(iters)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{iters}}{\code{integer()}\cr +Iteration number.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\subsection{Method \code{instantiate()}}{ +Materializes fixed training and test splits for a given task. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{ResamplingRepeatedSpCVEnv$instantiate(task)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{task}}{\link{Task}\cr +A task to instantiate.} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} +\if{html}{\out{}} +\subsection{Method \code{clone()}}{ +The objects of this class are cloneable with this method. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{ResamplingRepeatedSpCVEnv$clone(deep = FALSE)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{deep}}{Whether to make a deep clone.} +} +\if{html}{\out{
}} +} +} +} diff --git a/man/ResamplingSpCVEnv.Rd b/man/ResamplingSpCVEnv.Rd index fdd0b5969..42093d7ca 100644 --- a/man/ResamplingSpCVEnv.Rd +++ b/man/ResamplingSpCVEnv.Rd @@ -4,10 +4,10 @@ \alias{ResamplingSpCVEnv} \title{Environmental Block Cross Validation Resampling} \description{ -Environmental Block Cross Validation. This strategy uses k-means -clustering to specify blocks of smilar environmental conditions. Only numeric -features can be used. The \code{features} used for building blocks can be -specified in the \code{param_set}. By default, all numeric features are used. +Environmental Block Cross Validation. This strategy uses k-means clustering +to specify blocks of similar environmental conditions. Only numeric features +can be used. The \code{features} used for building blocks can be specified in the +\code{param_set}. By default, all numeric features are used. } \examples{ library(mlr3) @@ -27,10 +27,7 @@ intersect(rcv$train_set(1), rcv$test_set(1)) rcv$instance } \references{ -Valavi R, Elith J, Lahoz-Monfort JJ, Guillera-Arroita G. blockCV: -An r package for generating spatially or environmentally separated folds for -k-fold cross-validation of species distribution models. Methods Ecol Evol. -2019; 10:225–232. https://doi.org/10.1111/2041-210X.13107 +\cite{mlr3spatiotempcv}{valavi2018} } \section{Super class}{ \code{\link[mlr3:Resampling]{mlr3::Resampling}} -> \code{ResamplingSpCVEnv} diff --git a/man/autoplot_spatial_resampling.Rd b/man/autoplot_spatial_resampling.Rd index cfe6a467a..2b87d62fd 100644 --- a/man/autoplot_spatial_resampling.Rd +++ b/man/autoplot_spatial_resampling.Rd @@ -6,6 +6,7 @@ \alias{autoplot.ResamplingSpCVBuffer} \alias{autoplot.ResamplingSpCVCoords} \alias{autoplot.ResamplingRepeatedSpCVCoords} +\alias{autoplot.ResamplingRepeatedSpCVEnv} \title{Visualization of spatial resampling.} \usage{ \method{autoplot}{ResamplingSpCVBlock}( @@ -58,6 +59,17 @@ test_color = "#E18727", ... ) + +\method{autoplot}{ResamplingRepeatedSpCVEnv}( + object, + task, + fold_id = NULL, + repeats_id = 1, + grid = TRUE, + train_color = "#0072B5", + test_color = "#E18727", + ... +) } \arguments{ \item{object}{\link{Resampling}\cr @@ -169,4 +181,16 @@ autoplot(resampling, task, 1) autoplot(resampling, task, fold_id = 2, repeats_id = 2) autoplot(resampling, task, c(1, 2, 3, 4)) } +##### +# RepeatedSpCVEnv +##### +\donttest{ +task = tsk("ecuador") +resampling = rsmp("repeated-spcv-env", folds = 10, repeats = 2) +resampling$instantiate(task) +autoplot(resampling, task) +autoplot(resampling, task, 1) +autoplot(resampling, task, fold_id = 2, repeats_id = 2) +autoplot(resampling, task, c(1, 2, 3, 4)) +} } diff --git a/tests/figs/autoplot/repeatedspcvcoords-fold-1-4.svg b/tests/figs/autoplot/repeatedspcvcoords-fold-1-4.svg index b7b766048..89b07d28b 100644 --- a/tests/figs/autoplot/repeatedspcvcoords-fold-1-4.svg +++ b/tests/figs/autoplot/repeatedspcvcoords-fold-1-4.svg @@ -3987,9 +3987,9 @@ Fold 4 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/repeatedspcvcoords-fold-1.svg b/tests/figs/autoplot/repeatedspcvcoords-fold-1.svg index a6076924e..1dd262bf7 100644 --- a/tests/figs/autoplot/repeatedspcvcoords-fold-1.svg +++ b/tests/figs/autoplot/repeatedspcvcoords-fold-1.svg @@ -1014,9 +1014,9 @@ Fold 1 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/repeatedspcvenv-all-test-sets.svg b/tests/figs/autoplot/repeatedspcvenv-all-test-sets.svg new file mode 100644 index 000000000..ee82b20d1 --- /dev/null +++ b/tests/figs/autoplot/repeatedspcvenv-all-test-sets.svg @@ -0,0 +1,882 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +4 +° +S +3.995 +° +S +3.99 +° +S +3.985 +° +S +3.98 +° +S +3.975 +° +S +3.97 +° +S +3.965 +° +S + + + + + + + + + + + + + + + +79.085 +° +W +79.08 +° +W +79.075 +° +W +79.07 +° +W +79.065 +° +W +79.06 +° +W +79.055 +° +W + +Partition #, Rep 1 + + + + + + + + + + + + + + + + + + + + +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +RepeatedSpCVEnv all test sets + diff --git a/tests/figs/autoplot/repeatedspcvenv-fold-1-4.svg b/tests/figs/autoplot/repeatedspcvenv-fold-1-4.svg new file mode 100644 index 000000000..5c4fc3722 --- /dev/null +++ b/tests/figs/autoplot/repeatedspcvenv-fold-1-4.svg @@ -0,0 +1,3432 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +4 +° +S +3.995 +° +S +3.99 +° +S +3.985 +° +S +3.98 +° +S +3.975 +° +S +3.97 +° +S +3.965 +° +S + + + + + + + + + + + + + + + +79.085 +° +W +79.08 +° +W +79.075 +° +W +79.07 +° +W +79.065 +° +W +79.06 +° +W +79.055 +° +W +Fold 1 +Rep 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +4 +° +S +3.995 +° +S +3.99 +° +S +3.985 +° +S +3.98 +° +S +3.975 +° +S +3.97 +° +S +3.965 +° +S + + + + + + + + + + + + + + + +79.085 +° +W +79.08 +° +W +79.075 +° +W +79.07 +° +W +79.065 +° +W +79.06 +° +W +79.055 +° +W +Fold 2 +Rep 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +4 +° +S +3.995 +° +S +3.99 +° +S +3.985 +° +S +3.98 +° +S +3.975 +° +S +3.97 +° +S +3.965 +° +S + + + + + + + + + + + + + + + +79.085 +° +W +79.08 +° +W +79.075 +° +W +79.07 +° +W +79.065 +° +W +79.06 +° +W +79.055 +° +W +Fold 3 +Rep 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +4 +° +S +3.995 +° +S +3.99 +° +S +3.985 +° +S +3.98 +° +S +3.975 +° +S +3.97 +° +S +3.965 +° +S + + + + + + + + + + + + + + + +79.085 +° +W +79.08 +° +W +79.075 +° +W +79.07 +° +W +79.065 +° +W +79.06 +° +W +79.055 +° +W +Fold 4 +Rep 1 + + + + + +Train +Test + diff --git a/tests/figs/autoplot/repeatedspcvenv-fold-1.svg b/tests/figs/autoplot/repeatedspcvenv-fold-1.svg new file mode 100644 index 000000000..0a71b31c1 --- /dev/null +++ b/tests/figs/autoplot/repeatedspcvenv-fold-1.svg @@ -0,0 +1,882 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +4 +° +S +3.995 +° +S +3.99 +° +S +3.985 +° +S +3.98 +° +S +3.975 +° +S +3.97 +° +S +3.965 +° +S + + + + + + + + + + + + + + + +79.085 +° +W +79.08 +° +W +79.075 +° +W +79.07 +° +W +79.065 +° +W +79.06 +° +W +79.055 +° +W +Fold 1 +Rep 1 + + + + + +Train +Test + diff --git a/tests/figs/autoplot/spcvblock-fold-1-4.svg b/tests/figs/autoplot/spcvblock-fold-1-4.svg index 45acdc64b..6bd393d80 100644 --- a/tests/figs/autoplot/spcvblock-fold-1-4.svg +++ b/tests/figs/autoplot/spcvblock-fold-1-4.svg @@ -3423,9 +3423,9 @@ Fold 4 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/spcvblock-fold-1.svg b/tests/figs/autoplot/spcvblock-fold-1.svg index f774741b3..31d1ef218 100644 --- a/tests/figs/autoplot/spcvblock-fold-1.svg +++ b/tests/figs/autoplot/spcvblock-fold-1.svg @@ -873,9 +873,9 @@ Fold 1 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/spcvbuffer-fold-1-4.svg b/tests/figs/autoplot/spcvbuffer-fold-1-4.svg index 5b55ffb4f..e34c90469 100644 --- a/tests/figs/autoplot/spcvbuffer-fold-1-4.svg +++ b/tests/figs/autoplot/spcvbuffer-fold-1-4.svg @@ -3423,9 +3423,9 @@ Fold 4 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/spcvbuffer-fold-1.svg b/tests/figs/autoplot/spcvbuffer-fold-1.svg index 84b76ff3b..fd86a9fcb 100644 --- a/tests/figs/autoplot/spcvbuffer-fold-1.svg +++ b/tests/figs/autoplot/spcvbuffer-fold-1.svg @@ -873,9 +873,9 @@ Fold 1 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/spcvcoords-fold-1-4.svg b/tests/figs/autoplot/spcvcoords-fold-1-4.svg index 4233bf79c..09b589043 100644 --- a/tests/figs/autoplot/spcvcoords-fold-1-4.svg +++ b/tests/figs/autoplot/spcvcoords-fold-1-4.svg @@ -3423,9 +3423,9 @@ Fold 4 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/spcvcoords-fold-1.svg b/tests/figs/autoplot/spcvcoords-fold-1.svg index 4aefa3868..d94832368 100644 --- a/tests/figs/autoplot/spcvcoords-fold-1.svg +++ b/tests/figs/autoplot/spcvcoords-fold-1.svg @@ -873,9 +873,9 @@ Fold 1 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/spcvenv-fold-1-4.svg b/tests/figs/autoplot/spcvenv-fold-1-4.svg index d5fd4def5..5c4fc3722 100644 --- a/tests/figs/autoplot/spcvenv-fold-1-4.svg +++ b/tests/figs/autoplot/spcvenv-fold-1-4.svg @@ -3423,9 +3423,9 @@ Fold 4 Rep 1 - + - + Train Test diff --git a/tests/figs/autoplot/spcvenv-fold-1.svg b/tests/figs/autoplot/spcvenv-fold-1.svg index 5f9e94397..0a71b31c1 100644 --- a/tests/figs/autoplot/spcvenv-fold-1.svg +++ b/tests/figs/autoplot/spcvenv-fold-1.svg @@ -873,9 +873,9 @@ Fold 1 Rep 1 - + - + Train Test diff --git a/tests/testthat/test-ResamplingRepeatedSpCVEnv.R b/tests/testthat/test-ResamplingRepeatedSpCVEnv.R new file mode 100644 index 000000000..3140ed9db --- /dev/null +++ b/tests/testthat/test-ResamplingRepeatedSpCVEnv.R @@ -0,0 +1,15 @@ +test_that("folds can be printed", { + rsp = rsmp("repeated-spcv-env") + rsp$param_set$values = list(folds = 3, repeats = 5) + rsp$instantiate(task) + + expect_equal(rsp$folds(4:8), c(4, 5, 1, 2, 3)) +}) + +test_that("reps can be printed", { + rsp = rsmp("repeated-spcv-env") + rsp$param_set$values = list(folds = 3, repeats = 5) + rsp$instantiate(task) + + expect_equal(rsp$repeats(4:8), c(2, 2, 2, 3, 3)) +}) diff --git a/tests/testthat/test-autoplot.R b/tests/testthat/test-autoplot.R index 9e21143d0..0131a416f 100644 --- a/tests/testthat/test-autoplot.R +++ b/tests/testthat/test-autoplot.R @@ -111,3 +111,26 @@ test_that("autoplot works for SpCVEnv", { vdiffr::expect_doppelganger("SpCVEnv - Fold 1", env2) vdiffr::expect_doppelganger("SpCVEnv - Fold 1-4", env3) }) + +test_that("autoplot works for RepeatedSpCVEnv", { + + # RepeatedSpCVEnv ------------------------------------------------------------ + + set.seed(42) + + task = tsk("ecuador") + rresa_env = rsmp("repeated-spcv-env", folds = 10, repeats = 2) + rresa_env$instantiate(task) + renv1 = autoplot(rresa_env, task) + renv2 = autoplot(rresa_env, task, 1) + renv3 = autoplot(rresa_env, task, c(1, 2, 3, 4)) + + expect_true(is.ggplot(renv1)) + expect_true(is.ggplot(renv2)) + expect_true(is.ggplot(renv3)) + + skip_on_ci() + vdiffr::expect_doppelganger("RepeatedSpCVEnv all test sets", renv1) + vdiffr::expect_doppelganger("RepeatedSpCVEnv - Fold 1", renv2) + vdiffr::expect_doppelganger("RepeatedSpCVEnv - Fold 1-4", renv3) +}) diff --git a/tests/testthat/test_mlr_spcv_generic.R b/tests/testthat/test_mlr_spcv_generic.R index 10b285075..8cd224ba1 100644 --- a/tests/testthat/test_mlr_spcv_generic.R +++ b/tests/testthat/test_mlr_spcv_generic.R @@ -18,6 +18,8 @@ test_that("stratification throws errors", { ) spcv_rsp$`spcv-buffer` = NULL spcv_rsp$`repeated-spcv-coords` = NULL + spcv_rsp$`repeated-spcv-env` = NULL + for (i in spcv_rsp) { i$param_set$values = list(folds = 5, stratify = TRUE) expect_error(i$instantiate(task)) @@ -28,9 +30,9 @@ test_that("grouping throws errors when 'groups' is set", { spcv_rsp = mlr_resamplings$mget( as.data.table(mlr_resamplings)[map_lgl(key, grepl, pattern = "spcv"), key] ) - spcv_rsp_no_buffer = spcv_rsp - spcv_rsp_no_buffer$`spcv-buffer` = NULL spcv_rsp$`repeated-spcv-coords` = NULL + spcv_rsp$`repeated-spcv-env` = NULL + for (i in spcv_rsp) { expect_error(i$instantiate(task_grp), "Grouping is not supported for spatial resampling methods") @@ -43,6 +45,7 @@ test_that("grouping throws errors when 'groups' and 'stratify' is set", { ) spcv_rsp$`spcv-buffer` = NULL spcv_rsp$`repeated-spcv-coords` = NULL + spcv_rsp$`repeated-spcv-env` = NULL for (i in spcv_rsp) { i$param_set$values = list(folds = 5, stratify = TRUE) diff --git a/tests/testthat/test_spcv_generic.R b/tests/testthat/test_spcv_generic.R index 6e86d7a43..fff98d989 100644 --- a/tests/testthat/test_spcv_generic.R +++ b/tests/testthat/test_spcv_generic.R @@ -20,6 +20,7 @@ test_that("stratification throws errors", { spcv_rsp_no_buffer = spcv_rsp spcv_rsp_no_buffer$`spcv-buffer` = NULL spcv_rsp_no_buffer$`repeated-spcv-coords` = NULL + spcv_rsp_no_buffer$`repeated-spcv-env` = NULL for (i in spcv_rsp_no_buffer) { i$param_set$values = list(folds = 5, stratify = TRUE) @@ -33,8 +34,9 @@ test_that("grouping throws errors when 'groups' is set", { spcv_rsp = mlr_resamplings$mget( as.data.table(mlr_resamplings)[map_lgl(key, grepl, pattern = "spcv"), key] ) - spcv_rsp_no_buffer = spcv_rsp - spcv_rsp_no_buffer$`spcv-buffer` = NULL + spcv_rsp$`spcv-buffer` = NULL + spcv_rsp$`repeated-spcv-coords` = NULL + spcv_rsp$`repeated-spcv-env` = NULL for (i in spcv_rsp) { expect_error(i$instantiate(task_grp), @@ -46,9 +48,9 @@ test_that("grouping throws errors when 'groups' and 'stratify' is set", { spcv_rsp = mlr_resamplings$mget( as.data.table(mlr_resamplings)[map_lgl(key, grepl, pattern = "spcv"), key] ) - spcv_rsp = spcv_rsp spcv_rsp$`spcv-buffer` = NULL spcv_rsp$`repeated-spcv-coords` = NULL + spcv_rsp$`repeated-spcv-env` = NULL for (i in spcv_rsp) { i$param_set$values = list(folds = 5, stratify = TRUE) @@ -69,6 +71,7 @@ test_that("train and test set getter functions are working", { spcv_rsp$`spcv-buffer` = NULL spcv_rsp$`repeated-spcv-coords` = NULL + spcv_rsp$`repeated-spcv-env` = NULL for (i in spcv_rsp) { i$instantiate(task)