在 xgboost 0.81 中 cox ph 生存模型的新实现中,如何指定事件的开始和结束时间?
谢谢
R等效函数例如:
cph_mod = coxph(Surv(Start, Stop, Status) ~ Age + Sex + SBP, data=data)
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')
请注意,这些是对用于拟合模型的相同数据的预测。XGBoost 似乎获得了正确的值,但有时使用线性变换。我不知道为什么。玩转base_score和n_estimators。也许有人可以添加到这个答案。