统计

class lifelines.statistics.StatisticalResult(p_value, test_statistic, name=None, test_name=None, **kwargs)

此类保存统计测试的结果,并带有漂亮的打印包装器以显示结果。

注意

此类的API在0.16.0版本中发生了变化。

Parameters:
  • p_value (可迭代对象或浮点数) – 统计检验的p值

  • test_statistic (可迭代对象或浮点数) – 统计检验的检验统计量。如果是可迭代对象,则必须与p值的大小相同。

  • test_name (string) – 使用的测试。lifelines 应该设置这个。

  • name (可迭代对象或字符串) – 如果此类包含多个结果(例如:来自成对比较),则可以包含名称。如果为可迭代对象,则必须与p值的大小相同。

  • kwargs – 附加到对象并在print_summary()中显示的额外信息。

print_summary(decimals=2, style=None, **kwargs)

打印描述结果的摘要统计信息。

Parameters:
  • decimals (int, 可选 (默认=2)) – 指定显示的小数位数

  • style (string,) – {html, ascii, latex}, 默认 ascii

  • kwargs – 在输出中打印额外的元数据(在比较多个输出时,提供模型名称、数据集名称等非常有用)。

property summary

返回:包含测试统计量和p值的DataFrame :rtype: DataFrame

to_ascii(decimals=2, **kwargs)
to_html(decimals=2, **kwargs)
to_latex(decimals=2, **kwargs)
lifelines.statistics.logrank_test(durations_A, durations_B, event_observed_A=None, event_observed_B=None, t_0=-1, weights_A=None, weights_B=None, weightings=None, **kwargs) StatisticalResult

测量并报告两个强度过程是否不同。也就是说,给定两个事件序列,确定数据生成过程在统计上是否不同。检验统计量在原假设下是卡方分布。设\(h_i(t)\)为时间\(t\)时组\(i\)的风险比,则:

\[\begin{split}\begin{align} & H_0: h_1(t) = h_2(t) \\ & H_A: h_1(t) = c h_2(t), \;\; c \ne 1 \end{align}\end{split}\]

这隐式地使用了log-rank权重。

注意

  • lifelines 的 logrank 实现仅处理右删失数据。

  • 当比例风险假设成立时,对数秩检验具有最大的功效。因此,如果生存曲线交叉,对数秩检验将给出不准确的差异评估。

  • 此实现是函数multivariate_logrank_test的一个特例,该函数在内部使用。参见《生存与事件分析》第108页。

  • 使用对数秩检验与使用Cox回归相比只有缺点。更多讨论请参见这里。要转换为使用Cox回归:

from lifelines import CoxPHFitter

dfA = pd.DataFrame({'E': event_observed_A, 'T': durations_A, 'groupA': 1})
dfB = pd.DataFrame({'E': event_observed_B, 'T': durations_B, 'groupA': 0})
df = pd.concat([dfA, dfB])

cph = CoxPHFitter().fit(df, 'T', 'E')
cph.print_summary()
Parameters:
  • durations_A (可迭代对象) – 第一个群体的(n,)类列表的事件持续时间(从出生到死亡,…)。

  • durations_B (iterable) – 第二个群体的(n,)类似列表的事件持续时间(从出生到死亡,…)。

  • event_observed_A (可迭代的,可选的) – 第一个群体的(n,)类列表的审查标志(如果观察到则为1,否则为0)。 默认情况下假设所有都被观察到。

  • event_observed_B (可迭代的,可选的) – 第二个群体的(n,)类列表的审查标志(如果观察到则为1,否则为0)。 默认情况下假设所有都被观察到。

  • weights_A (可迭代对象, 可选) – 案例权重

  • weights_B (可迭代对象, 可选) – 案例权重

  • t_0 (float, optional (default=-1)) – 观察的最终时间段,在此时间之后经历事件的受试者将被设置为被审查。 指定-1以使用所有时间。

  • weightings (str, optional) – 应用加权对数秩检验:选项包括“wilcoxon”表示Wilcoxon(也称为Breslow),“tarone-ware”表示Tarone-Ware,“peto”表示Peto检验,“fleming-harrington”表示Fleming-Harrington检验。 这些对于测试生存曲线中的早期或晚期差异非常有用。对于Fleming-Harrington检验,还必须提供关键字参数p和q,且其值必须为非负数。

    Weightings are applied at the ith ordered failure time, \(t_{i}\), according to:

    Wilcoxon: \(n_i\) Tarone-Ware: \(\sqrt{n_i}\) Peto: \(\bar{S}(t_i)\) Fleming-Harrington: \(\hat{S}(t_i)^p \times (1 - \hat{S}(t_i))^q\)

    其中 \(n_i\) 是在时间 \(t_{i}\) 之前的风险数量,\(\bar{S}(t_i)\) 是 Peto-Peto 的修正生存估计,\(\hat{S}(t_i)\) 是在时间 \(t_{i}\) 的左连续 Kaplan-Meier 生存估计。

