time ROC代码

一、绘制符合ggplot2风格的图片,可以加theme

1、先定义一个函数,生成timeROC对象,注意数据集和相应列名需要修改

library(survivalROC)
## Define a helper functio nto evaluate at various t
survivalROC_helper <- function(t) {
    survivalROC(Stime        = ovarian$futime,
                status       = ovarian$fustat,
                marker       = ovarian$lp,
                predict.time = t,
                method       = "NNE",
                span = 0.25 * nrow(ovarian)^(-0.20))
}

2、计算每180天的ROC参数,具体时间可以修改,传入数据为向量

## Evaluate every 180 days
survivalROC_data <- data_frame(t = 180 * c(1,2,3,4,5,6)) %>%
    mutate(survivalROC = map(t, survivalROC_helper),
           ## Extract scalar AUC
           auc = map_dbl(survivalROC, magrittr::extract2, "AUC"),
           ## Put cut off dependent values in a data_frame
           df_survivalROC = map(survivalROC, function(obj) {
               as_data_frame(obj[c("cut.values","TP","FP")])
           })) %>%
    dplyr::select(-survivalROC) %>%
    unnest() %>%
    arrange(t, FP, TP)

3、画图

## Plot
survivalROC_data %>%
    ggplot(mapping = aes(x = FP, y = TP)) +
    geom_point() +
    geom_line() +
    geom_label(data = survivalROC_data %>% dplyr::select(t,auc) %>% unique,
               mapping = aes(label = sprintf("%.3f", auc)), x = 0.5, y = 0.5) +
    facet_wrap( ~ t) +
    theme_bw() +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
          legend.key = element_blank(),
          plot.title = element_text(hjust = 0.5),
          strip.background = element_blank())

二、用timeROC包画,便于事后各种对比

library(timeROC)
time_roc_1 <-  timeROC(
  T = data_ti_roc$STIATime,
  delta = data_ti_roc$STIA,
  marker = data_ti_roc$lp_ti_roc_1, #方向相反加个-
  cause = 1,
  weighting="marginal", #uses the Kaplan-Meier
  times = seq(10,60,10),
  ROC = TRUE,
  iid = T
)

library(RColorBrewer)  # 使用包之前,先加载
plot(time_roc_1,time=60,title =FALSE,col = brewer.pal(4,'Dark2')[1])        
plot(time_roc_2,time=60,add=TRUE,col = brewer.pal(4,'Dark2')[2]) 
plot(time_roc_3,time=60,add=TRUE,col = brewer.pal(4,'Dark2')[3]) 
plot(time_roc_4,time=60,add=TRUE,col = brewer.pal(4,'Dark2')[4])
legend("bottomright",c("CHA2DS2VASC","CHA2DS2VASC+PTFV15000","CHA2DS2VASC+LAE","CHA2DS2VASC+PTFV15000+LAE"),col = brewer.pal(4,'Dark2'),lty=1,lwd=2)

三、绘制timeAUC比较曲线

带置信区间的并不好看

## 绘制timeAUC
par(oma = c(0,0,0,0),      #外框据整个绘图区域四周距离,单位是"行"
    mar = c(4,4,0.5,0.5),  #图片四周距外框距离,同上,如果用omi或mri,单位是英寸,不算四周文字
    mgp = c(1.7,0.5,0),    #三个数字,分别为坐标标题到坐标轴距离,坐标数字到轴距离,ticks到轴距离
    cex = 0.8            #字体大小,不包括lengend
    #tck = -0.01           #ticks大小及方向,负值向外
)
plotAUCcurve(time_roc_1,conf.int=F,col="#00468B99")
legend(x = 20,y=1,c("CHA2DS2VASC"),col=c("#00468B99"),lty=1,lwd=2,cex = 0.8)
plotAUCcurve(time_roc_4,conf.int=F,col="#ED000099",add = T)
legend(x = 20,y=0.95,c("CHA2DS2VASC+PTFV15000+LAE"),col=c("#ED000099"),lty=1,lwd=2,cex = 0.8)

四、绘制timeAUC比较图

