id heduc heblack mixed years div
0 9 12-15 years No No 10.546 No
1 11 < 12 years No No 34.943 No
2 13 < 12 years No No 2.834 Yes
3 15 < 12 years No No 17.532 Yes
4 33 12-15 years No No 1.418 No
5 36 < 12 years No No 48.033 No
6 43 16+ years No No 16.706 No
7 47 < 12 years No No 24.999 No
8 50 < 12 years No No 24.999 No
9 56 < 12 years Yes No 3.869 No
In [20]:
df = patsy.dmatrix('heduc + heblack + heblack:mixed + years + mixed + div -1 ', data, return_type='dataframe')
In [21]:
df.head()
Out[21]:
heduc[12-15 years] heduc[16+ years] heduc[< 12 years] heblack[T.Yes] mixed[T.Yes] div[T.Yes] heblack[T.Yes]:mixed[T.Yes] years
0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 10.546
1 0.0 0.0 1.0 0.0 0.0 0.0 0.0 34.943
2 0.0 0.0 1.0 0.0 0.0 1.0 0.0 2.834
3 0.0 0.0 1.0 0.0 0.0 1.0 0.0 17.532
4 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.418

Aalen's Additive Model

In [24]:
aaf = AalenAdditiveFitter(fit_intercept=True, coef_penalizer=0.5)
In [25]:
timeline = np.linspace(0, 35, 1000)
In [26]:
aaf.fit(df, 'years', event_col='div[T.Yes]', timeline=timeline)
 [-----------------100%-----------------] 1032 of 1032 complete in 1.2 sec
Out[26]:
<lifelines.AalenAdditiveFitter: fitted with 3371 observations, 2339 censored>
In [27]:
figsize(12.5, 8.5)
aaf.cumulative_hazards_.plot()
plt.legend(loc='upper left')
Out[27]:
<matplotlib.legend.Legend at 0x10ee4a5d0>

Cox's Proportional Hazard Model

In [28]:
from lifelines import CoxPHFitter
In [37]:
cp = CoxPHFitter(penalizer=0.5)
cp.fit(df, 'years', event_col='div[T.Yes]')
Out[37]:
<lifelines.CoxPHFitter: fitted with 3371 observations, 2339 censored>
In [39]:
cp.plot()
Out[39]:
<matplotlib.axes._subplots.AxesSubplot at 0x11505fe90>