Skip to content
svgcif-curve

Cumulative incidence (competing risks)

How likely is each type of event by a given time when other events compete?

Demo data · seed 20261101 · 172 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read the height as the chance this event has happened by that time.
  2. Compare causes: when other events compete, each curve stays below what a one-minus-survival curve would give.
  3. Read the two curves' sum as the chance of any event.
  1. Callout 1: The cumulative chance of this event.
  2. Callout 2: A competing event.
  3. Callout 3: Time from the start.

Watch out

  • 1 − Kaplan–Meier overstates risk when other events compete. Use this chart instead.

Defaults

Confidence band
On
Shade the uncertainty around the curve.

Pinned in the code

  • Draws the engine's numbers as given (precomputed by the Python or R analysis); the chart computes nothing statistical.
  • Cumulative incidence, not 1 − Kaplan–Meier: with competing risks 1 − KM overstates the probability of the event of interest.
  • Demo: Aalen–Johansen CIFₖ(t) = Σ S(tⱼ−) dₖⱼ / nⱼ with S the all-cause Kaplan–Meier (R multi-state `survfit`).
# Cumulative incidence (competing risks) — Codesplash Chart Lab (recipe "cif-curve")
# controls: ci=true
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("cif-curve-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["cause"] = pd.Categorical(df["cause"].astype("string"), categories=["relapse", "death without relapse"])

def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
X = "months"
Y = "cumulative incidence"
LO = None
HI = None
G = "cause"
CI = True
groups = levels_of(df[G]) if G else [None]
series = []
for name in groups:
    d = df if name is None else df[df[G].astype(str) == str(name)]
    series.append((name if name is not None else "", d.dropna(subset=[X]).sort_values([X], kind="mergesort")))
fig, ax = plt.subplots(figsize=(8, 4.5))
for i, (name, d) in enumerate(series):
    col = PALETTE[i % len(PALETTE)]
    if CI and LO and HI:
        ax.fill_between(d[X], d[LO], d[HI], step="post", color=col, alpha=0.15, lw=0)
    ax.step(d[X], d[Y], where="post", color=col, linewidth=2, label=name or Y)
ax.set_ylim(0, None)
ax.set_xlabel(X)
ax.set_ylabel(Y)
if G:
    ax.legend(fontsize=8)
plt.show()

Needs pandas, numpy, matplotlib · reads cif-curve-demo.csv · app theme from the optional codesplash helper