Plot party decision tree

自古美人都是妖i 提交于 2019-12-09 13:56:09

问题


I have the following plot as you can see in the picture, Is there any way to see exact number of percentage in the leaf nodes?


回答1:


If you want to "see" the percentages, the easiest way is to make a table() of the terminal nodes vs. the response and then look at the conditional proportions.

If you want to "see" the proportions in the barplot, then there was no possibility to do this up to now. However, I tweaked the node_barplot() function to accomodate this feature. So if you re-install the partykit package (successor of the party package) from R-Forge you can try it:

install.packages("partykit", repos = "http://R-Forge.R-project.org")
library("partykit")

For illustration I will just use the iris data:

ct <- ctree(Species ~ ., data = iris)
plot(ct, tp_args = list(text = TRUE))

By enabling the text = TRUE option you get the labels drawn above the bars (horizontally). An equivalent specification would be text = "horizontal" or text = "h". If you want a narrower layout you could also use:

plot(ct, tp_args = list(text = "vertical", ymax = 1.5))

And the frequency table is simply:

tab <- table(predict(ct, type = "node"), iris$Species)
prop.table(tab, 1) * 100
##         setosa versicolor  virginica
##   2 100.000000   0.000000   0.000000
##   5   0.000000  97.826087   2.173913
##   6   0.000000  50.000000  50.000000
##   7   0.000000   2.173913  97.826087


来源:https://stackoverflow.com/questions/39999994/plot-party-decision-tree

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