Skip to content

Commit 56e2398

Browse files
committed
change factors to characters in tree data from randomForest, so we stop getting this warning: Factor split var contains implicit NA, consider using forcats::fct_explicit_na.
1 parent d6b345c commit 56e2398

File tree

3 files changed

+4
-0
lines changed

3 files changed

+4
-0
lines changed

R/measure_importance.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ measure_importance.randomForest <- function(forest, mean_sample = "top_trees", m
148148
}
149149
forest_table <-
150150
lapply(1:forest$ntree, function(i) randomForest::getTree(forest, k = i, labelVar = T) %>%
151+
mutate_if(is.factor, as.character) %>%
151152
calculate_tree_depth() %>% cbind(tree = i)) %>% rbindlist()
152153
min_depth_frame <- dplyr::group_by(forest_table, tree, `split var`) %>%
153154
dplyr::summarize(min(depth))

R/min_depth_distribution.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ min_depth_distribution.randomForest <- function(forest){
5454
tree <- NULL; `split var` <- NULL; depth <- NULL
5555
forest_table <-
5656
lapply(1:forest$ntree, function(i) randomForest::getTree(forest, k = i, labelVar = T) %>%
57+
mutate_if(is.factor, as.character) %>%
5758
calculate_tree_depth() %>% cbind(tree = i)) %>% rbindlist()
5859
min_depth_frame <- dplyr::group_by(forest_table, tree, `split var`) %>%
5960
dplyr::summarize(min(depth))

R/min_depth_interactions.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ min_depth_interactions_values <- function(forest, vars){
6060
`.` <- NULL; .SD <- NULL; tree <- NULL; `split var` <- NULL
6161
interactions_frame <-
6262
lapply(1:forest$ntree, function(i) randomForest::getTree(forest, k = i, labelVar = T) %>%
63+
mutate_if(is.factor, as.character) %>%
6364
calculate_tree_depth() %>% cbind(., tree = i, number = 1:nrow(.))) %>%
6465
data.table::rbindlist() %>% as.data.frame()
6566
interactions_frame[vars] <- as.numeric(NA)
@@ -164,6 +165,7 @@ min_depth_interactions.randomForest <- function(forest, vars = important_variabl
164165
interactions_frame$interaction <- paste(interactions_frame$root_variable, interactions_frame$variable, sep = ":")
165166
forest_table <-
166167
lapply(1:forest$ntree, function(i) randomForest::getTree(forest, k = i, labelVar = T) %>%
168+
mutate_if(is.factor, as.character) %>%
167169
calculate_tree_depth() %>% cbind(tree = i)) %>% rbindlist()
168170
min_depth_frame <- dplyr::group_by(forest_table, tree, `split var`) %>%
169171
dplyr::summarize(min(depth))

0 commit comments

Comments
 (0)