Xgboost Cox生存时间输入

时间:2018-11-30 18:15:14

标签: xgboost survival cox

在xgboost 0.81中新实现的cox ph生存模型中,如何指定事件的开始和结束时间?

谢谢

R等价函数例如: coxph = coxph(Surv(开始,停止,状态)〜年龄+性别+ SBP,data = data)

1 个答案:

答案 0 :(得分:2)

XGBoost不允许启动(即延迟进入)。如果对应用程序有意义,则始终可以更改基础时间范围,以便所有主题都从 time = 0 开始。但是,XGBoost确实允许使用正确的审查数据。似乎找不到任何有关如何实现Cox模型的文档/示例,但是从源代码中,您可以阅读“用于删失生存数据的Cox回归(负标签被视为删失)。”

对于想使用 obj =“ survival:cox” 的XGBoost的人来说,这是一个简短的示例。我们可以将结果与scikit-learn生存包 sksurv 进行比较。为了使XGBoost更类似于该框架,我们使用线性增强器而不是树形增强器。

import pandas as pd
import xgboost as xgb
from sksurv.datasets import load_aids
from sksurv.linear_model import CoxPHSurvivalAnalysis

# load and inspect the data
data_x, data_y = load_aids()
data_y[10:15]
Out[586]: 
array([(False, 334.), (False, 285.), (False, 265.), ( True, 206.),
   (False, 305.)], dtype=[('censor', '?'), ('time', '<f8')])

# Since XGBoost only allow one column for y, the censoring information
# is coded as negative values:
data_y_xgb = [x[1] if x[0] else -x[1] for x in data_y]
data_y_xgb[10:15]
Out[3]: [-334.0, -285.0, -265.0, 206.0, -305.0]

data_x = data_x[['age', 'cd4']]
data_x.head()
Out[4]: 
    age    cd4
0  34.0  169.0
1  34.0  149.5
2  20.0   23.5
3  48.0   46.0
4  46.0   10.0

# Since sksurv output log hazard ratios (here relative to 0 on predictors)
# we must use 'output_margin=True' for comparability.
estimator = CoxPHSurvivalAnalysis().fit(data_x, data_y)
gbm = xgb.XGBRegressor(objective='survival:cox',
                       booster='gblinear',
                       base_score=1,
                       n_estimators=1000).fit(data_x, data_y_xgb)
prediction_sksurv = estimator.predict(data_x)
predictions_xgb = gbm.predict(data_x, output_margin=True)
d = pd.DataFrame({'xgb': predictions_xgb,
                  'sksurv': prediction_sksurv})
d.head()
Out[13]: 
     sksurv       xgb
0 -1.892490 -1.843828
1 -1.569389 -1.524385
2  0.144572  0.207866
3  0.519293  0.502953
4  1.062392  1.045287

d.plot.scatter('xgb', 'sksurv')

enter image description here

请注意,这些是对用于拟合模型的相同数据的预测。似乎XGBoost可以正确获取值,但有时可以进行线性转换。我不知道为什么。与 base_score n_estimators 一起玩。也许有人可以添加到此答案中。