Skip to content

Commit e9f106b

Browse files
committed
add tests for ranger and fix an issue with interactions for ranger
1 parent 57594f9 commit e9f106b

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

R/min_depth_interactions.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ conditional_depth_ranger <- function(frame, vars){
3939
df <- frame[begin:nrow(frame), setdiff(names(frame), setdiff(vars, j))]
4040
df[[j]][1] <- 0
4141
for(k in 2:nrow(df)){
42-
if(length(df[(!is.na(df[, "leftChild"]) & df[, "leftChild"] == as.numeric(df[k, "number"])) |
43-
(!is.na(df[, "rightChild"]) & df[, "rightChild"] == as.numeric(df[k, "number"])), j]) != 0){
42+
if(length(df[(!is.na(df[, "leftChild"]) & df[, "leftChild"] == as.numeric(df[k, "number"]) - 1) |
43+
(!is.na(df[, "rightChild"]) & df[, "rightChild"] == as.numeric(df[k, "number"]) - 1), j]) != 0){
4444
df[k, j] <-
45-
df[(!is.na(df[, "leftChild"]) & df[, "leftChild"] == as.numeric(df[k, "number"])) |
46-
(!is.na(df[, "rightChild"]) & df[, "rightChild"] == as.numeric(df[k, "number"])), j] + 1
45+
df[(!is.na(df[, "leftChild"]) & df[, "leftChild"] == as.numeric(df[k, "number"]) - 1) |
46+
(!is.na(df[, "rightChild"]) & df[, "rightChild"] == as.numeric(df[k, "number"]) - 1), j] + 1
4747
}
4848
}
4949
frame[begin:nrow(frame), setdiff(names(frame), setdiff(vars, j))] <- df

inst/testdata/test_ranger.rda

2.45 KB
Binary file not shown.

tests/testthat/test_ranger.R

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,100 @@
11
library(ranger)
22
library(dplyr)
3-
set.seed(12345)
3+
load(system.file("testdata/test_ranger.rda", package="randomForestExplainer", mustWork=TRUE))
4+
# Test input generated by:
5+
# library(ranger)
6+
# library(survival)
7+
# set.seed(12345)
8+
# ranger_c <- ranger(Species ~ ., data = iris, importance = "impurity", num.trees = 2)
9+
# ranger_r <- ranger(mpg ~ ., data = mtcars, importance = "impurity", num.trees = 2)
10+
# ranger_s <- ranger(Surv(futime, fustat) ~ ., data = ovarian, importance = "impurity", num.trees = 2)
11+
# save(ranger_c, ranger_r, ranger_s, file = "inst/testdata/test_ranger.rda")
12+
413

514
context("Test ranger classification forests")
6-
forest <- ranger(Species ~ ., data = iris, probability = TRUE, num.trees = 2, importance = "impurity")
715

816
test_that("measure_importance works", {
9-
imp_df <- measure_importance(forest, mean_sample = "all_trees",
17+
imp_df <- measure_importance(ranger_c, mean_sample = "all_trees",
18+
measures = c("mean_min_depth", "impurity",
19+
"no_of_nodes", "times_a_root", "p_value"))
20+
expect_equal(as.character(imp_df$variable), c("Petal.Length", "Petal.Width", "Sepal.Length", "Sepal.Width"))
21+
})
22+
23+
test_that("important_variables works", {
24+
imp_vars <- important_variables(ranger_c, k = 3,
25+
measures = c("mean_min_depth", "impurity",
26+
"no_of_nodes", "times_a_root", "p_value"))
27+
expect_equal(imp_vars, c("Petal.Width", "Petal.Length", "Sepal.Length"))
28+
})
29+
30+
test_that("min_depth_distribution works", {
31+
min_depth_dist <- min_depth_distribution(ranger_c)
32+
expect_equivalent(min_depth_dist[min_depth_dist$tree == 1 & min_depth_dist$variable == "Petal.Width", ]$minimal_depth,
33+
0)
34+
})
35+
36+
test_that("min_depth_interactions works", {
37+
min_depth_int <- min_depth_interactions(ranger_c, vars = c("Petal.Width"))
38+
expect_equivalent(min_depth_int[min_depth_int$interaction == "Petal.Width:Sepal.Length", ]$mean_min_depth,
39+
1.5)
40+
})
41+
42+
43+
context("Test ranger regression forests")
44+
45+
test_that("measure_importance works", {
46+
imp_df <- measure_importance(ranger_r, mean_sample = "all_trees",
47+
measures = c("mean_min_depth", "impurity",
48+
"no_of_nodes", "times_a_root", "p_value"))
49+
expect_equal(as.character(imp_df$variable),
50+
c("am", "carb", "cyl", "disp", "drat", "gear", "hp", "qsec", "vs", "wt"))
51+
})
52+
53+
test_that("important_variables works", {
54+
imp_vars <- important_variables(ranger_r, k = 3,
55+
measures = c("mean_min_depth", "impurity",
56+
"no_of_nodes", "times_a_root", "p_value"))
57+
expect_equal(imp_vars, c("wt", "cyl", "disp"))
58+
})
59+
60+
test_that("min_depth_distribution works", {
61+
min_depth_dist <- min_depth_distribution(ranger_r)
62+
expect_equivalent(min_depth_dist[min_depth_dist$tree == 2 & min_depth_dist$variable == "cyl", ]$minimal_depth,
63+
0)
64+
})
65+
66+
test_that("min_depth_interactions works", {
67+
min_depth_int <- min_depth_interactions(ranger_r, vars = c("cyl"))
68+
expect_equivalent(min_depth_int[min_depth_int$interaction == "cyl:wt", ]$mean_min_depth,
69+
0.5)
70+
})
71+
72+
73+
context("Test ranger survival forests")
74+
75+
test_that("measure_importance works", {
76+
imp_df <- measure_importance(ranger_s, mean_sample = "all_trees",
1077
measures = c("mean_min_depth", "impurity",
11-
"no_of_nodes", "times_a_root"))
12-
expect_equal(imp_df$variable, c("Petal.Length", "Petal.Width", "Sepal.Length", "Sepal.Width"))
78+
"no_of_nodes", "times_a_root", "p_value"))
79+
expect_equal(as.character(imp_df$variable),
80+
c("age", "ecog.ps", "resid.ds", "rx"))
1381
})
1482

1583
test_that("important_variables works", {
16-
imp_vars <- important_variables(forest, k = 3,
84+
imp_vars <- important_variables(ranger_s, k = 3,
1785
measures = c("mean_min_depth", "impurity",
18-
"no_of_nodes", "times_a_root"))
19-
expect_equal(imp_vars, c("Petal.Width", "Sepal.Length", "Petal.Length"))
86+
"no_of_nodes", "times_a_root", "p_value"))
87+
expect_equal(imp_vars, c("age", "ecog.ps", "rx"))
2088
})
2189

