Skip to content

Commit

Permalink
use factor levels of data not RF
Browse files Browse the repository at this point in the history
  • Loading branch information
mnwright committed Jun 7, 2024
1 parent c88612c commit 17048e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/forde.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ forde <- function(
classes <- sapply(x, class)
x <- suppressWarnings(prep_x(x))
factor_cols <- sapply(x, is.factor)
lvls <- arf$forest$covariate.levels[factor_cols]
if (!is.null(lvls)) {
if (any(factor_cols)) {
lvls <- lapply(x[, factor_cols, drop = FALSE], levels)
names(lvls) <- colnames_x[factor_cols]
lvl_df <- rbindlist(lapply(seq_along(lvls), function(j) {
melt(as.data.table(lvls[j]), measure.vars = names(lvls)[j],
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-return_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ test_that("FORGE returns same column types", {
expect_equal(classes, classes_synth)
})

test_that("FORGE returns factors with same levels (and order of levels)", {
arf <- adversarial_rf(iris, num_trees = 2, verbose = FALSE, parallel = FALSE)
psi <- forde(arf, iris, parallel = FALSE)
x_synth <- forge(psi, n_synth = 10, parallel = FALSE)
expect_equal(levels(x_synth$Species), levels(iris$Species))
})

test_that("EXPCT returns factors with same levels (and order of levels)", {
arf <- adversarial_rf(iris, num_trees = 2, verbose = FALSE, parallel = FALSE)
psi <- forde(arf, iris, parallel = FALSE)
x_synth <- expct(psi, parallel = FALSE)
expect_equal(levels(x_synth$Species), levels(iris$Species))
})

# test_that("MAP returns proper column types", {
# n <- 50
# dat <- data.frame(numeric = rnorm(n),
Expand Down

0 comments on commit 17048e0

Please sign in to comment.