rpart 树中的错误标签

Lay*_*ale 3 tree visualization r labels rpart

在 R 中使用 rpart 时,我遇到了一些标签问题。

这是我的情况。

我正在处理具有分类变量的数据集,这是我的数据的摘录

head(Dataset)
Entity  IL  CP  TD  Budget 
  2      1   3   2     250
  5      2   2   1     663
  6      1   2   3     526 
  2      3   1   2     522
Run Code Online (Sandbox Code Playgroud)

当我绘制我的决策树添加标签时,使用

plot(tree) 
text(tree)
Run Code Online (Sandbox Code Playgroud)

我得到错误的标签:对于实体,我得到“abcd”

为什么我会得到这个,我该如何解决?

感谢您的帮助

mis*_*use 5

默认情况下,plot.rpart只会用 标记因子变量的级别letters,第一个级别将是a,第二个b等等。例子:

library(rpart)
library(ggplot2) #for the data

data("diamonds")    
df <- diamonds[1:2000,]

fit <- rpart(price ~ color + cut + clarity, data = df)
plot(fit)
text(fit)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在我看来,不要使用 rpart 绘图专用包来自定义此图:

library(rpart.plot)
prp(fit)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

它有许多自定义选项(示例):

prp(fit,
    type = 4,
    extra = 101,
    fallen.leaves = T,
    box.palette = colorRampPalette(c("red", "white", "green3"))(10),
    round = 2,
    branch.lty = 2,
    branch.lwd = 1,
    space = -1,
    varlen = 0,
    faclen = 0)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

另一种选择是:

library(rattle)
fancyRpartPlot(fit,
               type = 4)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

它在prp内部使用不同的默认值。