我有一个包含100个样本的数据集,每个样本都有195个突变,它们具有相应的已知临床意义("RealClass")和根据某种预测工具("PredictionValues")的预测值。
在演示中,这是一个与我的数据集具有相同结构的随机数据集:
predictions_100_samples<-as.data.frame(matrix(nrow=19500,ncol=3))
colnames(predictions_100_samples)<-c("Sample","PredictionValues","RealClass")
predictions_100_samples$Sample<-rep(c(1:100), each = 195)
predictions_100_samples$PredictionValues<-sample(seq(0,1,length.out=19500))
predictions_100_samples$RealClass<-rep(c("pathogenic","benign"),each=10)
colours_for_ROC_curves<-rainbow(n=100)我通过PROC包将这100个样本绘制成ROC曲线:
library("pROC")
roc_both <- plot(roc(predictor=predictions_100_samples[1:195,2],response = predictions_100_samples[1:195,3]), col = colours_for_ROC_curves[1],main="100 samples ROC curves",legacy.axes=TRUE,lwd=1)
i=2
for(i in 1:100){
set.seed(500)
roc_both <- plot(roc(predictor=predictions_100_samples[(((i-1)*195)+1):(i*195),2],response = predictions_100_samples[(((i-1)*195)+1):(i*195),3]), col = colours_for_ROC_curves[i], add = TRUE,lwd=1)
i=i+1
}这就是最终图的样子:

现在,我想将所有100条绘制的ROC曲线的平均ROC曲线添加到同一图中。我试着在我编写的循环中使用通过"roc“函数为每个阈值计算的灵敏度和特异度(可以通过roc_both$sensitivities,roc_both$specificities,roc_both$thresholds实现)
但主要的问题是,所选择的阈值是随机的,并且在我绘制的100条ROC曲线上不相等,因此我无法手动计算平均ROC曲线。
有没有不同的软件包可以让我生成多个ROC曲线的平均ROC曲线?或者,有没有一个软件包允许手动设置计算敏感度和特异度的阈值,以便稍后能够计算平均ROC曲线?你对我的问题有没有不同的解决方案?
谢谢!
发布于 2018-10-01 23:56:28
您可以使用cutpointr通过oc_manual函数手动指定阈值。我对数据生成进行了一些修改,以便ROC曲线看起来更好一些。
我们将相同的阈值序列应用于所有样本,并取每个阈值的敏感度和特异度的平均值,以获得“平均ROC曲线”。
predictions_100_samples <- data.frame(
Sample = rep(c(1:100), times = 195),
PredictionValues = c(rnorm(n = 9750), rnorm(n = 9750, mean = 1)),
RealClass = c(rep("benign", times = 9750), rep("pathogenic", times = 9750))
)
library(cutpointr)
library(tidyverse)
mean_roc <- function(data, cutoffs = seq(from = -5, to = 5, by = 0.5)) {
map_df(cutoffs, function(cp) {
out <- cutpointr(data = data, x = PredictionValues, class = RealClass,
subgroup = Sample, method = oc_manual, cutpoint = cp,
pos_class = "pathogenic", direction = ">=")
data.frame(cutoff = cp,
sensitivity = mean(out$sensitivity),
specificity = mean(out$specificity))
})
}
mr <- mean_roc(predictions_100_samples)
ggplot(mr, aes(x = 1 - specificity, y = sensitivity)) +
geom_step() + geom_point() +
theme(aspect.ratio = 1)

您可以使用cutpointr绘制单独的ROC曲线和相加的平均ROC曲线,方法如下:
cutpointr(data = predictions_100_samples,
x = PredictionValues, class = RealClass, subgroup = Sample,
pos_class = "pathogenic", direction = ">=") %>%
plot_roc(display_cutpoint = F) + theme(legend.position="none") +
geom_line(data = mr, mapping = aes(x = 1 - specificity, y = sensitivity),
color = "black")

或者,您可能希望研究汇总ROC曲线(SROC)的理论,以拟合组合多个ROC曲线的参数模型。
https://stackoverflow.com/questions/52467915
复制相似问题