# -*- coding: utf-8 -*- # weibull aft if __name__ == "__main__": import pandas as pd import time from lifelines import WeibullAFTFitter from lifelines.datasets import load_rossi df = load_rossi() df = pd.concat([df] * 1) # df = df.reset_index() # df['week'] = np.random.exponential(1, size=df.shape[0]) wp = WeibullAFTFitter() start_time = time.time() wp.fit(df, duration_col="week", event_col="arrest") print("--- %s seconds ---" % (time.time() - start_time)) wp.print_summary()
import numpy as np from scipy.stats import weibull_min import pandas as pd from lifelines import WeibullAFTFitter, CoxPHFitter # This is an implementation of https://uwspace.uwaterloo.ca/bitstream/handle/10012/10265/Cook_Richard-10265.pdf N = 50000 p = 0.5 bX = np.log(0.5) bZ = np.log(4) Z = np.random.binomial(1, p, size=N) X = np.random.binomial(1, 0.5, size=N) X_ = 20000 + 10 * np.random.randn(N) W = weibull_min.rvs(1, scale=1, loc=0, size=N) Y = bX * X + bZ * Z + np.log(W) T = np.exp(Y) ####################################### df = pd.DataFrame({"T": T, "x": X, "x_": X_}) wf = WeibullAFTFitter().fit(df, "T") wf.print_summary(4) cph = CoxPHFitter().fit(df, "T", show_progress=True, step_size=1.0) cph.print_summary(4)
color="failure", ) prebreakdown_merge_len_acc_1500_model_df_one_hot_no_censor = prebreakdown_merge_len_acc_1500_model_df_one_hot.query( "failure==1") aft = WeibullAFTFitter() aft.fit( prebreakdown_merge_len_acc_1500_model_df_one_hot, duration_col="mainline_vol", event_col="failure", formula= "ramp_metering+length_of_acceleration_lane+ffs_cap_df+number_of_mainline_lane_downstream+simple_merge", ) aft.print_summary() aft.plot() aft.median_survival_time_ aft.mean_survival_time_ fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 4)) aft.plot_partial_effects_on_outcome( "ramp_metering", [0, 1], cmap="coolwarm", ax=ax, plot_baseline=False, times=range(1000, 3200, 50), ) fig2, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(5, 4))
import pandas as pd from lifelines import WeibullAFTFitter, CoxPHFitter # This is an implementation of https://uwspace.uwaterloo.ca/bitstream/handle/10012/10265/Cook_Richard-10265.pdf N = 50000 p = 0.5 bX = np.log(0.5) bZ = np.log(4) Z = np.random.binomial(1, p, size=N) X = np.random.binomial(1, 0.5, size=N) X_ = 20000 + 10 * np.random.randn(N) W = weibull_min.rvs(1, scale=1, loc=0, size=N) Y = bX * X + bZ * Z + np.log(W) T = np.exp(Y) ####################################### df = pd.DataFrame({"T": T, "x": X, "x_": X_}) wf = WeibullAFTFitter().fit(df, "T") wf.print_summary(4) cph = CoxPHFitter().fit(df, "T", show_progress=True, step_size=1.0) cph.print_summary(4)