如何将pandas数据帧读入kaplan-meier曲线?

2024-05-28 19:12:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图复制Kaplan Meier表,即图1here。数字如下:

enter image description here

这是我写的代码:

# Python code to create the above Kaplan Meier curve
from lifelines import KaplanMeierFitter
import pandas as pd

df = pd.DataFrame({
                'T':[0,0,0,0,0,0,2.5,2.5,2.5,2.5,2.5,4,4,4,4,4,5,5,5,6,6],
                'E':[0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0],
})
## create a kmf object
kmf = KaplanMeierFitter() 

## Fit the data into the model
kmf.fit(df['T'], df['E'],label='Kaplan Meier Estimate')

## Create an estimate
kmf.plot(ci_show=False) 

我的输出图不同(参见比例):

here

当我打印生存函数时,它是不同的:

          Kaplan Meier Estimate
timeline                       
0.0                      1.0000
2.5                      0.9375
4.0                      0.7500
5.0                      0.6000
6.0                      0.6000

我假定我没有正确地将数据转换成数据帧(可能是?)。我试图搞乱数据帧,将1事件添加到时间帧的开始和结束,但这并不重要。有人能告诉我如何复制我正在尝试的例子吗


Tags: theto数据代码importdfcreatecode
1条回答
网友
1楼 · 发布于 2024-05-28 19:12:35

@Arne的评论是正确的。有6个主题,所以在TE向量中应该只有6个元素。回想一下,这些向量的每个元素都是一个单独的主题T表示观察受试者的时间,而E表示是否观察到受试者的“死亡”

有些相关,您可以使用生命线库中的实用程序函数将生存表转换为T,E向量:

from lifelines.utils import survival_events_from_table

df = pd.DataFrame([
    {"observed": 1, "censored": 0, "time": 2.5},
    {"observed": 2, "censored": 0, "time": 4.},
    {"observed": 1, "censored": 0, "time": 5.},
    {"observed": 0, "censored": 2, "time": 6.},
])
df = df.set_index("time")

T, E, W = survival_events_from_table(df)

kmf = KaplanMeierFitter().fit(T, E, weights=W)
kmf.plot()

相关问题 更多 >

    热门问题