如何在R的散点图中绘制分类树的分割条件?

时间:2012-09-22 18:33:01

标签: r tree

我从树包装了一棵树,用于模拟数据,其中一个输出和两个预测变量在R中。

如下图所示,我想在R中用我的模型绘制虚线。有没有办法做到这一点?我试图绘制条件= 0行,但行是从最小到最大的数据。

enter image description here

这是我到目前为止所做的最后两个类的虹膜数据和硬编码条件

    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

那产生了

enter image description here

我打算删除底部和左边的行。

1 个答案:

答案 0 :(得分:2)

我在partition.tree包中找到了tree。有了这个功能,我可以制作下面的图片。 enter image description here

虽然这不是我想要的,但它是我能做的最接近的事情。