测试数据上Cox和随机生存森林模型的生存曲线

时间:2018-07-26 14:40:44

标签: r machine-learning survival-analysis cox-regression

我正在比较Cox和随机生存森林模型在几个数据集上的研究结果。我已经对某些测试数据的方法使用了综合Brier得分,但希望使用测试数据上的生存曲线进一步比较它们。有人可以帮忙如何在测试数据上绘制方法的生存曲线(或适当的方法)?

我有以下用于老兵数据的示例代码

# load the required packages
library(ranger)
library(survival)
library(prodlim)
library(pec)

#load example data
data(veteran)
data <-veteran

n <- nrow(data)
max(data$time)

timepoint <- 200 # a time pont at which to estimate error for test data

# A formula to be inputted into the pec command
frm <- as.formula(paste("Surv(time,status)~",paste(names(data[,-c(3,4)]), collapse="+")))

# divede the data into training and testing parts
trainind <- sample(1:n,n*0.7)
testind <- (1:n)[-trainind]

# the cox model
cox <- coxph(frm,data=data[trainind,])
PredError <- pec(list(Cox=cox),Hist(time,status,cens.code="1")~1,data=data[testind,])
IBS_cox <- crps(object = PredError, times =timepoint, start = PredError$start)[2,1]
IBS_cox

# random survival forest

# a function to make pec to predict for ranger object
predictSurvProb.ranger <- function (object, newdata, times, ...) {ptemp <- ranger:::predict.ranger(object, data = newdata, importance ="none")$survival
pos <- prodlim::sindex(jump.times =object$unique.death.times,eval.times = times)
p <- cbind(1, ptemp)[, pos + 1, drop = FALSE]
if (NROW(p) != NROW(newdata) || NCOL(p) != length(times)) stop(paste("\nPrediction matrix has wrong dimensions:\nRequested newdata x times: ",NROW(dts[trainind,]), " x ", length(1), "\nProvided prediction matrix: ",NROW(p), " x ", NCOL(p), "\n\n", sep = ""))
p
}

rg <- ranger(Surv(time,status) ~ ., data = data[trainind,],num.trees=1000,num.threads = 1)

# Using pec for IBS estimation
PredError <- pec(object=rg, exact==TRUE,formula = frm, cens.model="marginal",data=data[testind,], verbose=F)    
IBS_rsf <- crps(object = PredError, times =timepoint, start = PredError$start)[2,1]
IBS_rsf

0 个答案:

没有答案