Skip to content

Commit

Permalink
Merge pull request #39 from bips-hb/verbose_warnings
Browse files Browse the repository at this point in the history
Let verbose=TRUE also silence the integer conversion warnings
  • Loading branch information
mnwright authored Feb 17, 2025
2 parents 5672323 + 4eb7ec8 commit 18303d3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions R/adversarial_rf.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' @param early_stop Terminate loop if performance fails to improve from one
#' round to the next?
#' @param prune Impose \code{min_node_size} by pruning?
#' @param verbose Print discriminator accuracy after each round?
#' @param verbose Print discriminator accuracy after each round? Will also show additional warnings.
#' @param parallel Compute in parallel? Must register backend beforehand, e.g.
#' via \code{doParallel} or \code{doFuture}; see examples.
#' @param ... Extra parameters to be passed to \code{ranger}.
Expand Down Expand Up @@ -118,7 +118,7 @@ adversarial_rf <- function(
i <- b <- cnt <- obs <- tree <- leaf <- N <- . <- NULL

# Prep data
x_real <- prep_x(x)
x_real <- prep_x(x, verbose)
n <- nrow(x_real)
d <- ncol(x_real)
factor_cols <- sapply(x_real, is.factor)
Expand Down
11 changes: 8 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ which.max.random <- function(x) {
#' This function prepares input data for ARFs.
#'
#' @param x Input data.frame.
#' @param verbose Show warning if recoding integers?
#' @keywords internal

prep_x <- function(x) {
prep_x <- function(x, verbose = TRUE) {
# Reclass all non-numeric features as factors
x <- as.data.frame(x)
idx_char <- sapply(x, is.character)
Expand All @@ -102,16 +103,20 @@ prep_x <- function(x) {
idx_integer[j] & length(unique(x[[j]])) > 5
})
if (any(to_numeric)) {
warning('Recoding integers with more than 5 unique values as numeric. ',
if (verbose) {
warning('Recoding integers with more than 5 unique values as numeric. ',
'To override this behavior, explicitly code these variables as factors.')
}
x[, to_numeric] <- lapply(x[, to_numeric, drop = FALSE], as.numeric)
}
to_factor <- sapply(seq_len(ncol(x)), function(j) {
idx_integer[j] & length(unique(x[[j]])) < 6
})
if (any(to_factor)) {
warning('Recoding integers with fewer than 6 unique values as ordered factors. ',
if (verbose) {
warning('Recoding integers with fewer than 6 unique values as ordered factors. ',
'To override this behavior, explicitly code these variables as numeric.')
}
x[, to_factor] <- lapply(which(to_factor), function(j) {
lvls <- sort(unique(x[[j]]))
factor(x[[j]], levels = lvls, ordered = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion man/adversarial_rf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/prep_x.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-return_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ test_that("FORGE returns correct column types", {
factor = factor(sample(letters[1:5], n, replace = TRUE)),
logical = (sample(0:1, n, replace = TRUE) == 1))

expect_warning(arf <- adversarial_rf(dat, num_trees = 2, verbose = FALSE, parallel = FALSE))
arf <- adversarial_rf(dat, num_trees = 2, verbose = FALSE, parallel = FALSE)
psi <- forde(arf, dat, parallel = FALSE)

# with round = TRUE
Expand Down

0 comments on commit 18303d3

Please sign in to comment.