gpt4 book ai didi

r - 在 R 中计算不同阈值的敏感性、特异性、NPV 和 PPV

转载 作者:行者123 更新时间:2023-12-02 01:03:54 30 4
gpt4 key购买 nike

我使用以下代码使用 RandomForest 作为分类器计算敏感性、特异性、NPV 和 PPV。

   suppressMessages(require(randomForest));
classifier <- randomForest(x.train,y.train,ntree=300,importance=T)
prediction <<- predict(classifier,x.test,type="response")

suppressMessages(require(caret));
accuracyData <- confusionMatrix(prediction,y.test)

accuracyData 中,我拥有有关预测质量(灵敏度、特异性等)的所有信息。

无论如何,我想针对不同的阈值进行计算,但我不知道如何在我的代码中指定这样的值。

最佳答案

问题在于,当您预测“响应”时,您正在做出二分法的决定,并且您正在丢失有关您的不确定性的信息。那时已经应用了一个阈值来做出决定。如果你想尝试不同的阈值,你应该输出响应的概率。例如

#sample data
set.seed(15)
x<- matrix(runif(100,0,5), ncol=1)
y<- 3-2*x[,1] + rnorm(100, 2, 2)
y<- factor(ifelse(y>median(y), "A","B"))

x.train<-x[1:50,, drop=F]
y.train<-y[1:50]

x.test<-x[-(1:50),,drop=F]
y.true<-y[-(1:50)]

#fit the model
library(randomForest)
classifier <- randomForest(x.train,y.train,ntree=500,importance=T)
prediction <- predict(classifier,x.test, type="prob")

#calculate performance
library(pROC)
mroc<-roc(y.true, prediction[,1], plot=T)

enter image description here

然后我们可以计算不同阈值的兴趣值

coords(mroc, .5, "threshold", ret=c("sensitivity","specificity","ppv","npv"))
# sensitivity specificity ppv npv
# 0.7586207 0.8095238 0.8461538 0.7083333

coords(mroc, .9, "threshold", ret=c("sensitivity","specificity","ppv","npv"))
# sensitivity specificity ppv npv
# 0.9655172 0.6666667 0.8000000 0.9333333

关于r - 在 R 中计算不同阈值的敏感性、特异性、NPV 和 PPV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043409/

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