Skip to contents

Extract tree information of a ranger object.

Usage

treeInfo(object, tree = 1)

Arguments

object

ranger object.

tree

Number of the tree of interest.

Value

A data.frame with the columns

nodeIDThe nodeID, 0-indexed.
leftChildID of the left child node, 0-indexed.
rightChildID of the right child node, 0-indexed.
splitvarIDID of the splitting variable, 0-indexed. Caution, the variable order changes if the formula interface is used.
splitvarNameName of the splitting variable.
splitvalThe splitting value. For numeric or ordinal variables, all values smaller or equal go to the left, larger values to the right. For unordered factor variables see above.
terminalLogical, TRUE for terminal nodes.
predictionOne column with the predicted class (factor) for classification and the predicted numerical value for regression. One probability per class for probability estimation in several columns. Nothing for survival, refer to object$forest$chf for the CHF node predictions.
numSamplesNumber of samples in the node (only if ranger called with node.stats = TRUE).
splitStatSplit statistics, i.e., value of the splitting criterion (only if ranger called with node.stats = TRUE).

Details

Node and variable ID's are 0-indexed, i.e., node 0 is the root node. If the formula interface is used in the ranger call, the variable ID's are usually different to the original data used to grow the tree. Refer to the variable name instead to be sure.

Splitting at unordered factors (nominal variables) depends on the option respect.unordered.factors in the ranger call. For the "ignore" and "order" approaches, all values smaller or equal the splitval value go to the left and all values larger go to the right, as usual. However, with "order" the values correspond to the order in object$forest$covariate.levels instead of the original order (usually alphabetical). In the "partition" mode, the splitval values for unordered factor are comma separated lists of values, representing the factor levels (in the original order) going to the right.

See also

Author

Marvin N. Wright

Examples

rf <- ranger(Species ~ ., data = iris)
treeInfo(rf, 1)
#>    nodeID leftChild rightChild splitvarID splitvarName splitval terminal
#> 1       0         1          2          2 Petal.Length     2.45    FALSE
#> 2       1        NA         NA         NA         <NA>       NA     TRUE
#> 3       2         3          4          2 Petal.Length     4.85    FALSE
#> 4       3         5          6          3  Petal.Width     1.65    FALSE
#> 5       4         7          8          3  Petal.Width     1.75    FALSE
#> 6       5        NA         NA         NA         <NA>       NA     TRUE
#> 7       6         9         10          2 Petal.Length     4.65    FALSE
#> 8       7        11         12          3  Petal.Width     1.65    FALSE
#> 9       8        NA         NA         NA         <NA>       NA     TRUE
#> 10      9        NA         NA         NA         <NA>       NA     TRUE
#> 11     10        NA         NA         NA         <NA>       NA     TRUE
#> 12     11        13         14          2 Petal.Length     4.95    FALSE
#> 13     12        NA         NA         NA         <NA>       NA     TRUE
#> 14     13        NA         NA         NA         <NA>       NA     TRUE
#> 15     14        15         16          0 Sepal.Length     6.05    FALSE
#> 16     15        17         18          2 Petal.Length     5.05    FALSE
#> 17     16        NA         NA         NA         <NA>       NA     TRUE
#> 18     17        NA         NA         NA         <NA>       NA     TRUE
#> 19     18        NA         NA         NA         <NA>       NA     TRUE
#>    prediction
#> 1        <NA>
#> 2      setosa
#> 3        <NA>
#> 4        <NA>
#> 5        <NA>
#> 6  versicolor
#> 7        <NA>
#> 8        <NA>
#> 9   virginica
#> 10  virginica
#> 11 versicolor
#> 12       <NA>
#> 13 versicolor
#> 14 versicolor
#> 15       <NA>
#> 16       <NA>
#> 17  virginica
#> 18  virginica
#> 19 versicolor