3

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?

enter image description here

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
Sina PN
  • 131
  • 3
  • 10

1 Answers1

3

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))

tree1

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))

tree2

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
Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • 2
    Error in terminal_panel(, text = "vertical", : unused argument (text = "vertical") – Sina PN Oct 13 '16 at 16:17
  • Sorry Achim, my mistake. need to redo decision trees with "partykit" instead of "party"...Thanks for your help – Sina PN Oct 13 '16 at 16:25
  • Yes, the infrastructure has been entirely rewritten, see `vignette("partykit", package = "partykit")` and `vignette("ctree", package = "partykit")` for an overview and some guidance. – Achim Zeileis Oct 13 '16 at 20:04
  • Could you provide more details? For me in R 3.4.3 using the CRAN version of `partykit` (1.2-0) everything runs smoothly. – Achim Zeileis Jan 20 '18 at 21:50