diff --git a/R/filter_collinear.R b/R/filter_collinear.R index 83fcfa7..ad3eec0 100644 --- a/R/filter_collinear.R +++ b/R/filter_collinear.R @@ -15,9 +15,9 @@ #' #' @param x A [`terra::SpatRaster`] object, a data.frame (with only numeric #' variables) -#' @param cutoff A numeric value used as a threshold to remove variables. +#' @param cutoff A numeric value used as a threshold to remove variables. #' For, "cor_caret" and "vif_cor", -#' it is the pair-wise absolute correlation cutoff, which defaults to 0.7. For +#' it is the pair-wise absolute correlation cutoff, which defaults to 0.7. For #' "vif_step", it is the variable inflation factor, which defaults to 10 #' @param verbose A boolean whether additional information should be provided #' on the screen @@ -27,7 +27,7 @@ #' (note that the function will return an error if the correlation among any of #' those variables is higher than the cutoff). #' @param method character. One of "cor_caret", "vif_cor" or "vif_step". -#' @param cor_type character. For methods that use correlation, which type +#' @param cor_type character. For methods that use correlation, which type #' of correlation: "pearson", "kendall", or "spearman". Defaults to "pearson" #' @param max_cells positive integer. The maximum number of cells to be used. If this is smaller than ncell(x), a regular sample of x is used #' @param ... additional arguments specific to a given object type @@ -35,12 +35,12 @@ #' (when \code{names = TRUE}), otherwise a vector of indices. Note that the indices #' are only for numeric variables (i.e. if factors are present, the indices do #' not take them into account). -#' @author for `cor_caret`: Original R code by Dong Li, modified by Max Kuhn +#' @author for `cor_caret`: Original R code by Dong Li, modified by Max Kuhn #' and Andrea Manica; for `vif_step` and `vif_cor`, original algorithm by Babak #' Naimi, rewritten by Andrea Manica for `tidysdm` -#' @references Naimi, B., Hamm, N.A.S., Groen, T.A., Skidmore, A.K., and -#' Toxopeus, A.G. 2014. Where is positional uncertainty a problem for species -#' distribution modelling?, Ecography 37 (2): 191-203. +#' @references Naimi, B., Hamm, N.A.S., Groen, T.A., Skidmore, A.K., and +#' Toxopeus, A.G. 2014. Where is positional uncertainty a problem for species +#' distribution modelling?, Ecography 37 (2): 191-203. #' @export filter_collinear <- function(x, @@ -94,7 +94,7 @@ filter_collinear.SpatRaster <- exhaustive = exhaustive) } else { x_matrix <- stats::na.omit(terra::as.matrix(x)) - + } # now dispatch to the matrix method filter_collinear( @@ -145,8 +145,8 @@ filter_collinear.data.frame <- max_cells = max_cells ) } - - + + #' @rdname filter_collinear #' @export filter_collinear.matrix <- function(x, @@ -161,12 +161,12 @@ filter_collinear.matrix <- function(x, if (ncol(x) <2) { stop("at least 2 numeric variables are needed") } - + # check that to_keep is valid if (!is.null(to_keep)){ if (!any(to_keep %in% colnames(x))) { stop("to_keep includes variables that are not present in x") - } + } } # sample rows if needed @@ -189,7 +189,7 @@ filter_collinear.matrix <- function(x, cutoff = cutoff, verbose = verbose, to_keep = to_keep - ) + ) } else if (method == "vif_cor") { vars_kept <- filter_vif_cor( x, @@ -197,7 +197,7 @@ filter_collinear.matrix <- function(x, verbose = verbose, to_keep = to_keep, cor_type = cor_type - ) + ) } else { stop ( "the selected method is not valid: only options 'cor_caret', 'vif_step' and 'vif_cor' are accepted." @@ -216,9 +216,3 @@ filter_collinear.matrix <- function(x, } - -################################################################################ -## legacy functions to be eventually removed -################################################################################ - - diff --git a/tests/testthat/test_filter_collinear.R b/tests/testthat/test_filter_collinear.R index 5471cf9..dbb7703 100644 --- a/tests/testthat/test_filter_collinear.R +++ b/tests/testthat/test_filter_collinear.R @@ -34,14 +34,14 @@ test_that("filter collinear variables with cor_caret", { # error for defautl object expect_error( filter_collinear("blah"), - "no method available for this object type" + "^no method available for this object type" ) - + # sample from data.frame set.seed(123) expect_true(!identical(filter_collinear(lacerta_thin, max_cells = 100), vars_to_keep)) - - + + # test method on SpatRaster climate_present <- terra::readRDS(system.file("extdata/lacerta_climate_present_10m.rds", package = "tidysdm" @@ -61,8 +61,8 @@ test_that("filter collinear variables with vif_step", { # we should remove two variables expect_true(all(!c("bio01", "bio18") %in% vars_to_keep)) # now keep them in - expect_true(all(c("bio01", "bio18") %in% - filter_collinear(lacerta_thin, method="vif_step", + expect_true(all(c("bio01", "bio18") %in% + filter_collinear(lacerta_thin, method="vif_step", to_keep = c("bio01", "bio18")))) }) @@ -75,7 +75,7 @@ test_that("filter collinear variables with vif_cor", { # we should remove two variables expect_true(all(!c("bio01", "bio18") %in% vars_to_keep)) # now keep them in - expect_true(all(c("bio01", "bio18") %in% - filter_collinear(lacerta_thin, method="vif_cor", + expect_true(all(c("bio01", "bio18") %in% + filter_collinear(lacerta_thin, method="vif_cor", to_keep = c("bio01", "bio18")))) })