rpart树中的标签错误 [英] Wrong labels in rpart tree
问题描述
在R中使用rpart时,我遇到了一些标签问题.
I am running into some labels issue when using rpart in R.
这是我的情况.
我正在处理带有分类变量的数据集,这是我的数据的一部分
I'm working on a dataset with categorical variables, here's an extract of my data
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
当我使用
plot(tree)
text(tree)
我得到了错误的标签:对于Entity,我得到了"abcd"
I get wrong labels : for Entity, I get "abcd"
我为什么要得到它以及如何解决?
Why do I get that and how can I fix that ?
谢谢您的帮助
推荐答案
默认情况下,plot.rpart
仅将因子变量的级别标记为letters
,第一个级别为a
,第二个级别为b
,依此类推在.示例:
By default plot.rpart
will just label the levels of factor variables with letters
, the first level will be a
, second b
and so on. Example:
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)
我认为不要自定义此绘图,而应使用rpart plotting专用软件包:
In my opinion instead of customizing this plot use the rpart plotting dedicated package:
library(rpart.plot)
prp(fit)
它具有许多自定义选项(例如):
it has many customization options (example):
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)
另一个选择是:
library(rattle)
fancyRpartPlot(fit,
type = 4)
在内部使用prp
并使用不同的默认值.
which uses prp
internally with different defaults.
这篇关于rpart树中的标签错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!