In [46]:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

plt.rcParams["figure.figsize"] = (15,7)
sns.set()

In [2]:
import pandas as pd
from lifelines.datasets import load_dd

data = load_dd()

In [50]:
data.head()


Out[50]:
ctryname cowcode2 politycode un_region_name un_continent_name ehead leaderspellreg democracy regime start_year duration observed
0 Afghanistan 700 700.0 Southern Asia Asia Mohammad Zahir Shah Mohammad Zahir Shah.Afghanistan.1946.1952.Mona... Non-democracy Monarchy 1946 7 1
1 Afghanistan 700 700.0 Southern Asia Asia Sardar Mohammad Daoud Sardar Mohammad Daoud.Afghanistan.1953.1962.Ci... Non-democracy Civilian Dict 1953 10 1
2 Afghanistan 700 700.0 Southern Asia Asia Mohammad Zahir Shah Mohammad Zahir Shah.Afghanistan.1963.1972.Mona... Non-democracy Monarchy 1963 10 1
3 Afghanistan 700 700.0 Southern Asia Asia Sardar Mohammad Daoud Sardar Mohammad Daoud.Afghanistan.1973.1977.Ci... Non-democracy Civilian Dict 1973 5 0
4 Afghanistan 700 700.0 Southern Asia Asia Nur Mohammad Taraki Nur Mohammad Taraki.Afghanistan.1978.1978.Civi... Non-democracy Civilian Dict 1978 1 0

In [31]:
from lifelines import KaplanMeierFitter
kmf1 = KaplanMeierFitter()
kmf2 = KaplanMeierFitter()

In [ ]:
T = data["duration"]
E = data["observed"]

In [9]:
kmf.fit(T, event_observed=E)


Out[9]:
<lifelines.KaplanMeierFitter: fitted with 1808 observations, 340 censored>

In [22]:
kmf.survival_function_.plot()
plt.title('Survival function of political regimes');



In [23]:
kmf.plot()


Out[23]:
<matplotlib.axes._subplots.AxesSubplot at 0x116189c50>

In [24]:
kmf.median_


Out[24]:
6.0

In [49]:
ax = plt.subplot(111)

dem = (data["democracy"] == "Democracy")
kmf1.fit(T[dem], event_observed=E[dem], label="Democratic Regimes")
kmf1.plot(ax=ax)

kmf2.fit(T[~dem], event_observed=E[~dem], label="Non-democratic Regimes")
kmf2.plot(ax=ax)

plt.ylim(0, 1);
plt.title("Lifespans of different global regimes");



In [35]:
kmf2.subtract(kmf1).plot()


Out[35]:
<matplotlib.axes._subplots.AxesSubplot at 0x116ed8b38>

In [26]:
from lifelines.statistics import logrank_test

results = logrank_test(T[dem], T[~dem], E[dem], E[~dem], alpha=.99)

results.print_summary()


t_0=-1, alpha=0.99, null_distribution=chi squared, df=1

test_statistic      p     
      260.4695 0.0000  ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

In [27]:
regime_types = data['regime'].unique()

for i,regime_type in enumerate(regime_types):
    ax = plt.subplot(2, 3, i+1)
    ix = data['regime'] == regime_type
    kmf.fit( T[ix], E[ix], label=regime_type)
    kmf.plot(ax=ax, legend=False)
    plt.title(regime_type)
    plt.xlim(0, 50)
    if i==0:
        plt.ylabel('Frac. in power after $n$ years')
plt.tight_layout()



In [36]:
from lifelines.datasets import load_rossi
from lifelines import CoxPHFitter

rossi_dataset = load_rossi()
cph = CoxPHFitter()
cph.fit(rossi_dataset, duration_col='week', event_col='arrest')

cph.print_summary()  # access the results using cph.summary


n=432, number of events=114

        coef  exp(coef)  se(coef)       z      p  lower 0.95  upper 0.95    
