我在使用ctree数据的for循环中遇到了一些奇怪的问题。如果我在循环中编写此代码,则R冻结。
data = read.csv("train.csv") #data description https://www.kaggle.com/c/titanic-gettingStarted/data
treet = ctree(Survived ~ ., data = data)
print(plot(treet))
有时我会收到一个错误:“预测因子超过52个级别,因打印输出被截断”,我的树以非常奇怪的方式显示。有时工作得很好。真的,真奇怪!
我的循环代码:
functionPlot <- function(traine, i) {
print(i) # print only once, then RStudio freezes
tempd <- ctree(Survived ~ ., data = traine)
print(plot(tempd))
}
for(i in 1:2) {
smp_size <- floor(0.70 * nrow(data))
train_ind <- sample(seq_len(nrow(data)), size = smp_size)
set.seed(100 + i)
train <- data[train_ind, ]
test <- data[-train_ind, ]
#
functionPlot(train,i)
}
答案 0 :(得分:3)
ctree()
函数期望(a)对每个变量使用适当的类(数字,因子等),并且(b)在模型公式中仅使用有用的预测变量。
至于(b)你提供的变量实际上只是字符(如Name
)而不是因素。这可能需要适当地预处理或从分析中省略。
即使你没有,你也不会得到最好的结果,因为一些变量(如Survived
和Pclass
)是用数字编码的,但实际上是分类变量应该是因子。如果您查看https://www.kaggle.com/c/titanic/forums/t/13390/introducing-kaggle-scripts中的脚本,那么您还将看到如何执行数据准备。在这里,我使用
titanic <- read.csv("train.csv")
titanic$Survived <- factor(titanic$Survived,
levels = 0:1, labels = c("no", "yes"))
titanic$Pclass <- factor(titanic$Pclass)
titanic$Name <- as.character(titanic$Name)
至于(b),然后我继续使用已经充分预处理以进行有意义分析的变量来调用ctree()
。 (我使用包partykit
中较新推荐的实现。)
library("partykit")
ct <- ctree(Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked,
data = titanic)
plot(ct)
print(ct)
这会产生以下图形输出:
以下打印输出:
Model formula:
Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked
Fitted party:
[1] root
| [2] Sex in female
| | [3] Pclass in 1, 2: yes (n = 170, err = 5.3%)
| | [4] Pclass in 3
| | | [5] Fare <= 23.25: yes (n = 117, err = 41.0%)
| | | [6] Fare > 23.25: no (n = 27, err = 11.1%)
| [7] Sex in male
| | [8] Pclass in 1
| | | [9] Age <= 52: no (n = 88, err = 43.2%)
| | | [10] Age > 52: no (n = 34, err = 20.6%)
| | [11] Pclass in 2, 3
| | | [12] Age <= 9
| | | | [13] Pclass in 3: no (n = 71, err = 18.3%)
| | | | [14] Pclass in 2: yes (n = 13, err = 30.8%)
| | | [15] Age > 9: no (n = 371, err = 11.3%)
Number of inner nodes: 7
Number of terminal nodes: 8