Returns:

一个具有属性 p_value, summary, test_statistic, print_summary 的 StatisticalResult 对象

Return type:

StatisticalResult

示例

T1 = [1, 4, 10, 12, 12, 3, 5.4]
E1 = [1, 0, 1,  0,  1,  1, 1]

T2 = [4, 5, 7, 11, 14, 20, 8, 8]
E2 = [1, 1, 1, 1,  1,  1,  1, 1]

from lifelines.statistics import logrank_test
results = logrank_test(T1, T2, event_observed_A=E1, event_observed_B=E2)

results.print_summary()
print(results.p_value)        # 0.7676
print(results.test_statistic) # 0.0872
lifelines.statistics.multivariate_logrank_test(event_durations, groups, event_observed=None, weights=None, t_0=-1, weightings=None, **kwargs) StatisticalResult

这个测试是对logrank_test的推广:它可以处理n>2个群体(当n=2时应该相等):

\[\begin{split}\begin{align} & H_0: h_1(t) = h_2(t) = h_3(t) = ... = h_n(t) \\ & H_A: \text{there exist at least one group that differs from the other.} \end{align}\end{split}\]
Parameters:
  • event_durations (iterable) – 一个 (n,) 类似列表的对象,表示所有个体的(可能是部分的)持续时间

  • groups (iterable) – 每个个体的唯一组标签的 (n,) 类似列表。

  • event_observed (可迭代对象, 可选) – 一个 (n,) 类似列表的事件观察结果:1 表示观察到死亡,0 表示截尾。默认为所有观察到的结果。

  • weights (可迭代对象, 可选) – 案例权重

  • t_0 (float, optional (default=-1)) – 观察的最终时间段,在此时间之后经历事件的受试者将被设置为被审查。 指定-1以使用所有时间。

  • weightings (str, 可选) – 应用加权对数秩检验:选项包括“wilcoxon”表示Wilcoxon(也称为Breslow),“tarone-ware”表示Tarone-Ware,“peto”表示Peto检验,“fleming-harrington”表示Fleming-Harrington检验。 这些对于测试生存曲线中的早期或晚期差异非常有用。对于Fleming-Harrington检验,还必须提供关键字参数p和q,且其值必须为非负数。

    Weightings are applied at the ith ordered failure time, \(t_{i}\), according to:

    Wilcoxon: \(n_i\) Tarone-Ware: \(\sqrt{n_i}\) Peto: \(\bar{S}(t_i)\) Fleming-Harrington: \(\hat{S}(t_i)^p \times (1 - \hat{S}(t_i))^q\)

    其中 \(n_i\) 是在时间 \(t_{i}\) 之前的风险数量,\(\bar{S}(t_i)\) 是 Peto-Peto 的修正生存估计,\(\hat{S}(t_i)\) 是在时间 \(t_{i}\) 的左连续 Kaplan-Meier 生存估计。

  • kwargs – 向实验摘要添加关键字和元数据。

Returns:

一个具有属性 p_value, summary, test_statistic, print_summary 的 StatisticalResult 对象

Return type:

StatisticalResult

示例

df = pd.DataFrame({
   'durations': [5, 3, 9, 8, 7, 4, 4, 3, 2, 5, 6, 7],
   'events': [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0],
   'groups': [0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2]
})
result = multivariate_logrank_test(df['durations'], df['groups'], df['events'])
result.test_statistic
result.p_value
result.print_summary()

