|
1 | 1 | library(ranger) |
2 | 2 | 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 | + |
4 | 13 |
|
5 | 14 | context("Test ranger classification forests") |
6 | | -forest <- ranger(Species ~ ., data = iris, probability = TRUE, num.trees = 2, importance = "impurity") |
7 | 15 |
|
8 | 16 | 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", |
10 | 77 | 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")) |
13 | 81 | }) |
14 | 82 |
|
15 | 83 | test_that("important_variables works", { |
16 | | - imp_vars <- important_variables(forest, k = 3, |
| 84 | + imp_vars <- important_variables(ranger_s, k = 3, |
17 | 85 | 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")) |
20 | 88 | }) |
21 | 89 |
|
22 | 90 | 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) |
29 | 94 | }) |
30 | 95 |
|
31 | 96 | 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) |
34 | 100 | }) |
0 commit comments