Skip to content

Commit b35f4da

Browse files
authored
Merge pull request #14 from Yue-Jiang/master
add plotting tests
2 parents d01a2b3 + d6b345c commit b35f4da

File tree

4 files changed

+180
-5
lines changed

4 files changed

+180
-5
lines changed

R/min_depth_distribution.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ plot_min_depth_distribution <- function(min_depth_frame, k = 10, min_no_of_trees
143143
mean_sample = "top_trees", mean_scale = FALSE, mean_round = 2,
144144
main = "Distribution of minimal depth and its mean"){
145145
minimal_depth <- NULL; mean_minimal_depth_label <- NULL; mean_minimal_depth <- NULL
146-
if("randomForest" %in% class(min_depth_frame)){
146+
if(any(c("randomForest", "ranger") %in% class(min_depth_frame))){
147147
min_depth_frame <- min_depth_distribution(min_depth_frame)
148148
}
149149
min_depth_count_list <- min_depth_count(min_depth_frame)

inst/testdata/test_ranger.rda

557 Bytes
Binary file not shown.

tests/testthat/test_randomForest.R

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ test_that("min_depth_interactions works", {
3939
0)
4040
})
4141

42+
test_that("plot_multi_way_importance works", {
43+
p <- plot_multi_way_importance(rf_c)
44+
expect_silent(print(p))
45+
})
46+
47+
test_that("plot_min_depth_distribution works", {
48+
p <- plot_min_depth_distribution(rf_c)
49+
expect_silent(print(p))
50+
})
51+
52+
test_that("plot_min_depth_interactions works", {
53+
p <- plot_min_depth_interactions(rf_c)
54+
expect_silent(print(p))
55+
})
56+
57+
test_that("plot_predict_interaction works", {
58+
p <- plot_predict_interaction(rf_c, data = iris, variable1 = "Petal.Width", variable2 = "Petal.Length")
59+
expect_silent(print(p))
60+
})
61+
62+
test_that("plot_importance_ggpairs works", {
63+
p <- plot_importance_ggpairs(rf_c)
64+
expect_silent(suppressMessages(print(p)))
65+
})
66+
67+
test_that("plot_importance_rankings works", {
68+
p <- plot_importance_rankings(rf_c)
69+
expect_silent(suppressWarnings(suppressMessages(print(p))))
70+
})
4271

4372
context("Test randomForest regression forests")
4473

@@ -69,6 +98,35 @@ test_that("min_depth_interactions works", {
6998
1)
7099
})
71100

101+
test_that("plot_multi_way_importance works", {
102+
p <- plot_multi_way_importance(rf_r)
103+
expect_silent(suppressWarnings(print(p)))
104+
})
105+
106+
test_that("plot_min_depth_distribution works", {
107+
p <- plot_min_depth_distribution(rf_r)
108+
expect_silent(print(p))
109+
})
110+
111+
test_that("plot_min_depth_interactions works", {
112+
p <- plot_min_depth_interactions(rf_r)
113+
expect_silent(print(p))
114+
})
115+
116+
test_that("plot_predict_interaction works", {
117+
p <- plot_predict_interaction(rf_r, data = mtcars, variable1 = "cyl", variable2 = "wt")
118+
expect_silent(print(p))
119+
})
120+
121+
test_that("plot_importance_ggpairs works", {
122+
p <- plot_importance_ggpairs(rf_r)
123+
expect_silent(suppressWarnings(suppressMessages(print(p))))
124+
})
125+
126+
test_that("plot_importance_rankings works", {
127+
p <- plot_importance_rankings(rf_r)
128+
expect_silent(suppressWarnings(suppressMessages(print(p))))
129+
})
72130

73131
context("Test randomForest unsupervised forests")
74132

@@ -98,3 +156,33 @@ test_that("min_depth_interactions works", {
98156
expect_equivalent(min_depth_int[min_depth_int$interaction == "Petal.Width:Sepal.Length", ]$mean_min_depth,
99157
1)
100158
})
159+
160+
test_that("plot_multi_way_importance works", {
161+
p <- plot_multi_way_importance(rf_u)
162+
expect_silent(suppressWarnings(print(p)))
163+
})
164+
165+
test_that("plot_min_depth_distribution works", {
166+
p <- plot_min_depth_distribution(rf_u)
167+
expect_silent(print(p))
168+
})
169+
170+
test_that("plot_min_depth_interactions works", {
171+
p <- plot_min_depth_interactions(rf_u)
172+
expect_silent(print(p))
173+
})
174+
175+
test_that("plot_predict_interaction does not work for unsupervised forests", {
176+
expect_warning(p <- plot_predict_interaction(rf_u, data = iris, variable1 = "Petal.Width", variable2 = "Sepal.Length"))
177+
expect_null(p)
178+
})
179+
180+
test_that("plot_importance_ggpairs works", {
181+
p <- plot_importance_ggpairs(rf_u)
182+
expect_silent(suppressWarnings(suppressMessages(print(p))))
183+
})
184+
185+
test_that("plot_importance_rankings works", {
186+
p <- plot_importance_rankings(rf_u)
187+
expect_silent(suppressWarnings(suppressMessages(print(p))))
188+
})

