1

Is there a way to calculate Hazard Ratios in Python's Lifeline package relative to a specified group?

What I have at the moment is:

from lifelines import CoxPHFitter

data=KM_DF[["Survival_Time","Event","30_40","40_50","50_60","60_70","70_80","Male"]]

cph=CoxPHFitter()
cph.fit(data,"Survival_Time",event_col="Event",show_progress=True)
cph.print_summary()

I would like to know the HR of the 40_50, 50_60, 60_70, and 70_80 group relative to the 30_40 group while also adjusting for sex. But at the moment, I get a HR of the group I want to be the reference ("30-40") as 3.02.

Is there a way to do this with lifelines?

For greater context/motivation, in R's workflow for Cox Proportional Hazard Ratio calculations, one can set variable "levels", and the subsequent Hazard Ratios are relative to the 1st level. However, I have been unable to sort out if this is possible in the popular "Lifelines" Python package.

1 Answer 1

0

Hopefully still a timely answer.

I have two suggestions here:

  1. use the pandas .set_categories method As I understand, this is similar to how you would set levels in R. And you can set an ordered category list. ie.
>>> ser = pd.Series(["a", "b", "c", "d"])
>>> ser.cat.set_categories(['a', 'b', 'c'], ordered=True)
ser
0   a
1   b
2   c
3   NaN
dtype: category
Categories (3, object): ['a' < 'b' < 'c']

I would collate all the interval groups you mentioned into one column and do the above. For example:

>>> df["intervals"] = pd.Series(["30-40", "40-50", "50-60", "60-70"])
>>> df["intervals"].cat.set_categories(['30-40', '40-50', '50-60'], ordered=True)
0   30-40
1   40-50
2   50-60
...

dtype: category
Categories (3, object): ['30-40' < '40-50' < '50-60']
  1. encode all categories into dummy variables with .get_dummies Drop the baseline category as in this answer.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.