Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAINT] Deal with global variable notes via globalVariables() #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Suggests:
MASS (>= 7.3.47),
testthat
VignetteBuilder: knitr
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
URL: https://github.com/ModelOriented/randomForestExplainer, https://modeloriented.github.io/randomForestExplainer/
Config/testthat/edition: 3
Config/Needs/website: ModelOriented/DrWhyTemplate
1 change: 0 additions & 1 deletion R/measure_importance.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ plot_multi_way_importance <- function(importance_frame, x_measure = "mean_min_de
y_measure = "times_a_root", size_measure = NULL,
min_no_of_trees = 0, no_of_labels = 10,
main = "Multi-way importance plot"){
variable <- NULL
if(any(c("randomForest", "ranger") %in% class(importance_frame))){
importance_frame <- measure_importance(importance_frame)
}
Expand Down
5 changes: 0 additions & 5 deletions R/min_depth_distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ min_depth_distribution <- function(forest){
#' @importFrom data.table rbindlist
#' @export
min_depth_distribution.randomForest <- function(forest){
tree <- NULL; `split var` <- NULL; depth <- NULL
forest_table <- lapply(
1:forest$ntree,
function(i)
Expand All @@ -85,7 +84,6 @@ min_depth_distribution.randomForest <- function(forest){
#' @importFrom data.table rbindlist
#' @export
min_depth_distribution.ranger <- function(forest){
tree <- NULL; splitvarName <- NULL; depth <- NULL
forest_table <- lapply(
1:forest$num.trees,
function(i)
Expand All @@ -103,7 +101,6 @@ min_depth_distribution.ranger <- function(forest){

# Count the trees in which each variable had a given minimal depth
min_depth_count <- function(min_depth_frame){
tree <- NULL; minimal_depth <- NULL; variable <- NULL
mean_tree_depth <- dplyr::group_by(min_depth_frame, tree) %>%
dplyr::summarize(depth = max(minimal_depth) + 1) %>% as.data.frame()
mean_tree_depth <- mean(mean_tree_depth$depth)
Expand All @@ -122,7 +119,6 @@ min_depth_count <- function(min_depth_frame){

# Get a data frame with means of minimal depth calculated using sample = c("all_trees", "top_trees", "relevant_trees")
get_min_depth_means <- function(min_depth_frame, min_depth_count_list, mean_sample){
.SD <- NULL; variable <- NULL
if(mean_sample == "all_trees"){
min_depth_count_list[[1]][is.na(min_depth_count_list[[1]]$minimal_depth), "minimal_depth"] <- min_depth_count_list[[3]]
min_depth_means <-
Expand Down Expand Up @@ -166,7 +162,6 @@ get_min_depth_means <- function(min_depth_frame, min_depth_count_list, mean_samp
plot_min_depth_distribution <- function(min_depth_frame, k = 10, min_no_of_trees = 0,
mean_sample = "top_trees", mean_scale = FALSE, mean_round = 2,
main = "Distribution of minimal depth and its mean"){
minimal_depth <- NULL; mean_minimal_depth_label <- NULL; mean_minimal_depth <- NULL
if(any(c("randomForest", "ranger") %in% class(min_depth_frame))){
min_depth_frame <- min_depth_distribution(min_depth_frame)
}
Expand Down
7 changes: 0 additions & 7 deletions R/min_depth_interactions.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Calculate conditional depth in a tree with respect to all variables from vector vars
# randomForest
conditional_depth <- function(frame, vars){
`.SD` <- NULL; depth <- NULL; `split var` <- NULL
index <- data.table::as.data.table(frame)[, .SD[which.min(depth), "number"], by = `split var`]
index <- index[!is.na(index$`split var`), ]
if(any(index$`split var` %in% vars)){
Expand Down Expand Up @@ -29,7 +28,6 @@ conditional_depth <- function(frame, vars){
# Calculate conditional depth in a tree with respect to all variables from vector vars
# ranger
conditional_depth_ranger <- function(frame, vars){
`.SD` <- NULL; depth <- NULL; splitvarName <- NULL
index <- data.table::as.data.table(frame)[, .SD[which.min(depth), "number"], by = splitvarName]
index <- index[!is.na(index$splitvarName), ]
if(any(index$splitvarName %in% vars)){
Expand Down Expand Up @@ -57,7 +55,6 @@ conditional_depth_ranger <- function(frame, vars){
# Get a data frame with values of minimal depth conditional on selected variables for the whole forest
# randomForest
min_depth_interactions_values <- function(forest, vars){
`.` <- NULL; .SD <- NULL; tree <- NULL; `split var` <- NULL
interactions_frame <- lapply(
1:forest$ntree,
function(i)
Expand Down Expand Up @@ -93,7 +90,6 @@ min_depth_interactions_values <- function(forest, vars){
# Get a data frame with values of minimal depth conditional on selected variables for the whole forest
# ranger
min_depth_interactions_values_ranger <- function(forest, vars){
`.` <- NULL; .SD <- NULL; tree <- NULL; splitvarName <- NULL
interactions_frame <- lapply(
1:forest$num.trees,
function(i)
Expand Down Expand Up @@ -149,7 +145,6 @@ min_depth_interactions <- function(forest, vars = important_variables(measure_im
#' @export
min_depth_interactions.randomForest <- function(forest, vars = important_variables(measure_importance(forest)),
mean_sample = "top_trees", uncond_mean_sample = mean_sample){
variable <- NULL; `.` <- NULL; tree <- NULL; `split var` <- NULL; depth <- NULL
min_depth_interactions_frame <- min_depth_interactions_values(forest, vars)
mean_tree_depth <- min_depth_interactions_frame[[2]]
min_depth_interactions_frame <- min_depth_interactions_frame[[1]]
Expand Down Expand Up @@ -217,7 +212,6 @@ min_depth_interactions.randomForest <- function(forest, vars = important_variabl
#' @export
min_depth_interactions.ranger <- function(forest, vars = important_variables(measure_importance(forest)),
mean_sample = "top_trees", uncond_mean_sample = mean_sample){
variable <- NULL; `.` <- NULL; tree <- NULL; splitvarName <- NULL; depth <- NULL
min_depth_interactions_frame <- min_depth_interactions_values_ranger(forest, vars)
mean_tree_depth <- min_depth_interactions_frame[[2]]
min_depth_interactions_frame <- min_depth_interactions_frame[[1]]
Expand Down Expand Up @@ -287,7 +281,6 @@ min_depth_interactions.ranger <- function(forest, vars = important_variables(mea
plot_min_depth_interactions <- function(interactions_frame, k = 30,
main = paste0("Mean minimal depth for ",
paste0(k, " most frequent interactions"))){
mean_min_depth <- NULL; occurrences <- NULL; uncond_mean_min_depth <- NULL
if(any(c("randomForest", "ranger") %in% class(interactions_frame))){
interactions_frame <- min_depth_interactions(interactions_frame)
}
Expand Down
22 changes: 21 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,25 @@ max_na <- function(x) {
max(x, na.rm = TRUE)
}

utils::globalVariables(c("prediction", "variable"))
# Deal with the global variable note
utils::globalVariables(
c(
".",
".SD",
"depth",
"mean_min_depth",
"mean_minimal_depth",
"mean_minimal_depth_label",
"minimal_depth",
"occurrences",
"prediction",
"split var",
"splitvarName",
"tree",
"uncond_mean_min_depth",
"variable"
)
)



Loading