##------------------------------------------绘制timeAUC比较---------------------------------------
pdf(file = "time_auc.pdf",width = 4.0,height = 4.0)
## 绘制timeAUC
par(oma = c(0,0,0,0),      #外框据整个绘图区域四周距离,单位是"行"
    mar = c(4,4,0.5,0.5),  #图片四周距外框距离,同上,如果用omi或mri,单位是英寸,不算四周文字
    mgp = c(1.7,0.5,0),    #三个数字,分别为坐标标题到坐标轴距离,坐标数字到轴距离,ticks到轴距离
    cex = 0.8            #字体大小,不包括lengend
    #tck = -0.01           #ticks大小及方向,负值向外
)
plotAUCcurve(time_roc_1,conf.int=F,col="#00468B99")
legend(x = 20,y=1,c("CHA2DS2VASC"),col=c("#00468B99"),lty=1,lwd=2,cex = 0.8)
plotAUCcurve(time_roc_4,conf.int=F,col="#ED000099",add = T)
legend(x = 20,y=0.95,c("CHA2DS2VASC+PTFV15000+LAE"),col=c("#ED000099"),lty=1,lwd=2,cex = 0.8)
dev.off()

五、timeROC面积比较

##---------------------------------timeROC各项参比较----------------------------------------

ci_roc_1 = confint(time_roc_1)
ci_roc_2 = confint(time_roc_2)
ci_roc_3 = confint(time_roc_3)
ci_roc_4 = confint(time_roc_4)

ci_roc_1 <- ci_roc_1$CI_AUC %>% t() %>% data.frame() %>% dplyr::select(t.60)
ci_roc_2 <- ci_roc_2$CI_AUC %>% t() %>% data.frame() %>% dplyr::select(t.60)
ci_roc_3 <- ci_roc_3$CI_AUC %>% t() %>% data.frame() %>% dplyr::select(t.60)
ci_roc_4 <- ci_roc_4$CI_AUC %>% t() %>% data.frame() %>% dplyr::select(t.60)

df_ti_rocs <- data.frame(AUC = c(time_roc_1$AUC['t=60'],time_roc_2$AUC['t=60'],time_roc_3$AUC['t=60'],time_roc_4$AUC['t=60']),
                         lower = c(ci_roc_1[1,1],ci_roc_2[1,1],ci_roc_3[1,1],ci_roc_4[1,1]),
                         upper = c(ci_roc_1[2,1],ci_roc_2[2,1],ci_roc_3[2,1],ci_roc_4[2,1])
)

df_ti_rocs$ORCI <- df_ti_rocs %>% apply(1,function(x){str_c(c(round(x["AUC"],3),"(",round(x["lower"],4),"-",round(x["upper"],4),")"),collapse = "")})

df_ti_rocs <- df_ti_rocs %>% mutate(p = c(NA,
                                          compare(time_roc_2,time_roc_1)$p_values_AUC[[6]],
                                          compare(time_roc_3,time_roc_1)$p_values_AUC[[6]],
                                          compare(time_roc_4,time_roc_1)$p_values_AUC[[6]]
))

write.csv(df_ti_rocs,file = "auc_compare.csv")

六、计算cox模型的Harrell's C-Statistics

##----------------------------------------------计算cox模型的Harrell's  C-Statistics------------------------------------
#BiocManager::install("survcomp")
library(survcomp)

cindex_with_ci <- fit_ti_rocs %>% lapply(function(x){c_index <- concordance.index(predict(x,data),
                                                                                  surv.time = data$STIATime,surv.event = data$STIA %>% as.numeric,method="noether",na.rm = T)
c_index_1 <- c(str_c(c(c_index$c.index %>% round(3),"(",c_index$lower %>% round(3),"-",c_index$upper %>% round(3),")"),collapse = ""))        
}) 
cindex_with_ci

cindex <- fit_ti_rocs %>% lapply(function(x){c_index <- concordance.index(predict(x,data),
                                                                          surv.time = data$STIATime,surv.event = data$STIA %>% as.numeric,method="noether",na.rm = T)
#cindex_with_ci <- cindex_with_ci %>% data.frame()

#c_index_1 <- c_index$c.index %>% round(3)    
})

## C-statistics 比较,比较的是列表,不是单个值
cind_comp_p <- c()
i = 1
while (i < length(cindex)) {
  p <- cindex.comp(cindex[[i+1]],cindex[[1]])
  p <- p[[1]] 
  cind_comp_p <- append(cind_comp_p,p)
  i = i+1
}
cind_comp_p

七、计算并比较cox模型IDI和NRI

##-----------------------------计算cox模型IDI和NRI--------------------------------------------
library(survIDINRI)
#定义时间节点
#t0=365*5
t0=60
## 所有列转成数字
data_ti_roc <- apply(data_ti_roc,2,function(x)as.numeric(x)) %>% data.frame()

