Skip to content

Commit

Permalink
Retain original factor levels
Browse files Browse the repository at this point in the history
  • Loading branch information
rpln committed Jun 22, 2020
1 parent 6580081 commit 20e758d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions R/nearmiss.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ bake.step_nearmiss <- function(object, new_data, ...) {
with_seed(
seed = object$seed,
code = {
original_levels <- levels(new_data[[object$column]])
new_data <- nearmiss(new_data, object$column,
k = object$neighbors,
under_ratio = object$under_ratio)
new_data[[object$column]] <- factor(new_data[[object$column]], levels = original_levels)
}
)

Expand Down
2 changes: 2 additions & 0 deletions R/rose.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,13 @@ bake.step_rose <- function(object, new_data, ...) {
with_seed(
seed = object$seed,
code = {
original_levels <- levels(new_data[[object$column]])
new_data <- ROSE(string2formula(object$column), new_data,
N = majority_size * object$over_ratio,
p = object$minority_prop,
hmult.majo = object$majority_smoothness,
hmult.mino = object$minority_smoothness)$data
new_data[[object$column]] <- factor(new_data[[object$column]], levels = original_levels)
}
)

Expand Down
7 changes: 4 additions & 3 deletions R/tomek.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ response_0_1 <- function(x) {
ifelse(x == names(sort(table(x)))[1], 1, 0)
}
# Turns 0-1 coded variable back into factor variable
response_0_1_to_org <- function(old, new) {
response_0_1_to_org <- function(old, new, levels) {
ref <- names(sort(table(old)))
names(ref) <- c("1", "0")
factor(unname(ref[as.character(new)]))
factor(unname(ref[as.character(new)]), levels = levels)
}

#' @importFrom tibble as_tibble tibble
Expand All @@ -165,6 +165,7 @@ bake.step_tomek <- function(object, new_data, ...) {
with_seed(
seed = object$seed,
code = {
original_levels <- levels(new_data[[object$column]])
tomek_data <- ubTomek(X = select(new_data, -!!object$column),
Y = response_0_1(new_data[[object$column]]),
verbose = FALSE)
Expand All @@ -174,7 +175,7 @@ bake.step_tomek <- function(object, new_data, ...) {
new_data0 <- mutate(
tomek_data$X,
!!object$column := response_0_1_to_org(new_data[[object$column]],
tomek_data$Y)
tomek_data$Y, levels = original_levels)
)

as_tibble(new_data0[names(new_data)])
Expand Down

0 comments on commit 20e758d

Please sign in to comment.