Quantcast
Viewing all articles
Browse latest Browse all 14069

How to use a for loop to plot Kaplan Meier with lifelines in python?

I would like to make a Kaplan Meier plot with multiple groups. The code below can show lines for two groups in one plot but the amount of groups is binary. I would like to use a for loop that runs over a list containing all the groups but without a fixed length and > 2. How can I achieve this with lifelines?

from lifelines import KaplanMeierFitterfrom lifelines.datasets import load_waltonswaltons = load_waltons()ix = waltons['group'] == 'control'ax = plt.subplot(111)kmf_control = KaplanMeierFitter()ax = kmf_control.fit(waltons.loc[ix]['T'], waltons.loc[ix]['E'],label='control').plot_survival_function(ax=ax)kmf_exp = KaplanMeierFitter()ax = kmf_exp.fit(waltons.loc[~ix]['T'], waltons.loc[~ix]['E'], label='exp').plot_survival_function(ax=ax)from lifelines.plotting import add_at_risk_countsadd_at_risk_counts(kmf_exp, kmf_control, ax=ax)plt.tight_layout()

Thank you in advance.


Viewing all articles
Browse latest Browse all 14069

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>