【发布时间】:2021-09-18 17:22:59
【问题描述】:
对于以下代码,我希望找到具有最低 xerror 的最小 cp 项
data(iris)
install.packages("rpart")
library(rpart)
set.seed(161)
tree.model1<-rpart(Sepal.Length~., data = iris)
install.packages("rpart.plot")
library(rpart.plot)
rpart.plot(tree.model1)
tree.model2<-rpart(Sepal.Length~., data = iris, cp=0.005)
tree.model2$cptable
par(mfrow=c(1,2))
rpart.plot(tree.model1)
rpart.plot(tree.model2)
which.min(tree.model2$cptable[,"xerror"])
我的问题集中在最后一行,如果我把
which.min(tree.model2$cptable[, xerror] 不行
这里加引号有什么作用?
【问题讨论】:
-
加上引号,您正在查看名称为
"xerror"的列。如果没有引号,您将查看名称由名称为xerror的变量的值给出的列。由于xerror不存在,因此您会收到错误消息。 -
感谢@Limey 非常有帮助
标签: r decision-tree rpart