tests/testthat/test_ranger.R

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ load(system.file("testdata/test_ranger.rda", package="randomForestExplainer", mu
55
# library(ranger)
66
# library(survival)
77
# set.seed(12345)
8-
# ranger_c <- ranger(Species ~ ., data = iris, importance = "impurity", num.trees = 2)
8+
# ranger_c <- ranger(Species ~ ., data = iris, importance = "impurity", num.trees = 2, probability = TRUE)
99
# ranger_r <- ranger(mpg ~ ., data = mtcars, importance = "impurity", num.trees = 2)
1010
# 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-
11+
# save(ranger_c, ranger_r, ranger_s, ovarian, file = "inst/testdata/test_ranger.rda")
1312

1413
context("Test ranger classification forests")
1514

@@ -24,7 +23,7 @@ test_that("important_variables works", {
2423
imp_vars <- important_variables(ranger_c, k = 3,
2524
measures = c("mean_min_depth", "impurity",
2625
"no_of_nodes", "times_a_root", "p_value"))
27-
expect_equal(imp_vars, c("Petal.Width", "Petal.Length", "Sepal.Length"))
26+
expect_equal(imp_vars, c("Petal.Width", "Sepal.Length", "Petal.Length"))
2827
})
2928

3029
test_that("min_depth_distribution works", {
@@ -39,6 +38,35 @@ test_that("min_depth_interactions works", {
3938
1.5)
4039
})
4140

41+
test_that("plot_multi_way_importance works", {
42+
p <- plot_multi_way_importance(ranger_c)
43+
expect_silent(print(p))
44+
})
45+
46+
test_that("plot_min_depth_distribution works", {
47+
p <- plot_min_depth_distribution(ranger_c)
48+
expect_silent(print(p))
49+
})
50+
51+
test_that("plot_min_depth_interactions works", {
52+
p <- plot_min_depth_interactions(ranger_c)
53+
expect_silent(print(p))
54+
})
55+
56+
test_that("plot_predict_interaction works", {
57+
p <- plot_predict_interaction(ranger_c, data = iris, variable1 = "Petal.Width", variable2 = "Petal.Length")
58+
expect_silent(print(p))
59+
})
60+
61+
test_that("plot_importance_ggpairs works", {
62+
p <- plot_importance_ggpairs(ranger_c)
63+
expect_silent(suppressMessages(print(p)))
64+
})
65+
66+
test_that("plot_importance_rankings works", {
67+
p <- plot_importance_rankings(ranger_c)
68+
expect_silent(suppressWarnings(suppressMessages(print(p))))
69+
})
4270

4371
context("Test ranger regression forests")
4472

@@ -69,6 +97,35 @@ test_that("min_depth_interactions works", {
6997
0.5)
7098
})
7199

100+
test_that("plot_multi_way_importance works", {
101+
p <- plot_multi_way_importance(ranger_r)
102+
expect_silent(suppressWarnings(print(p)))
103+
})
104+
105+
test_that("plot_min_depth_distribution works", {
106+
p <- plot_min_depth_distribution(ranger_r)
107+
expect_silent(print(p))
108+
})
109+
110+
test_that("plot_min_depth_interactions works", {
111+
p <- plot_min_depth_interactions(ranger_r)
112+
expect_silent(print(p))
113+
})
114+
115+
test_that("plot_predict_interaction works", {
116+
p <- plot_predict_interaction(ranger_r, data = mtcars, variable1 = "cyl", variable2 = "wt")
117+
expect_silent(print(p))
118+
})
119+
120+
test_that("plot_importance_ggpairs works", {
121+
p <- plot_importance_ggpairs(ranger_r)
122+
expect_silent(suppressWarnings(suppressMessages(print(p))))
123+
})
124+
125+
test_that("plot_importance_rankings works", {
126+
p <- plot_importance_rankings(ranger_c)
127+
expect_silent(suppressWarnings(suppressMessages(print(p))))
128+
})
72129

73130
context("Test ranger survival forests")
74131

@@ -98,3 +155,33 @@ test_that("min_depth_interactions works", {
98155
expect_equivalent(min_depth_int[min_depth_int$interaction == "age:ecog.ps", ]$mean_min_depth,
99156
0.5)
100157
})
158+
159+
test_that("plot_multi_way_importance works", {
160+
p <- plot_multi_way_importance(ranger_s)
161+
expect_silent(suppressWarnings(print(p)))
162+
})
163+
164+
test_that("plot_min_depth_distribution works", {
165+
p <- plot_min_depth_distribution(ranger_s)
166+
expect_silent(print(p))
167+
})
168+
169+
test_that("plot_min_depth_interactions works", {
170+
p <- plot_min_depth_interactions(ranger_s)
171+
expect_silent(print(p))
172+
})
173+
174+
test_that("plot_predict_interaction works", {
175+
p <- plot_predict_interaction(ranger_s, data = ovarian, variable1 = "age", variable2 = "ecog.ps")
176+
expect_silent(print(p))
177+
})
178+
179+
test_that("plot_importance_ggpairs works", {
180+
p <- plot_importance_ggpairs(ranger_s)
181+
expect_silent(suppressWarnings(suppressMessages(print(p))))
182+
})
183+
184+
test_that("plot_importance_rankings works", {
185+
p <- plot_importance_rankings(ranger_s)
186+
expect_silent(suppressWarnings(suppressMessages(print(p))))
187+
})

0 commit comments

Comments
 (0)