混淆矩阵的灵敏度和特异性长度匹配,但数据的水平不能超过参考值

问题描述 投票:0回答:1

问题:

测量敏感性和特异性所需的混淆矩阵。

问题:

对于混淆矩阵数据,我具有匹配的级别,在混淆矩阵中,数据不能具有比参考更多的级别,因此当我的级别匹配时(例如,列表中的下面),那么关于'数据的错误是什么?不能有比参考更多的水平?参考是'model_prediction'[last length()/ str()。我的因变量是一个因子变量。

尝试的努力:

对于R代码,首先我运行了具有因子结果的预测,并包含了na.action:

loans_predict_fcm <- factor(predict(full, newdata = data_train, type = "response", na.action = na.pass))

具有来自单独table()的结果,例如pred_table;我成功地使用公式计算了灵敏度和特异性。但是,我想与confusionMatrix()交叉确认。但是我无法让confusionMatrix()正常工作。

Sensitivity <- 100*(pred_table[1,1])/sum(pred_table[1,1] + pred_table[1,1])
Specificity <- 100*(pred_table[2,2])/sum(pred_table[2,1] + pred_table[2,2])

[当我尝试使用分解的predict()运行confusionMatrix()时。然后检查级别,发现它们不匹配,这就是为什么对数据失败的confusionMatrix()不能具有比引用更多的级别的原因,在此运行model_prediction。

confusionMatrix(loans_predict_fcm, model_prediction, positive="1")
identical (levels(loans_predict_fcm), levels(model_prediction))
> FALSE
> length(loans_predict_fcm)
[1] 27724
> str(loans_predict_fcm)
 Factor w/ 27424 levels "0.13079979710253",..: 15967 9625 15966 10703 7830 12394 21291 15023 17920 18442 ...
 - attr(*, "names")= chr [1:27724] "11413" "2561" "25337" "1643" ...
> length(loans_train_data$statusRank)
[1] 27724
> str(loans_train_data$statusRank)
 Factor w/ 2 levels "Bad","Good": 2 2 2 1 1 2 2 2 1 2 ...
> length(model_prediction)
[1] 27724
> str(model_prediction)
 Factor w/ 2 levels "Bad","Good": 2 2 2 2 2 2 2 2 2 2 ...
> 
r predict
1个回答
0
投票

对于“混淆矩阵的灵敏度和特异性”问题,列名和行名有所不同,因此我可以使用自定义函数来解决它,该函数基本上是对colnames()和rownames()的setdiff,并创建矩阵向量mat.or.vec()表示缺少的姓氏长度。

© www.soinside.com 2019 - 2024. All rights reserved.