r - R 中的 ROC 曲线与生存树的 rpart
问题描述
我在为 rpart 包创建的生存树创建 ROC 曲线时遇到问题。我的目标是通过 ROC 曲线中的曲线下面积 (AUC) 来评估我的生存树。我尝试了很多方法来绘制 ROC 曲线,但都失败了。我怎样才能接近我的下一步 ROC 曲线图?
这是我到目前为止的R代码:
library(survival)
library("rpart")
library("partykit")
library(rattle)
library(rpart.plot)
temp = coxph(Surv(pgtime, pgstat) ~ age+eet+g2+grade+gleason+ploidy, stagec)
newtime = predict(temp, type = 'expected')
fit <- rpart(Surv(pgtime, pgstat) ~ age+eet+g2+grade+gleason+ploidy, data = stagec)
fancyRpartPlot(fit)
tfit <- as.party(fit) #Transfer "rpart" to "party"
predtree<-predict(tfit,newdata=stagec,type="prob") #Prediction
这是我到目前为止尝试过的 R 代码:
1.
library("ROCR")
predROCR <- prediction(predict(tfit, newdata = stagec, type = "prob")[, 2],labels=Surv(stagec$pgtime, stagec$pgstat))
Error in predict(tfit, newdata = stagec, type = "prob")[, 2] :
incorrect number of dimensions
它不起作用。我检查了predict()的一个函数的预测结果,发现它是一个'Survival'对象(代码和结果如下:)。我猜这个方法失败是因为它不适合“生存”对象?
predict(tfit, newdata = stagec, type = "prob")[[1]]
Call: survfit(formula = y ~ 1, weights = w, subset = w > 0)
n events median 0.95LCL 0.95UCL
33 1 NA NA NA
我尝试推导出每个终端节点的生存函数值,并使用这些值绘制 ROC 曲线。这个对吗?以这种方式绘制的 ROC 曲线似乎将预测的分类结果视为连续变量而不是分类变量。
这是我尝试的 R 代码:
tree2 = fit
tree2$frame$yval = as.numeric(rownames(tree2$frame))
#Get the survival function value of each sample
Surv_value = data.frame(predict(tree2, newdata=stagec,type = "matrix"))[,1]
Out=data.frame()
Out=cbind(stagec,Surv_value)
#ROC
library(survivalROC)
roc=survivalROC(Stime=Out$pgtime, status=Out$pgstat, marker = Out$Surv_value, predict.time =5, method="KM")
roc$AUC #Get the AUC of ROC plot
#Plot ROC
aucText=c()
par(oma=c(0.5,1,0,1),font.lab=1.5,font.axis=1.5)
plot(roc$FP, roc$TP, type="l", xlim=c(0,1), ylim=c(0,1),col="#f8766d",
xlab="False positive rate", ylab="True positive rate",
lwd = 2, cex.main=1.3, cex.lab=1.2, cex.axis=1.2, font=1.2)
aucText=c(aucText,paste0("498"," (AUC=",sprintf("%.3f",roc$AUC),")"))
legend("bottomright", aucText,lwd=2,bty="n",col=c("#f8766d","#00bfc4","blue","green"))
abline(0,1)
解决方案
推荐阅读
- libgdx - 如何正确使用 LIBGDX FrameBuffer
- function - 谷歌表格脚本为整个列添加百分比
- sql-server - 从 VS2008 连接到 SQL Server 2017
- typescript - '巢中间件
' 不能分配给 'Function | 类型的参数 类型 ' - javascript - 为什么我无法设置属性 'innerHTML' of null 错误,任何人都知道这个答案
- android - 在根 Android 上的 SQLite 数据库中没有条目
- java - JsonArray:如何只更新一个字段并保存回同一个文件
- javascript - 如何在 prod 模式下解决此 AJAX 错误 500?
- javascript - JavaScript 通过数组中的值查找键
- sql - 获取所有与每个过失代码的最大违规数量相等的驾驶执照号码