rpartyctree

Obtaining full path from leaf to root for each terminal node with ctree from partykit


Im a currently working with ctree from R package "partykit" and I was wondering if there is a way to obtain the full path from terminal nodes to root. I would like to have for each leaf the full path to the root expressed as vectors containing the node ids.

library(partykit)
ct <- ctree(Species ~ ., data = iris) 
Model formula:
Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width

Fitted party:
[1] root
|   [2] Petal.Length <= 1.9: setosa (n = 50, err = 0.0%)
|   [3] Petal.Length > 1.9
|   |   [4] Petal.Width <= 1.7
|   |   |   [5] Petal.Length <= 4.8: versicolor (n = 46, err = 2.2%)
|   |   |   [6] Petal.Length > 4.8: versicolor (n = 8, err = 50.0%)
|   |   [7] Petal.Width > 1.7: virginica (n = 46, err = 2.2%)

Number of inner nodes:    3
Number of terminal nodes: 4

Plotting the tree

This is basically what I need:

[[1]]
[1] 2 1

[[2]]
[1] 5 4 3 1

[[3]]
[1] 6 4 3 1

[[4]]
[1] 7 3 1

I would appreciate any help! Thanks!


Solution

  • The following function should do the trick. The first line extracts a list of kids per node and from this you can recursively go through all nodes.

    get_path <- function(object) {
      ## list of kids per node (NULL if terminal)
      kids <- lapply(as.list(object$node), "[[", "kids")
    
      ## recursively add node IDs of children
      add_ids <- function(x) {
        ki <- kids[[x[1L]]]
        if(is.null(ki)) {
          return(list(x))
        } else {
          x <- lapply(ki, "c", x)
          return(do.call("c", lapply(x, add_ids)))
        }
      }
      add_ids(1L)
    }
    

    This can then be applied to any party object:

    get_path(ct)
    ## [[1]]
    ## [1] 2 1
    ## 
    ## [[2]]
    ## [1] 5 4 3 1
    ## 
    ## [[3]]
    ## [1] 6 4 3 1
    ## 
    ## [[4]]
    ## [1] 7 3 1