Tae-Sung Shin
Tae-Sung Shin

Reputation: 20643

How do I draw split conditions of classification tree in a scatter plot of R?

I fitted a tree from tree package for a simulated data with one output and two predictors in R.

Like a picture below, I want to draw the dash line with my model in R. Is there any way to do that? I tried to draw condition = 0 lines but the lines are from min to max of data.

enter image description here

This is what I did so far with iris data and hard-coded conditions for last two classes

    iristree<-tree(Species ~.,data=iris)
    iristree #find split criterions 
node), split, n, deviance, yval, (yprob)
  * denotes terminal node

1) root 150 329.600 setosa ( 0.33333 0.33333 0.33333 )  
   2) Petal.Length < 2.45 50   0.000 setosa ( 1.00000 0.00000 0.00000 ) *
   3) Petal.Length > 2.45 100 138.600 versicolor ( 0.00000 0.50000 0.50000 )  
     6) Petal.Width < 1.75 54  33.320 versicolor ( 0.00000 0.90741 0.09259 )  
      12) Petal.Length < 4.95 48   9.721 versicolor ( 0.00000 0.97917 0.02083 )  
        24) Sepal.Length < 5.15 5   5.004 versicolor ( 0.00000 0.80000 0.20000 ) *
        25) Sepal.Length > 5.15 43   0.000 versicolor ( 0.00000 1.00000 0.00000 ) *
      13) Petal.Length > 4.95 6   7.638 virginica ( 0.00000 0.33333 0.66667 ) *
     7) Petal.Width > 1.75 46   9.635 virginica ( 0.00000 0.02174 0.97826 )  
      14) Petal.Length < 4.95 6   5.407 virginica ( 0.00000 0.16667 0.83333 ) *
      15) Petal.Length > 4.95 40   0.000 virginica ( 0.00000 0.00000 1.00000 ) *

    ....
plot(iris$Petal.Length, iris$Petal.Width, pch=21, 
         bg=c("red","green3","blue")[unclass(iris$Species)], 
         main="Edgar Anderson's Iris Data")
lines(c(0,8),c(1.75,1.75)) # manually put split criterions
lines(c(4.95,4.95),c(0,3))  # manually put split criterions

and that produced

enter image description here

where I intend bottom and left lines to be deleted.

Upvotes: 2

Views: 2659

Answers (1)

Tae-Sung Shin
Tae-Sung Shin

Reputation: 20643

I found partition.tree in the tree package. With the function, I could make following picture. enter image description here

Although it is not what I wanted, but it's the closest thing I could work on.

Upvotes: 2

Related Questions