library(tidyverse)#fordatamanipulationlibrary(pkgsearch)#forsearchingpackages
找与ROC相关的包该包会提供一系列关于感兴趣主题的R包,包括他们的评分,作者,连接等等ps函数等价于pkg_search
size:定义返回结果数量
format="short"返回格式Sys.setlocale('LC_ALL','C')
rocPkg
head(rocPkg)
class(rocPkg)
[1]"C"
-"ROC"-------------------------------------74packagesin0.01seconds-
#packageversion
1100pROC1.15.0
244caTools1.17.1.2
318survivalROC1.0.3
418PRROC1.3.1
515plotROC2.2.1
614precrec0.10.1
by@
XavierRobin2M
ORPHANED4M
ParamitaSaha-Chaudhuri7y
JanGrau1y
MichaelC.Sachs1y
TakayaSaito3M
[1]"pkg_search_result""tbl_df""tbl"
[4]"data.frame"
ROCR包performance函数计算tpr,fprlibrary(ROCR)
data(ROCR.simple)
df
head(df)
##predictionslabels
##10.61254781
##20.36427101
##30.43213610
##40.14029110
##50.38489590
##60.24441551
pred
perf
perf
plot(perf,colorize=TRUE)image.png
plotROC包-ggplot绘制ROC曲线ROC曲线用于评估连续测量的精度,以预测二进制结果。在医学上,ROC曲线用于评价放射学和一般诊断的诊断试验有着悠久的历史。ROC曲线在信号检测理论中也有很长的应用历史。require(plotROC)
提供网页版操作,为了代码的连贯性,这里不介绍网页版,不可能我们分析到一般导出数据,拿到网页版去操作。基本用法set.seed(2529)
D.ex
M1
M2
test
M1=M1,M2=M2,stringsAsFactors=FALSE)
head(test)
##DD.strM1M2
##11Ill1.48117155-2.50636605
##21Ill0.619944781.46861033
##30Healthy0.576133450.07532573
##41Ill0.854331972.41997703
##50Healthy0.052583420.01863718
##61Ill0.667039890.24732453geom_roc绘图d为编码1/0, m为用于预测的值marker
注意需要一个disease code,不一定是1/0,但最后选择编码为1/0
如不1/0,则stat_roc默认按顺序最低值为无病状态basicplot
basicplotimage.png若diseaase编码非1/0:提示warning但仍能继续ggplot(test,aes(d=D.str,m=M1))+geom_roc()image.pngn.cuts参数:展示几个截断点
labelsize: 展示标签的大小
labelround: label值保留几位小数ggplot(test,aes(d=D,m=M1))+geom_roc(n.cuts=5,labelsize=5,labelround=2)修改style-style_roc函数styledplot
styledplotimage.png修改xlab, 主题basicplot+style_roc(theme=theme_grey,xlab="1-Specificity")image.png
multiROC-多因素诊断
meltroc类似于dplyr中的gather。转换数据为长数据,原数据为两列markerhead(test)
##DMname
##M1111.48117155M1
##M1210.61994478M1
##M1300.57613345M1
##M1410.85433197M1
##M1500.05258342M1
##M1610.66703989M1
longtest
head(longtest)
table(longtest$name)
##ROC曲线比较
ggplot(longtest,aes(d=D,m=M,color=name))+
geom_roc()+
style_roc()+
ggsci::scale_color_lancet()image.pngggplot2分面ggplot(longtest,aes(d=D,m=M,color=name))+
geom_roc()+
style_roc()+
facet_wrap(~name)+
ggsci::scale_color_lancet()image.png主题与注释AUC计算并绘制在图中-calc_auc函数
calc_auc(basicplot)$AUC提取basicplot+
style_roc(theme=theme_grey)+##主题修改
theme(axis.text=element_text(colour="blue"))+
ggtitle("Themesandannotations")+##标题
annotate("text",x=.75,y=.25,##注释text的位置
label=paste("AUC=",round(calc_auc(basicplot)$AUC,2)))+
scale_x_continuous("1-Specificity",breaks=seq(0,1,by=.1))##x刻度
##Scalefor'x'isalreadypresent.Addinganotherscalefor'x',whiimage.png对multi_ROC注释,实现多个AUC值的呈现,实际上仍然是ggplot2语法中的annotate注释p
geom_roc(n.cuts=0)+
style_roc()+
ggsci::scale_color_lancet()
auc
head(auc)
##PANELgroupAUC
##1110.833985
##2120.679599
p+annotate("text",x=.75,y=.25,##注释text的位置
label=paste("AUCofM1=",round(calc_auc(p)$AUC[1],2)))+
annotate("text",x=.75,y=.15,##注释text的位置)
label=paste("AUCofM2=",round(calc_auc(p)$AUC[2],2)))
其它计算ROC曲线的算法融入默认的calculate_roc 计算的是 empirical ROC曲线
只要有cutoff, TPF,FPF即可计算,将这些结果以数据框的形式传入到 ggroc 函数
代替默认的统计方法为identityrequire(plotROC)
require(ggplot2)
set.seed(2529)
D.ex
M1
M2
test
M1=M1,M2=M2,stringsAsFactors=FALSE)
head(test)
##DD.strM1M2
##11Ill1.48117155-2.50636605
##21Ill0.619944781.46861033
##30Healthy0.576133450.07532573
##41Ill0.854331972.41997703
##50Healthy0.052583420.01863718
##61Ill0.667039890.24732453
D.ex
M.ex
mu1
mu0
s1
s0
c.ex
##构造数据框传入数据
binorm.roc
FPF=pnorm((mu0-c.ex)/s0),
TPF=pnorm((mu1-c.ex)/s1)
)
head(binorm.roc)
binorm.plot
geom_roc(stat="identity")+style_roc(theme=theme_grey)
binorm.plotimage.png
时间依赖的ROC曲线配合survival ROC包
配合lapply函数实现批量绘图
lappy的结果返回为list,刚好输入do.callrequire(ggplot2)
require(plotROC)
library(survivalROC)
survT
cens
M
###时间2,5,10
sroc
stroc
predict.time=t,method="NNE",##KM法或NNE法
span=.25*350^(-.2))
data.frame(TPF=stroc[["TP"]],FPF=stroc[["FP"]],
c=stroc[["cut.values"]],
time=rep(stroc[["predict.time"]],length(stroc[["FP"]])))
})
##整合到数据框中
sroclong
class(sroclong)
##[1]"data.frame"
head(sroclong)
##TPFFPFctime
##111.0000000-Inf2
##210.9970286-96.210912
##310.9940573-89.133152
##410.9910859-80.534022
##510.9881145-70.531042
##610.9851431-67.813922
sroclong$time
##绘制ROC
pROC
geom_roc(labels=FALSE,stat="identity")+
style_roc()+
ggsci::scale_color_jco()
pROCimage.png添加注释pROC+annotate("text",x=.75,y=.25,##注释text的位置
label=paste("AUCof1years=",round(calc_auc(pROC)$AUC[1],2)))+
annotate("text",x=.75,y=.15,##注释text的位置)
label=paste("AUCof3years=",round(calc_auc(pROC)$AUC[2],2)))+
annotate("text",x=.75,y=.05,##注释text的位置)
label=paste("AUCof5years=",round(calc_auc(pROC)$AUC[3],2)))image.png
如果觉得《r roc函数_R语言绘制ROC曲线》对你有帮助,请点赞、收藏,并留下你的观点哦!