#data_ti_roc <- data_ti_roc[1:100,]       ## 先用小样本实验,计算时间很长

## 提取结局列
outcomes = as.matrix(subset(data_ti_roc,select=c(STIATime,STIA)))

##提取变量列,转成矩阵

#data_ti_roc$time = as.integer(data_ti_roc$STIATime)
#data_ti_roc$status = as.numeric(data_ti_roc$STIA)
head(data_ti_roc)
model_1 <- as.matrix(subset(data_ti_roc,select=c("CHA2DS2VASC"))) 
model_2 <- as.matrix(subset(data_ti_roc,select=c("CHA2DS2VASC","PTFV15000"))) 
model_3 <- as.matrix(subset(data_ti_roc,select=c(CHA2DS2VASC,LAE))) 
model_4 <- as.matrix(subset(data_ti_roc,select=c(CHA2DS2VASC,LAE,PTFV15000))) 
head(model_1)
head(model_2)
head(model_3)
head(model_4)
head(data_ti_roc)

##开始对比

x2<-IDI.INF(outcomes,model_1, model_2, t0, npert=1000)
IDI.INF.OUT(x2)
x3<-IDI.INF(outcomes,model_1, model_3, t0, npert=1000)
IDI.INF.OUT(x3)
x4<-IDI.INF(outcomes,model_1, model_4, t0, npert=1000)
IDI.INF.OUT(x4)
#只能手抄结果
#M1表示IDI
#M2表示NRI
#M3表示中位数差异

八、绘制COX校正曲线

##----------------------------制作cox校正曲线-----------------------------
library(rms)

dd<-datadist(data_ti_roc) #设置工作环境变量,将数据整合
options(datadist='dd') #设置工作环境变量,将数据整合
##
##time.in 和 u 要是一样的,都是要评价的时间节点
time_inc <- 60
coxm_1 <- cph(formula_ti_roc_4,data=data_ti_roc,surv=T,x=T,y=T,time.inc = time_inc)
###绘制cox回归生存概率的nomogram图
## 构建Nomo图的对象只能是rms保重d额cph()函数
surv <- Survival(coxm_1) # 构建生存概率函数
nom <- nomogram(coxm_1,fun=list(#function(x)surv(70,1-x),
                                function(x)surv(60,1-x),
                                function(x)surv(36,1-x)),  ##算出不同时间节点生存率值
                ##算出不同时间节点生存率值,显示在列线图上
                funlabel = c("60","36"),
                lp=F
                #fun.at=c('0.9','0.85','0.80','0.70','0.6','0.5','0.4','0.3','0.2','0.1')
)
plot(nom)

##time.in 和 u 要是一样的,都是要评价的时间节点
time_inc <- 60
coxm_1 <- cph(formula_ti_roc_4,data=data_ti_roc,surv=T,x=T,y=T,time.inc = time_inc)
m = (nrow(data_ti_roc)/5) %>% ceiling()   ## m 约等于样本数5分之一
m
cal_1<-calibrate(coxm_1,u=time_inc,cmethod='KM',m= m,B=1000)

pdf("fig5a.pdf",width = 3.9,height = 4.5)

plot(cal_1,lwd=2,lty=1, ##设置线条形状和尺寸
     errbar.col=c(rgb(0,118,192,maxColorValue = 255)), ##设置一个颜色
     xlab='Nomogram-Predicted Probability of 1-year DFS',#便签
     ylab='Actual 1-year DFS(proportion)',#标签
     col=c(rgb(192,98,83,maxColorValue = 255)),#设置一个颜色
     xlim = c(0,1),ylim = c(0,1)) ##x轴和y轴范围
dev.off()

table(data$STIA)

九、制作cox 决策曲线

##-----------------------------制作cox 决策曲线-----------------------------------------
library(ggDCA)
if (!require(ggDCA)) {
  devtools::install_github("yikeshu0611/ggDCA")
}

dca_1 <- dca(fit_ti_roc_3)
dca_4 <- dca(fit_ti_roc_4)
dca <- dca(fit_ti_roc_1,fit_ti_roc_4,times = 60,model.names = c("model1","model4"))
dca_1 <- ggplot(dca,       
                model.names="模型1",
                linetype =F, #线型
                lwd = 0.6)+
  #ylim(c(-0.02,0.05))+
  theme(legend.position = c(0.7,0.8),
        text = element_text(size = 8)
        )+
  scale_color_lancet()
dca_1

ggsave("time_dca.pdf",dca_1,width = 8,height = 8.5,units = "cm")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容