使用游侠R包计算Brier分数和综合Brier分数

时间:2017-07-14 14:05:48

标签: r machine-learning regression random-forest survival-analysis

我想使用“ranger”R包计算Brier分数和我的分析的综合Brier分数。

作为一个例子,我使用“生存”包中的退伍军人数据如下

install.packages("ranger")
library(ranger)
install.packages("survival")
library(survival)
#load veteran data
data(veteran)
data <- veteran
# training and test data
n <- nrow(data)
testind <- sample(1:n,n*0.7)
trainind <- (1:n)[-testind]
#train ranger
rg <- ranger(Surv(time, status) ~ ., data = data[trainind,])
# use rg to predict test data
pred <- predict(rg,data=data[testind,],num.trees=rg$num.trees)
#cummulative hazard function for each sample
pred$chf
#survival probability for each sample
pred$survival

如何计算Brier分数和综合Brier分数?

1 个答案:

答案 0 :(得分:2)

可以使用pec包的predictSurvProb函数计算综合布里尔分数(IBS),但您需要定义ranger命令以从{{?pec:::predictSurvProb提取生存概率预测1}}建模方法(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(newdata), " x ", length(times), "\nProvided prediction matrix: ", NROW(p), " x ", NCOL(p), "\n\n", sep = "")) p } 表示可用模型列表) 可能的解决方案是:

library(ranger)
library(survival)
data(veteran)
dts <- veteran
n <- nrow(dts)
set.seed(1)
testind <- sample(1:n,n*0.7)
trainind <- (1:n)[-testind]
rg <- ranger(Surv(time, status) ~ ., data = dts[trainind,])

# A formula to be inputted into the pec command
frm <- as.formula(paste("Surv(time, status)~",
       paste(rg$forest$independent.variable.names, collapse="+")))

library(pec)
# Using pec for IBS estimation
PredError <- pec(object=rg,
    formula = frm, cens.model="marginal",
    data=dts[testind,], verbose=F, maxtime=200)

此功能可以按如下方式使用:

print.pec

可以使用times命令评估IBS,在print(PredError, times=seq(10,200,50)) # ... # Integrated Brier score (crps): # # IBS[0;time=10) IBS[0;time=60) IBS[0;time=110) IBS[0;time=160) # Reference 0.043 0.183 0.212 0.209 # ranger 0.041 0.144 0.166 0.176 中指示显示IBS的时间点:

ELSE