Skip to content

BPower & sample size · power.survival

Power analysis — survival (log-rank / Cox)

How many events and participants does a survival comparison need?

Required events and total sample size, power or smallest detectable hazard ratio for comparing two groups with the log-rank test or a Cox model, from Schoenfeld's (1983) formula: events D = (z₁₋α/s + z_power)² / (p(1 − p)·ln²HR), N = D / P(event).

Verified in Python and R Python and R agree to 10 decimals

A real result

Example “Hr07 n”, recorded in both engines. In the app, “Try this example” loads it into Analyze.

Power analysis: survival (log-rank / Cox)Recorded · Python and R agreeRecorded 2026-11-09 · 10 values compared
R

N = 494, events = 247, HR = 0.70, α = 0.05, 1 − β = 0.800

Participants
494
Events
247
Power
0.8003
Hazard ratio
0.7000

Plots

Power by total sample sizeHow does power grow with sample size (or effect size), and where does it reach the target?
View data
PowerHazard ratioTotal sample size
0.0339HR = 0.772
0.0781HR = 0.7732.825
0.1140HR = 0.7763.650
0.1490HR = 0.7794.475
0.1837HR = 0.77125.30
0.2182HR = 0.77156.13
0.2524HR = 0.77186.95
0.2863HR = 0.77217.78
0.3196HR = 0.77248.60
0.3523HR = 0.77279.43
0.3843HR = 0.77310.25
0.4156HR = 0.77341.07
0.4459HR = 0.77371.90
0.4753HR = 0.77402.73
0.5037HR = 0.77433.55
0.5311HR = 0.77464.38
0.5575HR = 0.77495.20
0.5829HR = 0.77526.02
0.6071HR = 0.77556.85
0.6304HR = 0.77587.67
0.6525HR = 0.77618.50
0.6737HR = 0.77649.33
0.6938HR = 0.77680.15
0.7129HR = 0.77710.98
0.7310HR = 0.77741.80
0.7482HR = 0.77772.63
0.7645HR = 0.77803.45
0.7799HR = 0.77834.27
0.7944HR = 0.77865.10
0.8081HR = 0.77895.92
0.8210HR = 0.77926.75
0.8332HR = 0.77957.58
0.8446HR = 0.77988.40
0.8553HR = 0.771019.23
0.8654HR = 0.771050.05
0.8748HR = 0.771080.88
0.8837HR = 0.771111.70
0.8919HR = 0.771142.53
0.8997HR = 0.771173.35
0.9069HR = 0.771204.17
0.9137HR = 0.771,235
0.0374HR = 0.702
0.1080HR = 0.7032.825
0.1701HR = 0.7063.650
0.2314HR = 0.7094.475
0.2917HR = 0.70125.30
0.3504HR = 0.70156.13
0.4068HR = 0.70186.95
0.4606HR = 0.70217.78
0.5113HR = 0.70248.60
0.5588HR = 0.70279.43
0.6030HR = 0.70310.25
0.6439HR = 0.70341.07
0.6815HR = 0.70371.90
0.7159HR = 0.70402.73
0.7472HR = 0.70433.55
0.7756HR = 0.70464.38
0.8013HR = 0.70495.20
0.8244HR = 0.70526.02
0.8451HR = 0.70556.85

First 60 of 123 rows.

  • Schoenfeld (1983): events depend only on the hazard ratio, allocation and error rates; participants = events / P(event).
  • Power uses the one-tail normal approximation of the formula it inverts (the far tail is negligible).
Python

N = 494, events = 247, HR = 0.70, α = 0.05, 1 − β = 0.800

Participants
494
Events
247
Power
0.8003
Hazard ratio
0.7000

To keep this page light, the plots are shown once, under the R tab.

  • Schoenfeld (1983): events depend only on the hazard ratio, allocation and error rates; participants = events / P(event).
  • Power uses the one-tail normal approximation of the formula it inverts (the far tail is negligible).

Do it in CodeSplash Run it on your own data, in Python or R, with the code to prove it.

Worked examples in the app

  • Hr07 nNo data needed
  • Unequal powerNo data needed
  • Detectable hrNo data needed

In the guides

Where it sits in the decision guides

  • Sample-size planning · Statistics Guide · Step 0 — Prepare your data
  • Compare groups: log-rank · Statistics Guide · F — Time-to-event analysis
  • Model predictors: Cox PH · Statistics Guide · F — Time-to-event analysis
  • Sample size and power · Statistics Guide · Additional considerations

Choices you make

hypothesis

Solve fordefault n

Which quantity to compute from the others: the sample size, the power, or the smallest effect you could detect.

  • Sample size(default) — Finds the sample size needed for your power and effect; the usual question before a study.
  • Power — Finds the chance of detecting your effect at this sample; pick it when the sample size is fixed.
  • Smallest detectable hazard ratio — Finds the smallest hazard ratio this sample can detect; pick it when the sample size is fixed.
Hazard ratiodefault 0.7

