Análise de Sobrevivência em Python
Shae Wang
Senior Data Scientist
Uma estatística não paramétrica que estima a função de sobrevivência de dados de tempo até o evento.
Definições:
A função de sobrevivência $S(t)$ é estimada com: $$S(t)=\prod_{i:t_i\leq t}\bigg(1-\frac{d_i}{n_i}\bigg)$$
Suponha que temos eventos em 3 tempos: 1, 2, 3
Taxa de sobrevivência para $t=2$: $$S(t=2)=\bigg(1-\frac{d_1}{n_1}\bigg)*\bigg(1-\frac{d_2}{n_2}\bigg)$$
Taxa de sobrevivência para $t=3$: $$S(t=3)=S(t=2)*\bigg(1-\frac{d_3}{n_3}\bigg)$$
A taxa de sobrevivência no tempo t é o produto da chance de sobreviver em t e em cada tempo anterior.
from lifelines import KaplanMeierFitter
KaplanMeierFitter: uma classe da biblioteca lifelines
kmf = KaplanMeierFitter()
kmf.fit(durations, event_observed)
Nome do DataFrame: mortgage_df
| id | duration | paid_off |
|---|---|---|
| 1 | 25 | 0 |
| 2 | 17 | 1 |
| 3 | 5 | 0 |
| ... | ... | ... |
| 100 | 30 | 1 |
Nome do DataFrame: mortgage_df
| id | duration | paid_off |
|---|---|---|
| 1 | 25 | 0 |
| 2 | 17 | 1 |
| 3 | 5 | 0 |
| ... | ... | ... |
| 100 | 30 | 1 |
from lifelines import KaplanMeierFitter
mortgage_kmf = KaplanMeierFitter()
mortgage_kmf.fit(duration=mortgage_df["duration"],
event_observed=mortgage_df["paid_off"])
<lifelines.KaplanMeierFitter:"KM_estimate",
fitted with 100 total observations,
18 right-censored observations>
Qual é a mediana do tempo de uma hipoteca em aberto?
print(mortgage_kmf.median_survival_time_)
4.0
Qual é a probabilidade de uma hipoteca continuar em aberto a cada ano após o início?
print(mortgage_kmf.survival_function_)
KM_estimate
timeline
0.0 1.000000
1.0 0.983267
2.0 0.950933
3.0 0.892328
Qual é a probabilidade de uma hipoteca não estar quitada no ano 34 após o início?
mortgage_kmf.predict(34)
0.037998
.median_survival_time_ não pode ser calculado.Análise de Sobrevivência em Python