在过去的几天中,我一直在分析R实现随机森林的性能和可用的不同工具,以便获得:
因此,我使用了两种不同的方法:
关键是我已经意识到这两种方法之间有一些不同。
我开发了以下代码:
suppressMessages(library(randomForest))
suppressMessages(library(pROC))
suppressMessages(library(caret))
set.seed(100)
t_x <- as.data.frame(matrix(runif(100),ncol=10))
t_y <- factor(sample(c("A","B"), 10, replace = T), levels=c("A","B"))
v_x <- as.data.frame(matrix(runif(50),ncol=10))
v_y <- factor(sample(c("A","B"), 5, replace = T), levels=c("A","B"))
model <- randomForest(t_x, t_y, ntree=1000, importance=T);
prob.out <- predict(model, v_x, type="prob")[,1];
prediction.out <- predict(model, v_x, type="response");
mroc <- roc(v_y,prob.out,plot=F)
results <- coords(mroc,seq(0, 1, by = 0.01),input=c("threshold"),ret=c("sensitivity","specificity","ppv","npv"))
accuracyData <- confusionMatrix(prediction.out,v_y)如果您比较结果和变量,您会发现敏感性和特异性之间的关系正好相反。
也就是说,confusionMatrix的结果是:
Confusion Matrix and Statistics
Reference
Prediction A B
A 1 1
B 2 1
Accuracy : 0.4
95% CI : (0.0527, 0.8534)
No Information Rate : 0.6
P-Value [Acc > NIR] : 0.913
Kappa : -0.1538
Mcnemar's Test P-Value : 1.000
Sensitivity : 0.3333
Specificity : 0.5000
Pos Pred Value : 0.5000
Neg Pred Value : 0.3333
Prevalence : 0.6000
Detection Rate : 0.2000
Detection Prevalence : 0.4000
Balanced Accuracy : 0.4167
'Positive' Class : A 但是,如果我在计算和弦时寻找这样的敏感性和特异性,我会发现它们被交换了:
sensitivity specificity ppv npv
0.32 0.5 0.3333333 0.3333333 0.5000000显然,敏感性和特异性在同系物和confusionMatrix中是相反的。
考虑到confusionMatrix正确地识别了阳性类,我假设这是对敏感性和特异性的良好解释。
我的问题是:有没有办法强迫和弦以我想要的方式来解释正负两类?
发布于 2016-10-04 13:18:30
如果您查看confusionMatrix的输出,可以看到以下内容:
'Positive' Class : A 现在看看mroc,B类被看作是正类:
Data: prob.out in 3 controls (v_y A) < 2 cases (v_y B).基本上,pROC认为你的因素的水平是消极的,正的,而caret则恰恰相反。您可以使用pROC显式地指定您的级别以获得相同的行为:
mroc <- roc(v_y,prob.out,plot=F, levels = c("B", "A"))或者取决于您喜欢的行为,使用positive参数confusionMatrix
accuracyData <- confusionMatrix(prediction.out,v_y, positive = "B")发布于 2016-10-04 13:42:28
尝试一下,使用这两种方法就可以得到相同的结果(这都是关于正负类因素级别的):
accuracyData <- confusionMatrix(prediction.out,v_y, positive='A')
accuracyData
Confusion Matrix and Statistics
Reference
Prediction A B
A 1 0
B 2 2
Accuracy : 0.6
95% CI : (0.1466, 0.9473)
No Information Rate : 0.6
P-Value [Acc > NIR] : 0.6826
Kappa : 0.2857
Mcnemar's Test P-Value : 0.4795
Sensitivity : 0.3333
Specificity : 1.0000
Pos Pred Value : 1.0000
Neg Pred Value : 0.5000
Prevalence : 0.6000
Detection Rate : 0.2000
Detection Prevalence : 0.2000
Balanced Accuracy : 0.6667
'Positive' Class : A
mroc <- roc(v_y,prob.out,plot=F, levels=c("B", "A"))
results <- coords(mroc, 0.49, "threshold", ret=c("specificity", "sensitivity", "accuracy",
"tn", "tp", "fn", "fp", "npv", "ppv", "1-specificity",
"1-sensitivity", "1-accuracy", "1-npv", "1-ppv"))
results
specificity sensitivity accuracy tn tp fn fp npv ppv 1-specificity
1.0000000 0.3333333 0.6000000 2.0000000 1.0000000 2.0000000 0.0000000 0.5000000 1.0000000 0.0000000
1-sensitivity 1-accuracy 1-npv 1-ppv
0.6666667 0.4000000 0.5000000 0.0000000 https://stackoverflow.com/questions/39851675
复制相似问题