bstTree预测的混淆矩阵,错误:';数据必须包含与参考重叠的某些级别;

bstTree预测的混淆矩阵,错误:';数据必须包含与参考重叠的某些级别;,r,prediction,r-caret,confusion-matrix,R,Prediction,R Caret,Confusion Matrix,我正在尝试使用bstTree方法训练一个模型,并打印出混淆矩阵。不利影响是我的职业属性 set.seed(1234) splitIndex <- createDataPartition(attended_num_new_bstTree$adverse_effects, p = .80, list = FALSE, times = 1) trainSplit <- attended_num_new_bstTree[ splitIndex,] testSplit <- attend

我正在尝试使用bstTree方法训练一个模型,并打印出混淆矩阵。不利影响是我的职业属性

set.seed(1234)
splitIndex <- createDataPartition(attended_num_new_bstTree$adverse_effects, p = .80, list = FALSE, times = 1)
trainSplit <- attended_num_new_bstTree[ splitIndex,]
testSplit <- attended_num_new_bstTree[-splitIndex,]

ctrl <- trainControl(method = "cv", number = 5)
model_bstTree <- train(adverse_effects ~ ., data = trainSplit, method = "bstTree", trControl = ctrl)


predictors <- names(trainSplit)[names(trainSplit) != 'adverse_effects']
pred_bstTree <- predict(model_bstTree$finalModel, testSplit[,predictors])


plot.roc(auc_bstTree)

conf_bstTree= confusionMatrix(pred_bstTree,testSplit$adverse_effects)
如何解决此问题

> head(trainSplit)
   type New_missed Therapytypename New_Diesease gender adverse_effects change_in_exposure other_reasons other_medication
5     2          1              14           13      2               0                  0             0                0
7     2          0              14           13      2               0                  0             0                0
8     2          0              14           13      2               0                  0             0                0
9     2          0              14           13      2               1                  0             0                0
11    2          1              14           13      2               0                  0             0                0
12    2          0              14           13      2               0                  0             0                0
   uvb_puva_type missed_prev_dose skintypeA skintypeB Age DoseB DoseA
5              5                1         1         1  22 3.000     0
7              5                0         1         1  22 4.320     0
8              5                0         1         1  22 4.752     0
9              5                0         1         1  22 5.000     0
11             5                1         1         1  22 5.000     0
12             5                0         1         1  22 5.000     0
最大值(预测树)[1]1.03385
最小值(预测树)[1]1.011738

错误说明了一切。绘制ROC只是检查不同阈值点的效果。基于阈值舍入,例如0.7将转换为1(真类),0.3将转换为0(假类);在这种情况下,阈值为0.5。阈值在(0,1)范围内

在您的情况下,无论阈值如何,您都将始终将所有观测值纳入真类,因为即使最小预测值大于1。(这就是为什么@phiver想知道你是否在做回归而不是分类)。如果预测中没有任何零,“预测”中没有与不良影响中的零水平一致的水平,因此该错误


PS:如果您不发布数据,就很难说出错误的根本原因

我也有类似的问题,也就是这个错误。我使用了函数
confusionMatrix

混淆矩阵(实际值、预测值、截止值=0.5)

我得到了以下错误:
confusionMatrix中的错误。默认值(实际值、预测值、截止值=0.5):数据必须包含与参考重叠的某些级别。

我检查了以下几项:

类别(实际)
->数字

类(预测)
->整数

唯一(实际)
->大量值,因为它是概率

唯一(预测)
->2个级别:0和1

我的结论是,应用函数的截止部分存在问题,因此我之前通过以下方式进行了操作:

预测值0.5,1,0)

然后运行
confusionMatrix
函数,该函数现在可以正常工作:


cm看起来您是在预测回归,而不是分类。检查是否将不利影响设置为数据中的一个因子。是的,它是一个包含0和1的因子。即使我在转换为数值后进行预测,我也会得到相同的错误尝试添加数据样本。很难看出问题出在哪里。abhiieor,数据集包含近40000条记录,但88%的数据属于类0,其余属于类1。您提供的数据太少,无法复制。我希望在制作<代码>负面影响时,您已经做了<代码>模型
> head(trainSplit)
   type New_missed Therapytypename New_Diesease gender adverse_effects change_in_exposure other_reasons other_medication
5     2          1              14           13      2               0                  0             0                0
7     2          0              14           13      2               0                  0             0                0
8     2          0              14           13      2               0                  0             0                0
9     2          0              14           13      2               1                  0             0                0
11    2          1              14           13      2               0                  0             0                0
12    2          0              14           13      2               0                  0             0                0
   uvb_puva_type missed_prev_dose skintypeA skintypeB Age DoseB DoseA
5              5                1         1         1  22 3.000     0
7              5                0         1         1  22 4.320     0
8              5                0         1         1  22 4.752     0
9              5                0         1         1  22 5.000     0
11             5                1         1         1  22 5.000     0
12             5                0         1         1  22 5.000     0