カプラン・マイヤー曲線
特許の出願から登録までの期間、国内出願人と外国出願人の比較 — ログランク検定とリスク集合の表付き。

カプラン・マイヤー推定量は、追跡期間(イベントで終わるものもあれば、打ち切りとなるものもあります)を、各時点を超えて「生存」する確率を表す階段状の曲線に変換します。網掛けの帯は各時点ごとの 95% 信頼区間、小さな目盛りは打ち切りとなった出願を示し、破線は 50% から各群の 生存期間中央値 へと下ろしています。下の表はまだリスクにさらされている数を示しており、各曲線の裾がどれだけのデータに支えられているかがわかります。
設定は Vorreuther & Warin (2021, Data in Brief) で、中国の医薬品産業における特許の関連性と登録の速さに関するデータセットです。ここでのデータは説明のために シミュレーション したもので、論文のデータや結果ではありません。医薬品の特許出願 400 件を出願からの月数で測り、イベントは特許登録、観測終了時点でまだ審査中の出願は打ち切りとして扱います。曲線は KaplanMeierFitter、比較は logrank_test によるものです。
コードおよび図中のテキストは英語です。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from lifelines import KaplanMeierFitter
from lifelines.statistics import logrank_test
# 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)
groups = {"Domestic": df[df.foreign == 0], "Foreign": df[df.foreign == 1]}
colors = ["#007AFF", "#FF9500"]
ticks = np.arange(0, 97, 12)
fig, (ax, tab) = plt.subplots(2, 1, figsize=(8, 6.2), sharex=True,
gridspec_kw=dict(height_ratios=[4.2, 1]))
# Kaplan–Meier fit per group: step curve, CI band, censoring ticks, median line, risk table
for k, ((name, d), c) in enumerate(zip(groups.items(), colors)):
km = KaplanMeierFitter().fit(d.months, d.granted, label=name)
sf, ci = km.survival_function_[name], km.confidence_interval_
ax.step(sf.index, sf, where="post", color=c, lw=2, label=f"{name} applicants (n = {len(d)})")
ax.fill_between(ci.index, ci.iloc[:, 0], ci.iloc[:, 1], step="post", color=c, alpha=0.12, lw=0)
cm = d.loc[d.granted == 0, "months"]
ax.plot(cm, km.survival_function_at_times(cm), "|", color=c, ms=7, mew=1.2)
med = km.median_survival_time_
ax.plot([med, med], [0, 0.5], ls="--", lw=1, color=c)
ax.text(med, 0.02, f" {med:.0f} mo", color=c, fontsize=9.5, fontweight="semibold", ha="left")
for t in ticks:
tab.text(t, 1 - k, (d.months >= t).sum(), ha="center", va="center", color=c, fontsize=10)
ax.axhline(0.5, color="#8E8E93", lw=1, ls="--", zorder=1)
ax.set(ylim=(0, 1.02), xlim=(-3, 99), ylabel="Probability still pending", xlabel="Months since filing")
ax.tick_params(labelbottom=True)
ax.yaxis.set_major_formatter(plt.matplotlib.ticker.PercentFormatter(1))
ax.legend(loc="upper right")
# Log-rank test
dom, fgn = groups["Domestic"], groups["Foreign"]
lr = logrank_test(dom.months, fgn.months, dom.granted, fgn.granted)
p_txt = "p < .001" if lr.p_value < 0.001 else f"p = {lr.p_value:.3f}"
ax.set_title("Time to grant for two applicant groups, with a log-rank test", pad=26)
ax.text(0, 1.02, f"Log-rank χ²(1) = {lr.test_statistic:.1f}, {p_txt} · Kaplan–Meier with 95% CI · "
f"n = {n} applications (simulated), {df.granted.sum()} grants", transform=ax.transAxes,
fontsize=9.5, color="#6E6E73")
# Risk table styling
tab.set(ylim=(-0.5, 1.5), xticks=ticks)
tab.tick_params(labelbottom=False)
tab.set_yticks([1, 0], list(groups))
for lbl, c in zip(tab.get_yticklabels(), colors):
lbl.set_color(c)
tab.grid(False)
tab.spines[["left", "bottom"]].set_visible(False)
tab.set_title("Number at risk", fontsize=10, fontweight="normal", color="#6E6E73", pad=4)
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). Kaplan–Meier curves. In PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences (Version 1.1.0) [Computer software]. https://warin.ca/pystatlas/survival/kaplan-meier/
@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}
}