2290
test_that("min_depth_distribution works", {
23-
min_depth_dist <- min_depth_distribution(forest)
24-
print(min_depth_dist)
25-
expect_equivalent(arrange(min_depth_dist, tree, minimal_depth, variable),
26-
data.frame("tree" = c(1, 1, 1, 2, 2, 2),
27-
"variable"=c("Petal.Width", "Sepal.Length", "Petal.Length", "Petal.Width", "Sepal.Length", "Sepal.Width"),
28-
"minimal_depth"=c(0, 2, 3, 0, 3, 3), stringsAsFactors = FALSE))
91+
min_depth_dist <- min_depth_distribution(ranger_s)
92+
expect_equivalent(min_depth_dist[min_depth_dist$tree == 1 & min_depth_dist$variable == "age", ]$minimal_depth,
93+
0)
2994
})
3095

3196
test_that("min_depth_interactions works", {
32-
min_depth_int <- min_depth_interactions(forest, vars = c("Petal.Width"))
33-
expect_equal(as.character(min_depth_int$variable), c("Petal.Length", "Petal.Width", "Sepal.Length", "Sepal.Width"))
97+
min_depth_int <- min_depth_interactions(ranger_s, vars = c("age"))
98+
expect_equivalent(min_depth_int[min_depth_int$interaction == "age:ecog.ps", ]$mean_min_depth,
99+
0.5)
34100
})

0 commit comments

Comments
 (0)