Ctree() - How to Get the List of Splitting Conditions for Each Terminal Node

How to extract the splitting rules for the terminal nodes of ctree()

I would recommend to use the new partykit implementation of ctree() rather than the old party package, then you can use the function .list.rules.party(). This is not officially exported, yet, but can be leveraged to extract the desired information.

library("partykit")
airq <- subset(airquality, !is.na(Ozone))
ct <- ctree(Ozone ~ ., data = airq)
partykit:::.list.rules.party(ct)
## 3 5
## "Temp <= 82 & Wind <= 6.9" "Temp <= 82 & Wind > 6.9 & Temp <= 77"
## 6 8
## "Temp <= 82 & Wind > 6.9 & Temp > 77" "Temp > 82 & Wind <= 10.3"
## 9
## "Temp > 82 & Wind > 10.3"

Extracting list of the split of a lmtree object

Models of class lmtree inherit from party just like the output from ctree(). Hence, the same approaches that are discussed here on SO for ctree() output can also be applied to lmtree() output. Namely, you can use the (still unexported) .list.rules.party() function:

partykit:::.list.rules.party(tr)
## 2 3
## "z <= 0.495593577856198" "z > 0.495593577856198"

For further adaptations see: also:

  • ctree() - How to get the list of splitting conditions for each terminal node?
  • Get decision tree rule/path pattern for every row of predicted dataset for rpart/ctree package in R

Store terminal node split rule of ctree into a dataframe in R

I think you just want:

lvls <- levels(pdd_ctree@tree$psplit$splitpoint)
left.df = lvls[pdd_ctree@tree$psplit$splitpoint == 1]
right.df = lvls[pdd_ctree@tree$psplit$splitpoint == 0]

extracting predictors from ctree object

Below is the script I implemented to traverse the tree from a ctree object. I use the same example in the party package which is airct dataset.

require(party)
data(airquality)

traverse <- function(treenode){
if(treenode$terminal){
bas=paste("Current node is terminal node with",treenode$nodeID,'prediction',treenode$prediction)
print(bas)
return(0)
} else {
bas=paste("Current node",treenode$nodeID,"Split var. ID:",treenode$psplit$variableName,"split value:",treenode$psplit$splitpoint,'prediction',treenode$prediction)
print(bas)
}
traverse(treenode$left)
traverse(treenode$right)
}

airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq,
controls = ctree_control(maxsurrogate = 3))
plot(airct)

traverse(airct@tree)

This function, traverse, just traverses the tree in a depth-first order. You can change the order of the traversal by changing the recursive part.

Moreover, if you want to return other node characteristics, I would recommend checking the structure of the ctree object.

edit: Minor code revisions.



Related Topics



Leave a reply



Submit