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
Order of Legend Entries in Ggplot2 Barplots with Coord_Flip()
Using Grep in R to Delete Rows from a Data.Frame
How to Convert a String in a Function into an Object
How to Open an .Xlsb File in R
Automated Ggplot2 Example Gallery in Knitr
Building a Tiny R Package with Cuda and Rcpp
Merge/Combine Columns with Same Name But Incomplete Data
Is Data Really Copied Four Times in R's Replacement Functions
How to Get the Text Between Two Words in R
Rollmean with Dplyr and Magrittr
How to Generate Bin Frequency Table in R
Combining Elements of List of Lists by Index
R Dpylr Select_If with Multiple Conditions
Earliest Date for Each Id in R
Getting Frequency Values from Histogram in R