我有以下用于老兵数据的示例代码
# 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