Debian Codegraph @

R/xgb.plot.tree.R

raw
1 #' Plot boosted trees
2 #'
3 #' Read a tree model text dump and plot the model.
4 #'
5 #' @details
6 #' The content of each node is visualized as follows:
7 #' - For non-terminal nodes, it will display the split condition (number or name
8 #' if available, and the condition that would decide to which node to go
9 #' next).
10 #' - Those nodes will be connected to their children by arrows that indicate
11 #' whether the branch corresponds to the condition being met or not being met.
12 #' - Terminal (leaf) nodes contain the margin to add when ending there.
13 #'
14 #' The "Yes" branches are marked by the "< split_value" label.
15 #' The branches also used for missing values are marked as bold
16 #' (as in "carrying extra capacity").
17 #'
18 #' This function uses [GraphViz](https://www.graphviz.org/) as DiagrammeR
19 #' backend.
20 #'
21 #' @param model Object of class `xgb.Booster`. If it contains feature names
22 #' (they can be set through [setinfo()], they will be used in the
23 #' output from this function.
24 #' @param tree_idx An integer of the tree index that should be used. This
25 #' is an 1-based index.
26 #' @param plot_width,plot_height Width and height of the graph in pixels.
27 #' The values are passed to `DiagrammeR::render_graph()`.
28 #' @param with_stats Whether to dump some additional statistics about the
29 #' splits. When this option is on, the model dump contains two additional
30 #' values: gain is the approximate loss function gain we get in each split;
31 #' cover is the sum of second order gradient in each node.
32 #' @inheritParams xgb.train
33 #' @return
34 #'
35 #' Rendered graph object which is an htmlwidget of ' class `grViz`. Similar to
36 #' "ggplot" objects, it needs to be printed when not running from the command
37 #' line.
38 #'
39 #' @examples
40 #' data("ToothGrowth")
41 #' x <- ToothGrowth[, c("len", "dose")]
42 #' y <- ToothGrowth$supp
43 #' model <- xgboost(
44 #' x, y,
45 #' nthreads = 1L,
46 #' nrounds = 3L,
47 #' max_depth = 3L
48 #' )
49 #'
50 #' # plot the first tree
51 #' xgb.plot.tree(model, tree_idx = 1)
52 #'
53 #' # Below is an example of how to save this plot to a file.
54 #' if (require("DiagrammeR") && require("htmlwidgets")) {
55 #' fname <- file.path(tempdir(), "plot.html'")
56 #' gr <- xgb.plot.tree(model, tree_idx = 1)
57 #' htmlwidgets::saveWidget(gr, fname)
58 #' }
59 #' @export
60 xgb.plot.tree <- function(model,
61 tree_idx = 1,
62 plot_width = NULL,
63 plot_height = NULL,
64 with_stats = FALSE, ...) {
65 check.deprecation(deprecated_plottree_params, match.call(), ...)
66 if (!inherits(model, "xgb.Booster")) {
67 stop("model has to be an object of the class xgb.Booster")
68 }
69 if (!requireNamespace("DiagrammeR", quietly = TRUE)) {
70 stop("The DiagrammeR package is required for xgb.plot.tree", call. = FALSE)
71 }
72
73 txt <- xgb.dump(model, dump_format = "dot", with_stats = with_stats)
74 DiagrammeR::grViz(
75 txt[[tree_idx]], width = plot_width, height = plot_height
76 )
77 }