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

北城以北 提交于 2019-12-19 04:38:28

问题


I have a data set with 6 categorical variables with levels ranging from 5 to 28. I have obtained an output from ctree() (party package) with 17 terminal nodes. I have followed the inputs by @Galled from ctree() - How to get the list of splitting conditions for each terminal node? to arrive at my desired output.

But, I'm getting the following error post running the code:

Error in data.frame(ResulTable, Means, Counts) : 
  arguments imply differing number of rows: 17, 2

I have tried adding this extra lines:

ResulTable <- rbind(ResulTable, cbind(Node = Node, Path = Path2))

ResulTable$Node <- rownames(ResulTable)

melt(ResulTable)

but no success so far. Any pointers on where it is going wrong?


回答1:


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" 


来源:https://stackoverflow.com/questions/29999626/how-to-extract-the-splitting-rules-for-the-terminal-nodes-of-ctree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!