Hazard in group 2 relative to group 1; HR and 1/HR need the same number of events.

Proportion allocated to group 1default 0.5

Share of participants allocated to group 1 (0.5 = equal groups).

Probability of an event during follow-updefault 0.5

Overall share of participants expected to have the event before the end of follow-up.

Total sample sizedefault 200

Total number of participants to assume (ignored when you solve for it).

Target power (1 − β)default 0.8

Chance of detecting the effect if it is real. 0.8 is common; 0.9 is stricter and needs more data.

Significance level (α)default 0.05

The false-alarm rate you accept: the chance of a significant result when there is truly no effect. 0.05 is conventional.

Testdefault two-sided

Two-sided unless the direction was fixed before collecting data. One-sided needs fewer rows but ignores effects the other way.

  • Two-sided(default) — Plans for a test in either direction; the usual choice, though it needs more data than one-sided.
  • One-sided — Plans for a test in the expected direction only; needs less data, but can't claim an effect the other way.

What it reports

n
How many independent units the analysis used; it sets precision and power.
n_exact
How many independent units the analysis used; it sets precision and power.
events
In time-to-event data, the outcome happening (death, failure, churn). Rows without it by the end of follow-up are censored.
events_exact
In time-to-event data, the outcome happening (death, failure, churn). Rows without it by the end of follow-up are censored.
power
The chance a test detects an effect of a given size, if that effect is real.
target_power
The chance a test detects an effect of a given size, if that effect is real.
hr
The ratio of instantaneous event rates between groups; 0.7 means a 30% lower rate at any moment.
alloc
Share of participants assigned to group 1 in the survival power calculation; 0.5 means two equal groups.
p_event
In time-to-event data, the outcome happening (death, failure, churn). Rows without it by the end of follow-up are censored.
alpha
The false-positive rate you accept before looking at the data, usually 0.05.

Python and R

Python and R agree to 10 decimals (relative tolerance 1e-10). Python uses scipy, codesplash; R uses stats, codesplashr.

Python

The exact script CodeSplash ran for example “Hr07 n” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0

# Power analysis — survival (log-rank / Cox)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.survival 1.0.0 · spec f7f210f564cc83ed
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.

# %% Setup
import math

import codesplash as cs
from codesplash.stats import power as pw  # Schoenfeld's formula (twin of R cs_power_schoenfeld_*)

# %% Inputs
solve_for = "n"  # n | power | effect
hr = 0.7
alloc = 0.5  # proportion in group 1
p_event = 0.5  # probability of an event during follow-up
n_given = 200
target = 0.8
alpha = 0.05
alternative = "two-sided"
warnings = []

# %% Solve (closed form)
n_exact = None
events_exact = None
target_power = None
if solve_for in ("n", "power") and abs(math.log(hr)) < 1e-12:
    raise ValueError("A hazard ratio of 1 means no difference to detect.")
if solve_for == "n":
    target_power = target
    events_exact = pw.schoenfeld_events(hr, alloc, alpha, target, alternative)
    events = pw.ceil_n(events_exact)
    n_exact = events_exact / p_event
    n = pw.ceil_n(n_exact)
    power = pw.schoenfeld_power(hr, n * p_event, alloc, alpha, alternative)
elif solve_for == "power":
    n = n_given
    events = n * p_event  # expected events
    power = pw.schoenfeld_power(hr, events, alloc, alpha, alternative)
else:
    target_power = target
    n = n_given
    events = n * p_event
    hr = pw.schoenfeld_hr(events, alloc, alpha, target, alternative)
    power = pw.schoenfeld_power(hr, events, alloc, alpha, alternative)

# %% Curves
n_ref = n if math.isfinite(n) else 200.0
xs = pw.grid(2.0, max(2.5 * n_ref, 12.0))
hrs = [hr ** s for s in (0.75, 1.0, 1.25)]  # nearer to / farther from 1 on the log scale
curve = [(float(x), pw.schoenfeld_power(h, float(x) * p_event, alloc, alpha, alternative), f"HR = {h:.2f}") for h in hrs for x in xs]

# %% Result
cs.publish({
    "method": "power.survival",
    "methodVersion": "1.0.0",
    "title": "Power analysis: survival (log-rank / Cox)",
    "engine": cs.engine_info("scipy"),
    "summary": f"Log-rank / Cox comparison: N = {n:,.0f}, events = {events:,.1f}, HR = {hr:.3f}, power = {power:.3f} at {alternative} α = {alpha}.",
    "apa": f"N = {n:,.0f}, events = {events:,.0f}, HR = {cs.fmt_num(hr)}, α = {alpha}, 1 − β = {power:.3f}",
    "statistics": [
        {"key": "n", "label": "Participants", "value": n, "format": "integer"},
        {"key": "events", "label": "Events", "value": events},
        {"key": "power", "label": "Power", "value": power},
        {"key": "hr", "label": "Hazard ratio", "value": hr},
    ],
    "warnings": warnings,
    "notes": [
        "Schoenfeld (1983): events depend only on the hazard ratio, allocation and error rates; participants = events / P(event).",
        "Power uses the one-tail normal approximation of the formula it inverts (the far tail is negligible).",
    ],
    "charts": [cs.chart(
        "power", "power-curve", "Power by total sample size",
        {"x": cs.col("Total sample size", [c[0] for c in curve]), "power": cs.col("Power", [c[1] for c in curve]),
         "series": cs.col("Hazard ratio", [c[2] for c in curve])},
        controls={"target": target if target_power is not None else 0.8},
    )],
    "values": {
        "n": n, "n_exact": n_exact, "events": events, "events_exact": events_exact, "power": power,
        "target_power": target_power, "hr": hr, "alloc": alloc, "p_event": p_event, "alpha": alpha,
    },
})
R

