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

R 计算不同阈值的敏感性、特异性、NPV和PPV,r,classification,random-forest,R,Classification,Random Forest,我使用下面的代码计算敏感性、特异性、NPV和PPV,使用随机森林作为分类器 suppressMessages(require(randomForest)); classifier <- randomForest(x.train,y.train,ntree=300,importance=T) prediction <<- predict(classifier,x.test,type="response") suppressMessages(require

我使用下面的代码计算敏感性、特异性、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)
suppressMessages(需要(随机林));

分类器问题在于,当你预测一个“响应”时,你正在做出一个两分决策,你正在丢失关于你的不确定性的信息。此时,已经应用了一个阈值来做出决策。如果您想尝试不同的阈值,您应该输出响应的概率。比如说

#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)

什么是
x.train
<代码>y.列车
?请添加一些样本数据只需通过
predict
运行候选“x.test”向量,并将其与预测响应进行对比。敏感性和特异性是在特定的切点计算的,所以你可能没有你认为的那么多信息。谢谢!这就是我要找的!
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