Skip to content

Commit

Permalink
Check error message parsing on osx
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Oct 4, 2024
1 parent 5752605 commit 43e003c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
34 changes: 14 additions & 20 deletions R/filter_collinear.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,20 +27,20 @@
#' (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
#' @returns A vector of names of columns that are below the correlation threshold
#' (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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -145,8 +145,8 @@ filter_collinear.data.frame <-
max_cells = max_cells
)
}


#' @rdname filter_collinear
#' @export
filter_collinear.matrix <- function(x,
Expand All @@ -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
Expand All @@ -189,15 +189,15 @@ filter_collinear.matrix <- function(x,
cutoff = cutoff,
verbose = verbose,
to_keep = to_keep
)
)
} else if (method == "vif_cor") {
vars_kept <- filter_vif_cor(
x,
cutoff = cutoff,
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."
Expand All @@ -216,9 +216,3 @@ filter_collinear.matrix <- function(x,
}



################################################################################
## legacy functions to be eventually removed
################################################################################


16 changes: 8 additions & 8 deletions tests/testthat/test_filter_collinear.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"))))
})

Expand All @@ -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"))))
})

0 comments on commit 43e003c

Please sign in to comment.