Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a rpart decision tree modell with the sunburst view

I found a way to plot a decision tree solution from rpart with the sunburstR-package. To plotting a sunburst it is necessary to have data.frame which represents a sequence. I modified the decision tree result to a sequence like below

Result of the decision tree

rpart(Species~.,data=iris)

n= 150 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 150 100 setosa (0.33333333 0.33333333 0.33333333)  
  2) Petal.Length< 2.45 50   0 setosa (1.00000000 0.00000000 0.00000000) *
  3) Petal.Length>=2.45 100  50 versicolor (0.00000000 0.50000000 0.50000000)  
    6) Petal.Width< 1.75 54   5 versicolor (0.00000000 0.90740741 0.09259259) *
    7) Petal.Width>=1.75 46   1 virginica (0.00000000 0.02173913 0.97826087) * 

Sequence for the sunburst:

sequences_1<-1
sequences_1<-data.frame(sequences_1)
sequences_1[1:3,]<-1
sequences_1$V1[1]<-"Petal.Length<_2.45-setosa"
sequences_1$V1[2]<-"Petal.Length>=2.45-Petal.Width<_1.75_54_5-versicolor"
sequences_1$V1[3]<-"Petal.Length>=2.45-Petal.Width>=1.75_46_1-virginica"
sequences_1$V2[1]<-50
sequences_1$V2[2]<-54
sequences_1$V2[3]<-46
sequences_1$sequences_1<-NULL

Plotting Sunburst:

library(sunburstR)
sunburst(sequences_1,count=TRUE)

The sequence for the sunburst plot, I did manually. Do someone know how to build the sequence automatically like above from the result of the rpart decision tree?

like image 908
hkn Avatar asked Jan 29 '26 16:01

hkn


1 Answers

d3r provides a function d3_party to convert rpart/partykit into a d3 hierarchy. sunburst can use the result of d3_party with one minor modification to change "rule" to "name". This is not ideal but in most cases will work flawlessly.

library(rpart)
library(d3r)
# d3_party requires partykit
# install.packages("partykit")
library(sunburstR)

rp <- rpart(Species~.,data=iris)
rp_d3 <- d3_party(rp)

# one trick/hack required since sunburst expects
#   name but d3_party gives rule
#   this is ugly but let's replace all "rule" with "name"
#   with gsub
rp_d3 <- gsub(
  x = rp_d3,
  pattern = '"rule":',
  replacement = '"name":'
)

sunburst(
  rp_d3,
  valueField = "n",
  sumNodes = FALSE,
  count = TRUE,
  legend = FALSE
)
like image 159
timelyportfolio Avatar answered Feb 01 '26 07:02

timelyportfolio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!