#!/usr/bin/env python
# coding: utf-8
#
#
# # Lifelines examples
# ##### Divorce Rates
# In[1]:
from lifelines.estimation import AalenAdditiveFitter
import pandas as pd
import numpy as np
import patsy
get_ipython().run_line_magic('pylab', 'inline')
# In[18]:
data = pd.read_csv('../lifelines/datasets/divorce.dat', sep="\s{2,10}")
# In[19]:
data.head(10)
# In[20]:
df = patsy.dmatrix('heduc + heblack + heblack:mixed + years + mixed + div -1 ', data, return_type='dataframe')
# In[21]:
df.head()
# ## 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)
# In[27]:
figsize(12.5, 8.5)
aaf.cumulative_hazards_.plot()
plt.legend(loc='upper left')
# ## 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]')
# In[39]:
cp.plot()
# In[ ]:
# In[ ]: