Skip to content

Commit bc2e6c4

Browse files
Corrections for the devtools::check()
1 parent 5cba257 commit bc2e6c4

File tree

9 files changed

+30
-10
lines changed

9 files changed

+30
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Imports:
1818
GGally (>= 1.3.0),
1919
ggplot2 (>= 2.2.1),
2020
ggrepel (>= 0.6.5),
21+
MASS (>= 7.3.47),
2122
randomForest (>= 4.6.12),
2223
reshape2 (>= 1.4.2),
2324
rmarkdown (>= 1.5)

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ import(ggplot2)
1818
import(ggrepel)
1919
importFrom(data.table,frankv)
2020
importFrom(data.table,rbindlist)
21+
importFrom(stats,as.formula)
22+
importFrom(stats,predict)
23+
importFrom(stats,terms)

R/explain_forest.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#'
1717
#' @examples
1818
#' forest <- randomForest::randomForest(Species ~ ., data = iris, localImp = TRUE)
19-
#' explain_forest(forest, vars = names(iris), interactions = TRUE)
19+
#' explain_forest(forest, vars = names(iris))
2020
#'
2121
#' @export
2222
explain_forest <- function(forest, interactions = FALSE, data = NULL, vars = NULL, no_of_pred_plots = 3, pred_grid = 100,

R/measure_importance.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ measure_min_depth <- function(min_depth_frame, mean_sample){
88

99
# Calculate the number of nodes split on each variable for a data frame with the whole forest
1010
measure_no_of_nodes <- function(forest_table){
11+
`split var` <- NULL
1112
frame <- dplyr::group_by(forest_table, `split var`) %>% dplyr::summarize(n())
1213
colnames(frame) <- c("variable", "no_of_nodes")
1314
frame <- as.data.frame(frame[!is.na(frame$variable),])
@@ -45,6 +46,7 @@ measure_vimp <- function(forest, only_nonlocal = FALSE){
4546

4647
# Calculate the number of trees using each variable for splitting
4748
measure_no_of_trees <- function(min_depth_frame){
49+
variable <- NULL
4850
frame <- dplyr::group_by(min_depth_frame, variable) %>%
4951
dplyr::summarize(count = n()) %>% as.data.frame()
5052
colnames(frame)[2] <- "no_of_trees"
@@ -54,6 +56,7 @@ measure_no_of_trees <- function(min_depth_frame){
5456

5557
# Calculate the number of times each variable is split on the root node
5658
measure_times_a_root <- function(min_depth_frame){
59+
variable <- NULL
5760
frame <- min_depth_frame[min_depth_frame$minimal_depth == 0, ] %>%
5861
dplyr::group_by(variable) %>% dplyr::summarize(count = n()) %>% as.data.frame()
5962
colnames(frame)[2] <- "times_a_root"
@@ -65,7 +68,7 @@ measure_times_a_root <- function(min_depth_frame){
6568
measure_p_value <- function(importance_frame){
6669
total_no_of_nodes <- sum(importance_frame$no_of_nodes)
6770
p_value <- unlist(lapply(importance_frame$no_of_nodes,
68-
function(x) binom.test(x, total_no_of_nodes, 1/nrow(importance_frame),
71+
function(x) stats::binom.test(x, total_no_of_nodes, 1/nrow(importance_frame),
6972
alternative = "greater")$p.value))
7073
return(p_value)
7174
}
@@ -89,6 +92,7 @@ measure_p_value <- function(importance_frame){
8992
#'
9093
#' @export
9194
measure_importance <- function(forest, mean_sample = "top_trees", measures = NULL){
95+
tree <- NULL; `split var` <- NULL; depth <- NULL
9296
if(is.null(measures)){
9397
if(forest$type == "classification"){
9498
measures <- c("mean_min_depth", "no_of_nodes", "accuracy_decrease",
@@ -223,6 +227,7 @@ plot_multi_way_importance <- function(importance_frame, x_measure = "mean_min_de
223227
y_measure = "times_a_root", size_measure = NULL,
224228
min_no_of_trees = 0, no_of_labels = 10,
225229
main = "Multi-way importance plot"){
230+
variable <- NULL
226231
if("randomForest" %in% class(importance_frame)){
227232
importance_frame <- measure_importance(importance_frame)
228233
}
@@ -332,7 +337,7 @@ plot_importance_rankings <- function(importance_frame, measures =
332337
apply(importance_frame[, -c(1, 2)], 2,
333338
function(x) frankv(x, order = -1, ties.method = "dense")))
334339
plot <- ggpairs(rankings[, measures], lower = list(continuous = function(data, mapping){
335-
ggplot(data = data, mapping = mapping) + geom_point() + geom_smooth(method = loess)
340+
ggplot(data = data, mapping = mapping) + geom_point() + geom_smooth(method = "loess")
336341
}))+ theme_bw()
337342
if(!is.null(main)){
338343
plot <- plot + ggtitle(main)

R/min_depth_distribution.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ calculate_tree_depth <- function(frame){
3030
#'
3131
#' @export
3232
min_depth_distribution <- function(forest){
33+
tree <- NULL; `split var` <- NULL; depth <- NULL
3334
forest_table <-
3435
lapply(1:forest$ntree, function(i) randomForest::getTree(forest, k = i, labelVar = T) %>%
3536
calculate_tree_depth() %>% cbind(tree = i)) %>% rbindlist()
@@ -42,6 +43,7 @@ min_depth_distribution <- function(forest){
4243

4344
# Count the trees in which each variable had a given minimal depth
4445
min_depth_count <- function(min_depth_frame){
46+
tree <- NULL; minimal_depth <- NULL; variable <- NULL
4547
mean_tree_depth <- dplyr::group_by(min_depth_frame, tree) %>%
4648
dplyr::summarize(depth = max(minimal_depth) + 1) %>% as.data.frame()
4749
mean_tree_depth <- mean(mean_tree_depth$depth)
@@ -60,18 +62,19 @@ min_depth_count <- function(min_depth_frame){
6062

6163
# Get a data frame with means of minimal depth calculated using sample = c("all_trees", "top_trees", "relevant_trees")
6264
get_min_depth_means <- function(min_depth_frame, min_depth_count_list, mean_sample){
65+
.SD <- NULL; variable <- NULL
6366
if(mean_sample == "all_trees"){
6467
min_depth_count_list[[1]][is.na(min_depth_count_list[[1]]$minimal_depth), "minimal_depth"] <- min_depth_count_list[[3]]
6568
min_depth_means <-
66-
data.table::as.data.table(min_depth_count_list[[1]])[, weighted.mean(.SD[["minimal_depth"]], .SD[["count"]]),
69+
data.table::as.data.table(min_depth_count_list[[1]])[, stats::weighted.mean(.SD[["minimal_depth"]], .SD[["count"]]),
6770
by = variable] %>% as.data.frame()
6871
} else if(mean_sample == "top_trees"){
6972
min_depth_count_list[[1]][is.na(min_depth_count_list[[1]]$minimal_depth), "count"] <-
7073
min_depth_count_list[[1]][is.na(min_depth_count_list[[1]]$minimal_depth), "count"] -
7174
min(min_depth_count_list[[1]][is.na(min_depth_count_list[[1]]$minimal_depth), "count"])
7275
min_depth_count_list[[1]][is.na(min_depth_count_list[[1]]$minimal_depth), "minimal_depth"] <- min_depth_count_list[[3]]
7376
min_depth_means <-
74-
data.table::as.data.table(min_depth_count_list[[1]])[, weighted.mean(.SD[["minimal_depth"]], .SD[["count"]]),
77+
data.table::as.data.table(min_depth_count_list[[1]])[, stats::weighted.mean(.SD[["minimal_depth"]], .SD[["count"]]),
7578
by = variable] %>% as.data.frame()
7679
} else if(mean_sample == "relevant_trees"){
7780
min_depth_means <- stats::aggregate(minimal_depth ~ variable, data = min_depth_frame, mean)
@@ -103,6 +106,7 @@ get_min_depth_means <- function(min_depth_frame, min_depth_count_list, mean_samp
103106
plot_min_depth_distribution <- function(min_depth_frame, k = 10, min_no_of_trees = 0,
104107
mean_sample = "top_trees", mean_scale = FALSE, mean_round = 2,
105108
main = "Distribution of minimal depth and its mean"){
109+
minimal_depth <- NULL; mean_minimal_depth_label <- NULL; mean_minimal_depth <- NULL
106110
if("randomForest" %in% class(min_depth_frame)){
107111
min_depth_frame <- min_depth_distribution(min_depth_frame)
108112
}

R/min_depth_interactions.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Calculate conditional depth in a tree with respect to all variables from vector vars
22
conditional_depth <- function(frame, vars){
3+
`.SD` <- NULL; depth <- NULL; `split var` <- NULL
34
index <- data.table::as.data.table(frame)[, .SD[which.min(depth), "number"], by = `split var`]
45
index <- index[!is.na(index$`split var`), ]
56
if(any(index$`split var` %in% vars)){
@@ -26,6 +27,7 @@ conditional_depth <- function(frame, vars){
2627

2728
# Get a data frame with values of minimal depth conditional on selected variables for the whole forest
2829
min_depth_interactions_values <- function(forest, vars){
30+
`.` <- NULL; .SD <- NULL; tree <- NULL; `split var` <- NULL
2931
interactions_frame <-
3032
lapply(1:forest$ntree, function(i) randomForest::getTree(forest, k = i, labelVar = T) %>%
3133
calculate_tree_depth() %>% cbind(., tree = i, number = 1:nrow(.))) %>%
@@ -62,12 +64,13 @@ min_depth_interactions_values <- function(forest, vars){
6264
#' @importFrom data.table rbindlist
6365
#'
6466
#' @examples
65-
#' forest <- randomForest::randomForest(Species ~ ., data = iris)
67+
#' forest <- randomForest::randomForest(Species ~ ., data = iris, ntree = 200)
6668
#' min_depth_interactions(forest, names(iris))
6769
#'
6870
#' @export
6971
min_depth_interactions <- function(forest, vars = important_variables(measure_importance(forest)),
7072
mean_sample = "top_trees", uncond_mean_sample = mean_sample){
73+
variable <- NULL; `.` <- NULL; tree <- NULL; `split var` <- NULL; depth <- NULL
7174
min_depth_interactions_frame <- min_depth_interactions_values(forest, vars)
7275
mean_tree_depth <- min_depth_interactions_frame[[2]]
7376
min_depth_interactions_frame <- min_depth_interactions_frame[[1]]
@@ -122,13 +125,14 @@ min_depth_interactions <- function(forest, vars = important_variables(measure_im
122125
#' @import ggplot2
123126
#'
124127
#' @examples
125-
#' forest <- randomForest::randomForest(Species ~ ., data = iris)
128+
#' forest <- randomForest::randomForest(Species ~ ., data = iris, ntree = 200)
126129
#' plot_min_depth_interactions(min_depth_interactions(forest, names(iris)))
127130
#'
128131
#' @export
129132
plot_min_depth_interactions <- function(interactions_frame, k = 30,
130133
main = paste0("Mean minimal depth for ",
131134
paste0(k, " most frequent interactions"))){
135+
mean_min_depth <- NULL; occurrences <- NULL; uncond_mean_min_depth <- NULL
132136
if("randomForest" %in% class(interactions_frame)){
133137
interactions_frame <- min_depth_interactions(interactions_frame)
134138
}
@@ -165,6 +169,9 @@ plot_min_depth_interactions <- function(interactions_frame, k = 30,
165169
#' @return A ggplot2 object
166170
#'
167171
#' @import ggplot2
172+
#' @importFrom stats predict
173+
#' @importFrom stats terms
174+
#' @importFrom stats as.formula
168175
#'
169176
#' @examples
170177
#' forest <- randomForest::randomForest(Species ~., data = iris)

man/explain_forest.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/min_depth_interactions.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_min_depth_interactions.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)