fin  -0.3794     0.6843    0.1914 -1.9826 0.0474     -0.7545     -0.0043   *
age  -0.0574     0.9442    0.0220 -2.6109 0.0090     -0.1006     -0.0143  **
race  0.3139     1.3688    0.3080  1.0192 0.3081     -0.2898      0.9176    
wexp -0.1498     0.8609    0.2122 -0.7058 0.4803     -0.5657      0.2662    
mar  -0.4337     0.6481    0.3819 -1.1357 0.2561     -1.1822      0.3147    
paro -0.0849     0.9186    0.1958 -0.4336 0.6646     -0.4685      0.2988    
prio  0.0915     1.0958    0.0286  3.1938 0.0014      0.0353      0.1476  **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

Concordance = 0.640

In [38]:
X = rossi_dataset.drop(["week", "arrest"], axis=1)
cph.predict_partial_hazard(X)
cph.predict_survival_function(X)


Out[38]:
0 1 2 3 4 5 6 7 8 9 ... 422 423 424 425 426 427 428 429 430 431
event_at
0.0 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 ... 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
1.0 0.997616 0.993695 0.994083 0.999045 0.997626 0.997578 0.998865 0.997827 0.995453 0.997462 ... 0.997826 0.996005 0.996031 0.997774 0.998892 0.999184 0.997033 0.998866 0.998170 0.998610
2.0 0.995230 0.987411 0.988183 0.998089 0.995250 0.995154 0.997728 0.995653 0.990914 0.994922 ... 0.995649 0.992014 0.992067 0.995547 0.997782 0.998366 0.994065 0.997730 0.996337 0.997217
3.0 0.992848 0.981162 0.982314 0.997133 0.992878 0.992734 0.996592 0.993482 0.986392 0.992388 ... 0.993476 0.988037 0.988115 0.993324 0.996673 0.997548 0.991105 0.996595 0.994507 0.995826
4.0 0.990468 0.974941 0.976468 0.996176 0.990507 0.990316 0.995455 0.991311 0.981882 0.989855 ... 0.991304 0.984067 0.984171 0.991100 0.995563 0.996729 0.988147 0.995458 0.992676 0.994433
5.0 0.988085 0.968739 0.970639 0.995216 0.988135 0.987895 0.994315 0.989139 0.977378 0.987320 ... 0.989129 0.980101 0.980231 0.988875 0.994450 0.995909 0.985189 0.994319 0.990843 0.993039
6.0 0.985699 0.962552 0.964820 0.994254 0.985758 0.985471 0.993172 0.986961 0.972877 0.984781 ... 0.986950 0.976134 0.976290 0.986645 0.993334 0.995085 0.982227 0.993177 0.989006 0.991640
7.0 0.983305 0.956370 0.959004 0.993287 0.983374 0.983039 0.992024 0.984777 0.968371 0.982235 ... 0.984764 0.972162 0.972343 0.984409 0.992213 0.994258 0.979258 0.992030 0.987162 0.990236
8.0 0.971402 0.926001 0.930402 0.988460 0.971520 0.970950 0.986294 0.973910 0.946118 0.969581 ... 0.973888 0.952510 0.952816 0.973282 0.986619 0.990125 0.964523 0.986304 0.977978 0.983232
9.0 0.966614 0.913958 0.919043 0.986508 0.966752 0.966089 0.983979 0.969536 0.937236 0.964495 ... 0.969510 0.944651 0.945006 0.968804 0.984358 0.988453 0.958609 0.983991 0.974277 0.980405
10.0 0.964223 0.907978 0.913399 0.985531 0.964370 0.963660 0.982821 0.967350 0.932815 0.961954 ... 0.967323 0.940735 0.941114 0.966567 0.983226 0.987616 0.955657 0.982833 0.972426 0.978990
11.0 0.959411 0.896023 0.902109 0.983560 0.959577 0.958774 0.980485 0.962951 0.923949 0.956844 ... 0.962920 0.932876 0.933304 0.962064 0.980945 0.985928 0.949724 0.980499 0.968700 0.976140
12.0 0.954529 0.883991 0.890739 0.981555 0.954715 0.953817 0.978109 0.958486 0.914994 0.951661 ... 0.958451 0.924929 0.925406 0.957494 0.978624 0.984209 0.943711 0.978124 0.964915 0.973242
13.0 0.952091 0.878023 0.885094 0.980551 0.952287 0.951343 0.976920 0.956255 0.910539 0.949074 ... 0.956218 0.920972 0.921473 0.955212 0.977463 0.983349 0.940712 0.976937 0.963023 0.971793
14.0 0.944785 0.860283 0.868304 0.977534 0.945010 0.943926 0.973347 0.949567 0.897248 0.941322 ... 0.949525 0.909152 0.909724 0.948369 0.973974 0.980761 0.931734 0.973366 0.957347 0.967440
15.0 0.939883 0.848506 0.857145 0.975502 0.940127 0.938950 0.970942 0.945077 0.888381 0.936123 ... 0.945031 0.901255 0.901874 0.943775 0.971624 0.979018 0.925719 0.970963 0.953532 0.964511
16.0 0.934978 0.836824 0.846067 0.973462 0.935241 0.933972 0.968528 0.940583 0.879552 0.930923 ... 0.940533 0.893382 0.894047 0.939177 0.969266 0.977267 0.919708 0.968551 0.949711 0.961574
17.0 0.927644 0.819543 0.829662 0.970400 0.927935 0.926528 0.964907 0.933858 0.866428 0.923150 ... 0.933803 0.881661 0.882395 0.932299 0.965729 0.974638 0.910735 0.964932 0.943988 0.957171
18.0 0.920239 0.802323 0.813294 0.967294 0.920559 0.919014 0.961236 0.927063 0.853273 0.915307 ... 0.927003 0.869892 0.870693 0.925351 0.962142 0.971970 0.901694 0.961264 0.938199 0.952710
19.0 0.915280 0.790918 0.802441 0.965205 0.915619 0.913983 0.958769 0.922511 0.844517 0.910056 ... 0.922447 0.862045 0.862891 0.920696 0.959731 0.970176 0.895649 0.958798 0.934317 0.949715
20.0 0.902910 0.762911 0.775749 0.959965 0.903296 0.901434 0.952584 0.911145 0.822864 0.896968 ... 0.911072 0.842599 0.843552 0.909078 0.953687 0.965671 0.880605 0.952618 0.924611 0.942213
21.0 0.897956 0.751869 0.765209 0.957855 0.898360 0.896409 0.950094 0.906589 0.814266 0.891729 ... 0.906512 0.834860 0.835856 0.904421 0.951254 0.963856 0.874594 0.950130 0.920715 0.939197
22.0 0.895478 0.746385 0.759970 0.956797 0.895892 0.893896 0.948847 0.904309 0.809983 0.889110 ... 0.904231 0.831001 0.832018 0.902092 0.950034 0.962945 0.871591 0.948883 0.918765 0.937686
23.0 0.892994 0.740910 0.754738 0.955734 0.893417 0.891376 0.947593 0.902023 0.805698 0.886483 ... 0.901943 0.827138 0.828176 0.899755 0.948809 0.962031 0.868581 0.947631 0.916807 0.936169
24.0 0.883110 0.719381 0.734141 0.951488 0.883570 0.881354 0.942591 0.892923 0.788761 0.876042 ... 0.892836 0.811845 0.812963 0.890458 0.943919 0.958376 0.856630 0.942632 0.909010 0.930116
25.0 0.875711 0.703521 0.718943 0.948291 0.876198 0.873851 0.938826 0.886104 0.776194 0.868229 ... 0.886011 0.800471 0.801649 0.883492 0.940239 0.955622 0.847704 0.938870 0.903159 0.925566
26.0 0.868213 0.687672 0.703735 0.945034 0.868726 0.866249 0.934994 0.879188 0.763555 0.860316 ... 0.879091 0.789011 0.790246 0.876429 0.936492 0.952815 0.838677 0.935040 0.897218 0.920939
27.0 0.863219 0.677241 0.693713 0.942855 0.863750 0.861187 0.932432 0.874579 0.755193 0.855049 ... 0.874478 0.781415 0.782689 0.871723 0.933988 0.950937 0.832675 0.932480 0.893255 0.917848
28.0 0.858226 0.666911 0.683781 0.940670 0.858775 0.856127 0.929863 0.869969 0.746877 0.849785 ... 0.869865 0.773851 0.775162 0.867016 0.931475 0.949052 0.826684 0.929913 0.889287 0.914750
30.0 0.853213 0.656638 0.673893 0.938468 0.853780 0.851046 0.927275 0.865338 0.738571 0.844501 ... 0.865230 0.766286 0.767634 0.862288 0.928945 0.947152 0.820676 0.927327 0.885298 0.911632
31.0 0.850700 0.651527 0.668969 0.937361 0.851276 0.848499 0.925976 0.863016 0.734424 0.841853 ... 0.862906 0.762505 0.763872 0.859918 0.927674 0.946197 0.817668 0.926028 0.883296 0.910066
32.0 0.845672 0.641374 0.659183 0.935141 0.846266 0.843405 0.923369 0.858367 0.726161 0.836557 ... 0.858255 0.754964 0.756366 0.855174 0.925124 0.944281 0.811657 0.923423 0.879287 0.906928
33.0 0.840638 0.631308 0.649471 0.932910 0.841249 0.838304 0.920750 0.853711 0.717932 0.831257 ... 0.853595 0.747443 0.748882 0.850422 0.922564 0.942355 0.805647 0.920806 0.875267 0.903778
34.0 0.835564 0.621261 0.639767 0.930653 0.836193 0.833163 0.918103 0.849015 0.709682 0.825915 ... 0.848895 0.739893 0.741366 0.845630 0.919974 0.940405 0.799597 0.918160 0.871210 0.900594
35.0 0.825421 0.601478 0.620632 0.926117 0.826084 0.822888 0.912785 0.839619 0.693327 0.815245 ... 0.839493 0.724893 0.726435 0.836044 0.914772 0.936485 0.787531 0.912846 0.863082 0.894207
36.0 0.817800 0.586875 0.606483 0.922687 0.818489 0.815169 0.908767 0.832553 0.681158 0.807234 ... 0.832422 0.713705 0.715297 0.828838 0.910840 0.933519 0.778490 0.908831 0.856959 0.889386
37.0 0.807650 0.567773 0.587941 0.918089 0.808372 0.804891 0.903384 0.823134 0.665111 0.796572 ... 0.822996 0.698914 0.700570 0.819233 0.905574 0.929540 0.766480 0.903452 0.848785 0.882936
38.0 0.805099 0.563033 0.583333 0.916927 0.805829 0.802308 0.902026 0.820764 0.661106 0.793893 ... 0.820624 0.695215 0.696887 0.816817 0.904245 0.928534 0.763466 0.902094 0.846726 0.881309
39.0 0.799999 0.553632 0.574190 0.914599 0.800746 0.797144 0.899304 0.816026 0.653135 0.788540 ... 0.815883 0.687847 0.689549 0.811987 0.901581 0.926518 0.757450 0.899374 0.842607 0.878051
40.0 0.789848 0.535214 0.556247 0.909939 0.790629 0.786869 0.893858 0.806587 0.637407 0.777893 ... 0.806438 0.673275 0.675036 0.802367 0.896251 0.922481 0.745505 0.893932 0.834390 0.871541
42.0 0.784733 0.526079 0.547334 0.907577 0.785530 0.781692 0.891101 0.801827 0.629550 0.772531 ... 0.801674 0.665979 0.667770 0.797516 0.893552 0.920433 0.739499 0.891176 0.830240 0.868247
43.0 0.774567 0.508214 0.529874 0.902855 0.775396 0.771404 0.885591 0.792357 0.614073 0.761880 ... 0.792198 0.651575 0.653421 0.787869 0.888158 0.916337 0.727592 0.885670 0.821973 0.861674
44.0 0.769460 0.499383 0.521230 0.900469 0.770304 0.766237 0.882809 0.787596 0.606367 0.756533 ... 0.787434 0.644387 0.646260 0.783019 0.885435 0.914266 0.721624 0.882890 0.817810 0.858358
45.0 0.764349 0.490641 0.512664 0.898071 0.765209 0.761066 0.880015 0.782827 0.598701 0.751184 ... 0.782662 0.637225 0.639125 0.778163 0.882699 0.912184 0.715661 0.880098 0.813638 0.855030
46.0 0.754116 0.473429 0.495770 0.893242 0.755007 0.750715 0.874391 0.773273 0.583494 0.740482 ... 0.773102 0.622985 0.624935 0.768435 0.877192 0.907989 0.703753 0.874478 0.805265 0.848340
47.0 0.751552 0.469176 0.491589 0.892026 0.752451 0.748121 0.872976 0.770877 0.579712 0.737801 ... 0.770705 0.619437 0.621399 0.765997 0.875806 0.906932 0.700776 0.873063 0.803163 0.846658
48.0 0.746427 0.460745 0.483296 0.889587 0.747341 0.742938 0.870139 0.766086 0.572189 0.732446 ... 0.765910 0.612370 0.614356 0.761120 0.873028 0.904812 0.694831 0.870229 0.798956 0.843288
49.0 0.733641 0.440128 0.462975 0.883460 0.734592 0.730010 0.863018 0.754121 0.553625 0.719096 ... 0.753938 0.594882 0.596926 0.748945 0.866053 0.899481 0.680046 0.863112 0.788432 0.834839
50.0 0.725969 0.428037 0.451031 0.879752 0.726942 0.722254 0.858714 0.746932 0.542625 0.711092 ... 0.746745 0.584487 0.586564 0.741632 0.861836 0.896252 0.671203 0.858810 0.782096 0.829740
52.0 0.715699 0.412181 0.435335 0.874752 0.716702 0.711875 0.852915 0.737300 0.528067 0.700388 ... 0.737106 0.570689 0.572807 0.731835 0.856154 0.891896 0.659404 0.853014 0.773592 0.822879