# numpy example
G = [0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2]
T = [5, 3, 9, 8, 7, 4, 4, 3, 2, 5, 6, 7]
E = [1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0]
result = multivariate_logrank_test(T, G, E)
result.test_statistic
lifelines.statistics.pairwise_logrank_test(event_durations, groups, event_observed=None, t_0=-1, weightings=None, **kwargs) StatisticalResult

对所有\(n \ge 2\)个唯一组进行成对的对数秩检验。

Parameters:
  • event_durations (iterable) – 一个 (n,) 类似列表的对象,表示所有个体(可能部分)的持续时间

  • groups (iterable) – 每个个体的唯一组标签的 (n,) 类似列表。

  • event_observed (可迭代对象, 可选) – 一个 (n,) 类似列表的事件观察结果:1 表示观察到死亡,0 表示截尾。默认为所有观察到的结果。

  • t_0 (float, optional (default=-1)) – 观察的最终时间段,在此时间之后经历事件的受试者将被设置为被审查。 指定-1以使用所有时间。

  • weightings (str, optional) – 应用加权对数秩检验:选项包括“wilcoxon”表示Wilcoxon(也称为Breslow),“tarone-ware”表示Tarone-Ware,“peto”表示Peto检验,“fleming-harrington”表示Fleming-Harrington检验。 这些对于测试生存曲线中的早期或晚期差异非常有用。对于Fleming-Harrington检验,还必须提供关键字参数p和q,且其值必须为非负数。

    Weightings are applied at the ith ordered failure time, \(t_{i}\), according to:

    Wilcoxon: \(n_i\) Tarone-Ware: \(\sqrt{n_i}\) Peto: \(\bar{S}(t_i)\) Fleming-Harrington: \(\hat{S}(t_i)^p \times (1 - \hat{S}(t_i))^q\)

    其中 \(n_i\) 是在时间 \(t_{i}\) 之前的风险数量,\(\bar{S}(t_i)\) 是 Peto-Peto 的修正生存估计,\(\hat{S}(t_i)\) 是在时间 \(t_{i}\) 的左连续 Kaplan-Meier 生存估计。

  • kwargs – 向实验摘要添加关键字和元数据。

Returns:

