Skip to content

Getting MCU information from rpart and party

Nathaniel Phillips edited this page Jul 12, 2017 · 1 revision

Getting MCU info from rpart

One of the main outputs of an FFTrees object is mcu "Mean cues used". This indicates how many nodes are used, on average, when applying a tree to data. To do similar calculations from an rpart or party object, one can do the following

partykit

# Solution given by Achim Zeileis ([email protected])
nparent <- function(x) {
 if(inherits(x, "party")) x <- node_party(x)
 if(is.terminal(x)) return(cbind("id" = x$id, "nparent" = 0L))
 np <- do.call("rbind", lapply(kids_node(x), nparent))
 np[, 2L] <- np[, 2L] + 1L
 return(np)
}

rpart

# Solution given by Terry Therneau ([email protected])
fit <- rpart(y ~ ...)

#The row names of zz$frame are the node numbers in the tree.  
node <- as.numeric(row.names(fit$frame))
depth <- floor(log(node, 2))
Clone this wiki locally