Python CoxPHFitter to extract hazard ratio and confidence intervals

1.2k Views Asked by At

I am new to survival analysis and I have been reading many research paper where the authors report adjusted (age and gender) and unadjusted hazard ratios along with confidence intervals. I am currently using CoxPHFitter from lifelines python package but I am unable to extract hazard ratios. I have followed many links e.g. https://databricks.com/notebooks/survival_analysis/survival_analysis_03_modeling_hazards.html and https://towardsdatascience.com/survival-analysis-part-a-70213df21c2e but none of them give any details on how to extract hazard ratio along with confidence intervals for adjusted or unadjusted cox regression. Using the "baseline_hazard" does give hazard ratio for the intervals but no confidence interval (I am not sure whether this is the right variable to look at) The "confidence_intervals" provides the confidence intervals of the covariates but I am looking for hazard ratio of the fitted model. Can anyone please help me with this? I am new to this analysis

2

There are 2 best solutions below

2
On BEST ANSWER

The hazard ratios (labelled exp(coef)) and confidence intervals are available in the cph.summary, and in a prettier format with cph.print_summary().

0
On

That's my code for extracting hazard_ratios and the confidence intervals. The extracted hazerd_ratios are in fact exp(coef). Because the cph.confidence_intervals_ function calculate the confedence interval of coef, so we need to use math.exp() function to calculate the confedence interval of exp(coef):

cph = CoxPHFitter(
    # penalizer=0.1
).fit(df_concat, duration_col=col_time, event_col=col_event)
p_value = cph._compute_p_values()
# _, _, p_value, hazard_ratios = cph.print_summary()
c_index = cph.score(df_concat, scoring_method="concordance_index")
hazard_ratios = cph.hazard_ratios_.tolist()
hazard_ratios_ci = cph.confidence_intervals_
print('p_value', p_value)
print('hazard_ratios', hazard_ratios)
if p_value[-1] < 0.05:
    feature_sig.append(pred_factor)
# if p_value[-1] < 0.1:
size = df_result.index.size
try:
    lower = math.exp(hazard_ratios_ci.iloc[-1, 0])
except OverflowError:
    lower = float('inf')
try:
    upper = math.exp(hazard_ratios_ci.iloc[-1, 1])
except OverflowError:
    upper = float('inf')