The exact script CodeSplash ran for example “Hr07 n” · R version 4.5.1 (2025-06-13)

# Power analysis — survival (log-rank / Cox)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.survival 1.0.0 · spec f7f210f564cc83ed
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.

# %% Setup
library(codesplashr) # Schoenfeld's formula (twin of codesplash.stats.power)

# %% Inputs
solve_for <- "n" # n | power | effect
hr <- 0.7
alloc <- 0.5 # proportion in group 1
p_event <- 0.5 # probability of an event during follow-up
n_given <- 200
target <- 0.8
alpha <- 0.05
alternative <- "two-sided"
warnings <- list()

# %% Solve (closed form)
n_exact <- NA
events_exact <- NA
target_power <- NA
if (solve_for %in% c("n", "power") && abs(log(hr)) < 1e-12) stop("A hazard ratio of 1 means no difference to detect.")
if (solve_for == "n") {
  target_power <- target
  events_exact <- cs_power_schoenfeld_events(hr, alloc, alpha, target, alternative)
  events <- cs_power_ceil(events_exact)
  n_exact <- events_exact / p_event
  n <- cs_power_ceil(n_exact)
  power <- cs_power_schoenfeld_power(hr, n * p_event, alloc, alpha, alternative)
} else if (solve_for == "power") {
  n <- n_given
  events <- n * p_event # expected events
  power <- cs_power_schoenfeld_power(hr, events, alloc, alpha, alternative)
} else {
  target_power <- target
  n <- n_given
  events <- n * p_event
  hr <- cs_power_schoenfeld_hr(events, alloc, alpha, target, alternative)
  power <- cs_power_schoenfeld_power(hr, events, alloc, alpha, alternative)
}

# %% Curves
n_ref <- if (is.finite(n)) n else 200
xs <- cs_power_grid(2, max(2.5 * n_ref, 12))
hrs <- hr^c(0.75, 1, 1.25) # nearer to / farther from 1 on the log scale
curve <- do.call(rbind, lapply(hrs, function(h) {
  data.frame(x = xs, power = cs_power_schoenfeld_power(h, xs * p_event, alloc, alpha, alternative), series = sprintf("HR = %.2f", h))
}))

# %% Result
cs_publish(list(
  method = "power.survival",
  methodVersion = "1.0.0",
  title = "Power analysis: survival (log-rank / Cox)",
  engine = cs_engine_info("stats"),
  summary = sprintf("Log-rank / Cox comparison: N = %s, events = %.1f, HR = %.3f, power = %.3f at %s alpha = %s.", format(n, big.mark = ","), events, hr, power, alternative, format(alpha)),
  apa = sprintf("N = %s, events = %.0f, HR = %s, α = %s, 1 − β = %.3f", format(n, big.mark = ","), events, cs_fmt_num(hr), format(alpha), power),
  statistics = list(
    list(key = "n", label = "Participants", value = n, format = "integer"),
    list(key = "events", label = "Events", value = events),
    list(key = "power", label = "Power", value = power),
    list(key = "hr", label = "Hazard ratio", value = hr)
  ),
  warnings = warnings,
  notes = c("Schoenfeld (1983): events depend only on the hazard ratio, allocation and error rates; participants = events / P(event).",
            "Power uses the one-tail normal approximation of the formula it inverts (the far tail is negligible)."),
  charts = list(cs_chart(
    "power", "power-curve", "Power by total sample size",
    list(x = cs_col("Total sample size", curve$x), power = cs_col("Power", curve$power), series = cs_col("Hazard ratio", curve$series)),
    controls = list(target = if (is.na(target_power)) 0.8 else target)
  )),
  values = list(
    n = n, n_exact = n_exact, events = events, events_exact = events_exact, power = power,
    target_power = target_power, hr = hr, alloc = alloc, p_event = p_event, alpha = alpha
  )
))

Sources

  • Schoenfeld, D. A. (1983). Sample-size formula for the proportional-hazards regression model. Biometrics, 39, 499–503.
  • Collett, D. (2015). Modelling survival data in medical research (3rd ed.), chapter 10.

From the CodeSplash method registry · power.survival v1.0.0.