2

如何创建一个包含下图中每个节点的百分比的表?

library(rpart)
library(rattle)
library(rpart.plot)
library(RColorBrewer)

fit <- rpart(Species ~ ., data=iris, method="class")
fancyRpartPlot(fit)

结果如下图:

图片

我想输出一个表格,其中物种作为第一列,第二列中每个节点的相关百分比。表的第二次迭代将排除第一个节点 (100%) 并通过保留包含更高百分比的行来删除重复项。

在浏览了“rpart”文档之后,我仍然无法弄清楚如何创建这个表。请让我知道你的想法。

感谢您的时间。

4

1 回答 1

1

rpart-object 的 where 元素是终端节点的预测类。你可以在一个表中得到这个:

> iris$where <- fit$where
> with(iris, table(Species, where))
            where
Species       2  4  5
  setosa     50  0  0
  versicolor  0 49  1
  virginica   0  5 45

我猜你想要列总和除以总计数?

> 100*colSums(with(iris, table(Species, where)) )/150
       2        4        5 
33.33333 36.00000 30.66667 
于 2014-12-31T23:56:32.877 回答