调整后的生存曲线

Cox 模型预测的四种关联度水平下的专利授权时间,其他协变量取均值。

调整后的生存曲线

风险比难以直观感受,生存曲线则不然。这里使用拟合好的 Cox 比例风险模型,为仅在与申请人既有专利的关联度上不同的假想申请预测完整曲线,其他所有协变量都固定在样本均值。曲线之间的横向差距,就是关联度效应换算成等待授权月数后的结果。

背景为 Vorreuther 和 Warin (2021, Data in Brief),一个关于中国制药业专利关联度与授权速度的数据集。此处数据为说明用途的模拟数据,并非论文的数据或结果:400 件医药专利申请,关联度以 0 到 1 之间的 Jaccard 相似度衡量。模型用 lifelines.CoxPHFitter 基于全部四个协变量拟合,曲线来自 predict_survival_function();整个样本观测到的 Kaplan–Meier 曲线以灰色绘出,作为参照。

代码及图中文字均为英文。

adjusted-survival.py · 53 行
下载 GitHub
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from lifelines import CoxPHFitter, KaplanMeierFitter
# Simulated data, set in the context of Vorreuther & Warin (2021) [vorreuther2021]

# Data: months from filing to grant of 400 patent applications (shared generator)
rng = np.random.default_rng(2021)
n = 400
df = pd.DataFrame({"foreign": rng.integers(0, 2, n), "university": (rng.random(n) < 0.25).astype(int),
                   "claims": rng.poisson(12, n), "relatedness": rng.beta(2, 4, n).round(2)})
lp = 0.6 * df.foreign - 0.04 * (df.claims - 12) + 2.0 * (df.relatedness - 0.33) - 0.35 * df.university
grant = 40 * rng.weibull(2, n) * np.exp(-lp / 2)        # Weibull PH: higher lp, faster grant
cens = rng.uniform(24, 96, n)                           # end of observation window
df["months"], df["granted"] = np.minimum(grant, cens).round(1), (grant <= cens).astype(int)

cph = CoxPHFitter().fit(df, duration_col="months", event_col="granted")

# Hypothetical applications: everything at the mean, relatedness varies
levels = [0.1, 0.3, 0.5, 0.7]
profiles = pd.DataFrame([df.drop(columns=["months", "granted"]).mean()] * len(levels))
profiles["relatedness"] = levels
surv = cph.predict_survival_function(profiles, times=np.arange(0, 73))
colors = plt.cm.Blues(np.linspace(0.4, 0.95, len(levels)))

fig, ax = plt.subplots(figsize=(8, 5))

# Observed Kaplan–Meier curve of the whole sample, for reference
km = KaplanMeierFitter().fit(df.months, df.granted)
ax.step(km.survival_function_.index, km.survival_function_.iloc[:, 0], where="post",
        color="#AEAEB2", lw=1.5, ls="--", label=f"Observed Kaplan–Meier, all {n} applications")

# Model-predicted curves, with the predicted median time to grant in the legend
for col, r, c in zip(surv.columns, levels, colors):
    s = surv[col]
    med = s.index[(s <= 0.5).argmax()]
    ax.step(s.index, s, where="post", color=c, lw=2.2,
            label=f"relatedness = {r:.1f}  ·  median {med:.0f} months")
ax.axhline(0.5, color="#C7C7CC", lw=1, zorder=0)
ax.legend(loc="upper right")

hr = np.exp(0.1 * cph.params_["relatedness"])
lo, hi = np.exp(0.1 * cph.confidence_intervals_.loc["relatedness"])
p = cph.summary.loc["relatedness", "p"]
p_txt = "p < .001" if p < 0.001 else f"p = {p:.3f}"
ax.set(xlim=(0, 72), ylim=(0, 1.01), xticks=range(0, 73, 12),
       xlabel="Months since filing", ylabel="Predicted probability still pending")
ax.yaxis.set_major_formatter(plt.matplotlib.ticker.PercentFormatter(1, decimals=0))
ax.set_title("Applications related to earlier patents are granted sooner, all else equal", pad=26)
ax.text(0, 1.02, f"Cox PH, other covariates at their mean  ·  HR per 0.1 relatedness = "
        f"{hr:.2f} [{lo:.2f}, {hi:.2f}], {p_txt}  ·  n = {n} (simulated)",
        transform=ax.transAxes, fontsize=9.5, color="#6E6E73")
plt.show()

图表使用了可选的 PyStatlas 样式:添加 plt.style.use("pystatlas.mplstyle") 即可完全还原其外观。

数据来源

  • Simulated data. Simulated data with a fixed random seed, created for this example.
    No real respondents, documents or measurements: the numbers only illustrate how the chart and its statistics work.
  • Vorreuther & Warin (2021), patent velocity in China's pharmaceutical industry. Vorreuther, C. M., & Warin, T. (2021). Patent relatedness and velocity in the Chinese pharmaceutical industry: A dataset of Jaccard similarities. Data in Brief, 35, 106814. https://doi.org/10.1016/j.dib.2021.106814
    The example is set in the context of this paper; unless its description says otherwise, the numbers are simulated and are not the paper's data or results.

这些示例用于说明统计方法。除条目另有说明外,其数值均为模拟数据,图表标题描述的是该图在这些模拟数据中呈现的内容——既不是所引论文的结论,也不是作者对实质问题的立场。关于结果、背景和数据,请以此处引用的论文为准。

如何引用

PyStatlas 由 Thierry Warin 创建并维护。如果它对你的工作有所帮助,敬请引用。

Warin, T. (2026). Adjusted survival curves. In PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences (Version 1.1.0) [Computer software]. https://warin.ca/pystatlas/survival/adjusted-survival/

@misc{warin2026statlas,
  author       = {Warin, Thierry},
  title        = {PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences},
  year         = {2026},
  version      = {1.1.0},
  howpublished = {\url{https://warin.ca/pystatlas/}},
  note         = {Source code: https://github.com/warint/pystatlas}
}

更多生存分析图表