一个包含所有成对比较的StatisticalResult对象(尝试StatisticalResult.summaryStatisticalResult.print_summary

Return type:

StatisticalResult

lifelines.statistics.power_under_cph(n_exp, n_con, p_exp, p_con, postulated_hazard_ratio, alpha=0.05) float

这计算了假设检验的效力,即实验组和对照组具有不同的风险(也就是说,相对风险比不同于1。)

Parameters:
  • n_exp (整数) – 实验组的大小。

  • n_con (整数) – 控制组的大小。

  • p_exp (float) – 实验组在研究期间失败的概率。

  • p_con (float) – 研究期间对照组失败的概率

  • postulated_hazard_ratio (float)

  • 假设的风险比

  • alpha (float, optional (默认=0.05)) – 第一类错误率

Returns:

检测风险比大小的能力,小至由postulated_hazard_ratio指定的值。

Return type:

浮点数

注释

Reference.

lifelines.statistics.proportional_hazard_test(fitted_cox_model, training_df, time_transform='rank', precomputed_residuals=None, **kwargs) StatisticalResult

测试Cox模型中的任何变量是否违反了比例风险假设。此方法使用了R的survival曾经使用的近似方法,但在2019年末进行了更改,因此lifelines和R之间会存在差异。

Parameters:
  • fitted_cox_model (CoxPHFitter) – 你想要测试的已拟合的Cox模型,使用training_df进行拟合。目前仅支持CoxPHFitter,但将来也会支持CoxTimeVaryingFitter。

  • training_df (DataFrame) – 用于调用Cox模型的fit的DataFrame。如果提供precomputed_residuals,则为可选

  • time_transform (向量化函数、列表或字符串,可选(默认=’rank’)) – {‘all’, ‘km’, ‘rank’, ‘identity’, ‘log’} 上述字符串之一,字符串列表,或用于转换时间的函数(必须接受(time, durations, weights))。‘all’将展示所有转换。

  • precomputed_residuals (DataFrame, 可选) – 指定已计算的缩放Schoenfeld残差。

  • kwargs – 添加到StatisticalResult的额外参数

注释

R 使用默认的 km,我们使用 rank,因为它在与其他转换相比时表现良好。参见 http://eprints.lse.ac.uk/84988/1/06_ParkHendry2015-ReassessingSchoenfeldTests_Final.pdf

参考文献

lifelines.statistics.sample_size_necessary_under_cph(power, ratio_of_participants, p_exp, p_con, postulated_hazard_ratio, alpha=0.05)

这计算了在Cox比例风险模型下比较两组所需功效的样本量。

Parameters:
  • power (float) – 用于检测危险比大小的能力,该危险比大小由postulated_hazard_ratio指定。

  • ratio_of_participants (实验组与对照组的参与者比例。)

  • p_exp (float) – 实验组在研究期间失败的概率。

  • p_con (float) – 研究期间对照组失败的概率

  • postulated_hazard_ratio (float) – 假设的风险比

  • alpha (float, optional (默认=0.05)) – 第一类错误率

Returns:

  • n_exp (integer) – 实验组所需的样本量以达到期望的统计功效

  • n_con (integer) – 对照组所需的样本量以达到期望的统计功效

示例

from lifelines.statistics import sample_size_necessary_under_cph

desired_power = 0.8
ratio_of_participants = 1.
p_exp = 0.25
p_con = 0.35
postulated_hazard_ratio = 0.7
n_exp, n_con = sample_size_necessary_under_cph(desired_power, ratio_of_participants, p_exp, p_con, postulated_hazard_ratio)
# (421, 421)

参考文献

https://cran.r-project.org/web/packages/powerSurvEpi/powerSurvEpi.pdf

另请参阅

power_under_cph

lifelines.statistics.survival_difference_at_fixed_point_in_time_test(point_in_time, fitterA, fitterB, **result_kwargs) StatisticalResult

分析师通常希望比较特定时间点的群体生存率,而不是比较整个生存曲线。例如,分析师可能对5年生存率感兴趣。在特定时间点统计比较原始的Kaplan-Meier点实际上会降低统计效力(参见[1])。通过对生存函数进行转换,我们可以恢复更多的统计效力。此函数使用log(-log(·))转换。

Parameters:
  • point_in_time (float,) – 分析生存曲线的时间点。

  • fitterA – 一个适用于数据的lifelines单变量模型。这可以是KaplanMeierFitterWeibullFitter等。

  • fitterB – 第二个用于比较的lifelines模型。

  • result_kwargs – 向实验摘要添加关键字和元数据

Returns:

一个具有属性 p_value, summary, test_statistic, print_summary 的 StatisticalResult 对象

Return type:

StatisticalResult

示例

T1 = [1, 4, 10, 12, 12, 3, 5.4]
E1 = [1, 0, 1,  0,  1,  1, 1]
kmf1 = KaplanMeierFitter().fit(T1, E1)

T2 = [4, 5, 7, 11, 14, 20, 8, 8]
E2 = [1, 1, 1, 1,  1,  1,  1, 1]
kmf2 = KaplanMeierFitter().fit(T2, E2)

from lifelines.statistics import survival_difference_at_fixed_point_in_time_test
results = survival_difference_at_fixed_point_in_time_test(12.0, kmf1, kmf2)

results.print_summary()
print(results.p_value)        # 0.77
print(results.test_statistic) # 0.09

注释

1. 其他变换是可能的,但Klein等人[1]表明,log(-log(·))变换具有最理想的统计特性。

  1. 此函数的API在v0.25.3版本中发生了变化。这个新的API允许测试右、左和区间删失模型。

参考文献

[1] Klein, J. P., Logan, B. , Harhoff, M. 和 Andersen, P. K. (2007), 在固定时间点分析生存曲线。Statist. Med., 26: 4505-4519. doi:10.1002/sim.2864