def test_plot_km(): if pdf_output: from matplotlib.backends.backend_pdf import PdfPages pdf = PdfPages("test_survfunc.pdf") else: pdf = None sr1 = SurvfuncRight(ti1, st1) sr2 = SurvfuncRight(ti2, st2) fig = plot_survfunc(sr1) close_or_save(pdf, fig) fig = plot_survfunc(sr2) close_or_save(pdf, fig) fig = plot_survfunc([sr1, sr2]) close_or_save(pdf, fig) # Plot the SAS BMT data gb = bmt.groupby("Group") sv = [] for g in gb: s0 = SurvfuncRight(g[1]["T"], g[1]["Status"], title=g[0]) sv.append(s0) fig = plot_survfunc(sv) ax = fig.get_axes()[0] ax.set_position([0.1, 0.1, 0.64, 0.8]) ha, lb = ax.get_legend_handles_labels() fig.legend([ha[k] for k in (0, 2, 4)], [lb[k] for k in (0, 2, 4)], 'center right') close_or_save(pdf, fig) # Simultaneous CB for BMT data ii = bmt.Group == "ALL" sf = SurvfuncRight(bmt.loc[ii, "T"], bmt.loc[ii, "Status"]) fig = sf.plot() ax = fig.get_axes()[0] ax.set_position([0.1, 0.1, 0.64, 0.8]) ha, lb = ax.get_legend_handles_labels() lcb, ucb = sf.simultaneous_cb(transform="log") plt.fill_between(sf.surv_times, lcb, ucb, color="lightgrey") lcb, ucb = sf.simultaneous_cb(transform="arcsin") plt.plot(sf.surv_times, lcb, color="darkgrey") plt.plot(sf.surv_times, ucb, color="darkgrey") plt.plot(sf.surv_times, sf.surv_prob - 2*sf.surv_prob_se, color="red") plt.plot(sf.surv_times, sf.surv_prob + 2*sf.surv_prob_se, color="red") plt.xlim(100, 600) close_or_save(pdf, fig) if pdf_output: pdf.close()
evt = -mn*np.log(np.random.uniform(size=4*n)) # Event times fut = -mf*np.log(np.random.uniform(size=4*n)) # Follow up times y = np.where(evt < fut, evt, fut) # The time that is observed c = (y == evt).astype(np.int) # Censoring indicator (1 if censored) g = np.kron(np.arange(4), np.ones(n)) # Group labels df = pd.DataFrame({"y": y, "c": c, "g": g}) # Estimate the survival functions sf = [] for k, dx in df.groupby("g"): s = sm.SurvfuncRight(dx.y, dx.c) sf.append(s) # Plot the survival function estimates. plt.clf() plot_survfunc(sf) ha, lb = plt.gca().get_legend_handles_labels() leg = plt.figlegend(ha, lb, "center right") leg.draw_frame(False) pdf.savefig() # Simulate data for proportional hazards regression analysis n = 400 xmat = np.random.normal(size=(n, 4)) lp = np.dot(xmat, np.r_[1, -1, 0, 0]) evt = - np.exp(-lp) * np.log(np.random.uniform(size=n)) # Event times fut = -5 * np.log(np.random.uniform(size=n)) # Follow up times y = np.where(evt < fut, evt, fut) # The time that is observed c = (y == evt).astype(np.int) # Censoring indicator (1 if censored) df = pd.DataFrame({"y": y, "c": c, "x0": xmat[:, 0], "x1": xmat[:, 1], "x2": xmat[:, 2], "x3": xmat[:, 3]})