gpt4 book ai didi

r - train() 函数中的缺失值错误 树木的插入符号

转载 作者:行者123 更新时间:2023-12-04 09:32:51 27 4
gpt4 key购买 nike

我是 R 新手,我正在尝试构建决策树。我已经用过这个包 partyctreerpart对于 rpart。

但是,因为我需要对我的模型进行交叉验证,所以我开始使用 caret包,因为我可以通过使用函数 `train() 和我想使用的方法来做到这一点。

library(caret)
cvCtrl <- trainControl(method = "repeatedcv", repeats = 2,
classProbs = TRUE)

ctree.installed<- train(TARGET ~ OPENING_BALANCE+ MONTHS_SINCE_EXPEDITION+
RS_DESC+SAP_STATUS+ ACTIVATION_STATUS+ ROTUL_STATUS+
SIM_STATUS+ RATE_PLAN_SEGMENT_NORM,
data=trainSet,
method = "ctree",
trControl = cvCtrl)

但是,我的变量 OPENING_BALANCEMONTHS_SINCE_EXPEDITION有一些缺失值,因此该函数不起作用。我不明白为什么会发生这种情况,因为我正在尝试 build 一棵树。当我使用其他软件包时不会发生此问题。

这是错误:
Error in na.fail.default(list(TARGET = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,  : 
missing values in object

我不想使用 na.action=pass因为我真的不想丢弃这些观察结果。

难道我做错了什么?为什么会这样?您对此有什么建议吗?

最佳答案

我开始考虑数据集 PimaIndiansDiabetes2mlbench有一些缺失值的包。

data(PimaIndiansDiabetes2, package = "mlbench")
head(PimaIndiansDiabetes2)

pregnant glucose pressure triceps insulin mass pedigree age diabetes
1 6 148 72 35 NA 33.6 0.627 50 pos
2 1 85 66 29 NA 26.6 0.351 31 neg
3 8 183 64 NA NA 23.3 0.672 32 pos
4 1 89 66 23 94 28.1 0.167 21 neg
5 0 137 40 35 168 43.1 2.288 33 pos
6 5 116 74 NA NA 25.6 0.201 30 neg

train我设置了 na.actionna.pass (这导致返回数据集不变)然后设置 maxsurrogate ctree 中的参数:
library(caret)
cvCtrl <- trainControl(method="repeatedcv", repeats = 2, classProbs = TRUE)
set.seed(1234)
ctree1 <- train(diabetes ~ ., data=PimaIndiansDiabetes2,
method = "ctree",
na.action = na.pass,
trControl = cvCtrl,
controls=ctree_control(maxsurrogate=2))

结果是:
print(ctree1)
Conditional Inference Tree

392 samples
8 predictor
2 classes: 'neg', 'pos'

No pre-processing
Resampling: Cross-Validated (10 fold, repeated 2 times)
Summary of sample sizes: 691, 692, 691, 691, 691, 691, ...
Resampling results across tuning parameters:

mincriterion Accuracy Kappa
0.01 0.7349111 0.4044195
0.50 0.7485731 0.4412557
0.99 0.7323906 0.3921662

Accuracy was used to select the optimal model using the largest value.
The final value used for the model was mincriterion = 0.5.

关于r - train() 函数中的缺失值错误 树木的插入符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43655544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com