50 rows × 432 columns


In [39]:
cph = CoxPHFitter()
cph.fit(rossi_dataset, duration_col='week', event_col='arrest')

cph.plot_covariate_groups('prio', [0, 5, 10, 15])


Out[39]:
<matplotlib.axes._subplots.AxesSubplot at 0x11709ea58>

In [40]:
from lifelines.datasets import load_dd
from lifelines import KaplanMeierFitter

data = load_dd()

democracy_0 = data.loc[data['democracy'] == 'Non-democracy']
democracy_1 = data.loc[data['democracy'] == 'Democracy']

kmf0 = KaplanMeierFitter()
kmf0.fit(democracy_0['duration'], event_observed=democracy_0['observed'])

kmf1 = KaplanMeierFitter()
kmf1.fit(democracy_1['duration'], event_observed=democracy_1['observed'])

fig, axes = plt.subplots()
kmf0.plot_loglogs(ax=axes)
kmf1.plot_loglogs(ax=axes)

axes.legend(['Non-democracy', 'Democracy'])

plt.show()



In [ ]:
def cohort_period(df):
    loan_funded = df.index.get_level_values('loan_funded')
    df['cohort_period'] = [
        lf for lf in loan_funded]
    return df

cohorts = cohorts.groupby(level=0).apply(cohort_period)

cohorts.reset_index(inplace=True)

cohorts.loc[:, 'cohort_period'] = \
    pd.to_datetime(cohorts['cohort_period'])

cohorts.set_index(['loan_created', 'cohort_period'], inplace=True)

cohorts = (df_or.groupby([df_or['loan_created'], 
                          df_or['loan_funded'].fillna('None')])
                .agg({'loan_funded_bool': 'sum'})
                .groupby(level=[0]).cumsum()
          )

# create a Series holding the total size of each cohort group
cohort_group_size = cohorts['loan_funded_bool'].groupby(level=0).sum()

conversion_rate = (
    cohorts['loan_funded_bool'].unstack(0)
                               .divide(cohort_group_size, axis=1)
)
conversion_rate.drop(conversion_rate.index[0], inplace=True)
# conversion_rate.fillna(method='backfill', inplace=True)
# conversion_rate.fillna(0, inplace=True)

ax = conversion_rate.plot(figsize=(15, 5), x_compat=True)
ax.xaxis.set_major_locator(mdates.MonthLocator())

plt.tick_params(axis='both', which='major', labelsize=13)
ax.set_xlim([min_date, max_date])
plt.legend(bbox_to_anchor=(1.04,1))
plt.show()