免费手机网站空间申请,三杰网站建设,网站搜索引擎推广,公司网站模板设计目录
ROC基础
生成模拟数据
率的计算
R语言计算测试
ROCR#xff1a;
pROC
ROC绘制
单个ROC
两个ROC
Logistic回归的ROC曲线
timeROC ROC基础
ROC曲线的横坐标是假阳性率#xff0c;纵坐标是真阳性率#xff0c;需要的结果是这个率表示疾病阳性的率#xff08;…目录
ROC基础
生成模拟数据
率的计算
R语言计算测试
ROCR
pROC
ROC绘制
单个ROC
两个ROC
Logistic回归的ROC曲线
timeROC ROC基础
ROC曲线的横坐标是假阳性率纵坐标是真阳性率需要的结果是这个率表示疾病阳性的率而不能误算为阴性的率。
ROC阳性结果还是阴性结果_roc曲线计算阳性阴性预测值-CSDN博客
生成模拟数据
rm(list ls())
set.seed(1234)
#模拟数据
ca125 - c(rnorm(10,80,20),rnorm(20,50,10))
##前10个均值为80标准差为20的正态分布数据后20个均值为50标准差为10的正态分布随机数
group - c(rep(c(肿瘤,非肿瘤),c(10,20)))#
data - data.frame(ca125,group)
head(data) ca125 group
1 55.85869 肿瘤
2 85.54858 肿瘤
3 101.68882 肿瘤
4 33.08605 肿瘤
5 88.58249 肿瘤
6 90.12112 肿瘤
率的计算
#设置ca12560判断为肿瘤
data$pre - ifelse(data$ca125 60,猜他是肿瘤,猜他不是肿瘤)
#列联表
table(data$group,data$pre)猜他不是肿瘤 猜他是肿瘤
非肿瘤 19 1
肿瘤 2 8
根据这个四格表我们就能算出目前的真阳性率和假阳性率
真阳性率猜他是肿瘤猜对的人数 / 所有肿瘤人数 假阳性率猜他是肿瘤猜错的人数 / 所有非肿瘤人数
真阳性率 8 / (28) 0.8 假阳性率 1 / (191) 0.05
如果目标结果是确定非肿瘤患者
假阳性率猜他不是肿瘤猜错的人数 / 所有肿瘤人数 真阳性率猜他不是肿瘤猜对的人数 / 所有非肿瘤人数
假阳性率 2 / (28) 0.2 真阳性率 19 / (191) 0.95
R语言计算测试
R语言计算AUCROC曲线的注意事项_r语言auc-CSDN博客
ROCR
ROCR默认计算顺序靠后的类别的AUC。如果提供给labels的值是有序因子型变量则排在前面的默认是阴性结果negtive排在后面的默认是阳性结果positive默认计算阳性结果排序靠后的AUC。如果是无序因子、数值、字符、逻辑型变量会按照R语言的默认排序比如按照数字大小、首字母顺序等也是计算排序靠后的类别的AUC。 set.seed(20220840)
ca125_1 - c(rnorm(10,80,20),rnorm(20,50,10))
ca125_2 - c(rnorm(10,20,20),rnorm(20,70,10))
classc(rep(1:0,c(10,20)))tumor - c(rep(c(癌症,非癌症),c(10,20)))df - data.frame(classclass,ca125_1ca125_1,ca125_2ca125_2,tumortumor)
library(ROCR)pred - prediction(predictions ca125_1, # 预测指标labels tumor # 真实结果)performance(pred, auc)y.values[[1]]
# [1] 0.075
这里我们想计算癌症的AUC而不是非癌症的AUC手动指定顺序
pred - prediction(predictions ca125_1, # 预测指标labels tumor # 真实结果,label.ordering c(非癌症,癌症) # 此时就是计算癌症的AUC)performance(pred, auc)y.values[[1]]
## [1] 0.925
pROC
pROC包计算AUC也需要用来预测结果的指标以及真实结果。
这个包计算pROC略有不同它是根据中位数来的谁的中位数大就计算谁的AUC比如我们的这个例子计算下中位数看看 ca125_1
# 把ca125_1按照tumor的两个类别进行分组然后分别计算中位数
tapply(ca125_1, tumor, median)
## 癌症 非癌症
## 81.34426 49.99926肿瘤的均值大library(pROC)
roc(responsetumor, predictorca125_1)
#Area under the curve: 0.925计算结果为癌症的
ca125_2是计算非癌症的AUC。
tapply(ca125_2, tumor, median)
## 癌症 非癌症
## 13.52771 69.69272非肿瘤的平均数大roc(responsetumor, predictorca125_2)
Data: ca125_2 in 10 controls (tumor 癌症) 20 cases (tumor 非癌症).
Area under the curve: 0.9
#需要设置levels和direction
# 此时计算的就是癌症的AUC
roc(responsetumor, predictorca125_2,levelsc(非癌症, 癌症), # 这个顺序随便设定重要的是directiondirection # 手动设定非癌症 癌症
)
#Data: ca125_2 in 20 controls (tumor 非癌症) 10 cases (tumor 癌症).
#Area under the curve: 0.1
ROC绘制
各章示例代码/Chapter13 临床诊断实验评价.R · 杨敏迪/Analysis of Medical data by R language - 码云 - 开源中国 (gitee.com)
单个ROC
数据为动脉瘤性蛛网膜下腔出血患者的检测数据和预后s100b是一个血清指标outcome根据格拉斯哥评分分为good4-5分这里最大为5 、poor1-3分 rm(list ls())
library(pROC)
data(aSAH)
roc1 - roc(outcome ~ s100b, data aSAH)
attributes(roc1)#查看结果包含内容
roc1$auc#
#Area under the curve: 0.7314 #求约登指数roc.result - data.frame(threshold roc1$thresholds,sensitivity roc1$sensitivities,specificity roc1$specificities)View(roc.result)roc.result$youden - roc.result$sensitivity roc.result$specificity - 1head(roc.result)threshold sensitivity specificity youden
1 -Inf 1.0000000 0.00000000 0.00000000
2 0.035 0.9756098 0.00000000 -0.02439024
3 0.045 0.9756098 0.06944444 0.04505420
4 0.055 0.9756098 0.11111111 0.08672087
5 0.065 0.9756098 0.13888889 0.11449864
6 0.075 0.9024390 0.22222222 0.12466125#找出约登指数最大的一行which.max(roc.result$youden)
[1] 18roc.result[18, ]threshold sensitivity specificity youden
18 0.205 0.6341463 0.8055556 0.4397019
绘图
?plot.roc#查看参数细节
plot.roc(roc1, print.auc TRUE, auc.polygon TRUE,grid c(0.1,0.2), grid.col c(green,red),auc.polygon.col lightblue, print.thres TRUE)
#AUC的置信区间——DeLong法
ci.auc(roc1) 两个ROC
roc1 - roc(aSAH$outcome, aSAH$s100b)
roc2 - roc(aSAH$outcome, aSAH$ndka)
#DeLong非参数方法Venkatraman回归模型法bootstrap重抽样法
#默认为DeLong法
#默认为两组相关检测结果AUC的比较
#独立参数paired FALSE
roc.test(roc1,roc2)
#绘图
plot(roc1)
lines(roc2, col red)
test - roc.test(roc1, roc2)
text(0.5,0.5, labels paste(p-value ,round(test$p.value, 3)))
legend(bottomright,legend c(S100b, NDKA),col c(1,red), lwd 2)Logistic回归的ROC曲线
二分类变量 data infert
#建立Logistic回归模型
fit - glm(case ~ induced spontaneous, family binomial, data infert)
library(epiDisplay)
logistic.display(fit)
#ROC绘制
lroc(fit, line.col red, lwd 2) timeROC
如何绘制时间依赖性ROC曲线 (qq.com)
参考
1《R语言医学数据分析实战》
2R语言计算AUCROC曲线的注意事项_r语言auc-CSDN博客
3ROC阳性结果还是阴性结果_roc曲线计算阳性阴性预测值-CSDN博客
4各章示例代码/Chapter13 临床诊断实验评价.R · 杨敏迪/Analysis of Medical data by R language - 码云 - 开源中国 (gitee.com)
5如何绘制时间依赖性ROC曲线 (qq.com) 文章转载自: http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.ydnxm.cn.gov.cn.ydnxm.cn http://www.morning.lzttq.cn.gov.cn.lzttq.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.cwpny.cn.gov.cn.cwpny.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.gbqgr.cn.gov.cn.gbqgr.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.bfysg.cn.gov.cn.bfysg.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.rcqyk.cn.gov.cn.rcqyk.cn http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.kaakyy.com.gov.cn.kaakyy.com http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.xqwq.cn.gov.cn.xqwq.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.cwgn.cn.gov.cn.cwgn.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.qzpsk.cn.gov.cn.qzpsk.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.tbplf.cn.gov.cn.tbplf.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn