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).
A real result
Example “Hr07 n”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
N = 494, events = 247, HR = 0.70, α = 0.05, 1 − β = 0.800
- Participants
- 494
- Events
- 247
- Power
- 0.8003
- Hazard ratio
- 0.7000
Plots
View data
| Power | Hazard ratio | Total sample size |
|---|---|---|
| 0.0339 | HR = 0.77 | 2 |
| 0.0781 | HR = 0.77 | 32.825 |
| 0.1140 | HR = 0.77 | 63.650 |
| 0.1490 | HR = 0.77 | 94.475 |
| 0.1837 | HR = 0.77 | 125.30 |
| 0.2182 | HR = 0.77 | 156.13 |
| 0.2524 | HR = 0.77 | 186.95 |
| 0.2863 | HR = 0.77 | 217.78 |
| 0.3196 | HR = 0.77 | 248.60 |
| 0.3523 | HR = 0.77 | 279.43 |
| 0.3843 | HR = 0.77 | 310.25 |
| 0.4156 | HR = 0.77 | 341.07 |
| 0.4459 | HR = 0.77 | 371.90 |
| 0.4753 | HR = 0.77 | 402.73 |
| 0.5037 | HR = 0.77 | 433.55 |
| 0.5311 | HR = 0.77 | 464.38 |
| 0.5575 | HR = 0.77 | 495.20 |
| 0.5829 | HR = 0.77 | 526.02 |
| 0.6071 | HR = 0.77 | 556.85 |
| 0.6304 | HR = 0.77 | 587.67 |
| 0.6525 | HR = 0.77 | 618.50 |
| 0.6737 | HR = 0.77 | 649.33 |
| 0.6938 | HR = 0.77 | 680.15 |
| 0.7129 | HR = 0.77 | 710.98 |
| 0.7310 | HR = 0.77 | 741.80 |
| 0.7482 | HR = 0.77 | 772.63 |
| 0.7645 | HR = 0.77 | 803.45 |
| 0.7799 | HR = 0.77 | 834.27 |
| 0.7944 | HR = 0.77 | 865.10 |
| 0.8081 | HR = 0.77 | 895.92 |
| 0.8210 | HR = 0.77 | 926.75 |
| 0.8332 | HR = 0.77 | 957.58 |
| 0.8446 | HR = 0.77 | 988.40 |
| 0.8553 | HR = 0.77 | 1019.23 |
| 0.8654 | HR = 0.77 | 1050.05 |
| 0.8748 | HR = 0.77 | 1080.88 |
| 0.8837 | HR = 0.77 | 1111.70 |
| 0.8919 | HR = 0.77 | 1142.53 |
| 0.8997 | HR = 0.77 | 1173.35 |
| 0.9069 | HR = 0.77 | 1204.17 |
| 0.9137 | HR = 0.77 | 1,235 |
| 0.0374 | HR = 0.70 | 2 |
| 0.1080 | HR = 0.70 | 32.825 |
| 0.1701 | HR = 0.70 | 63.650 |
| 0.2314 | HR = 0.70 | 94.475 |
| 0.2917 | HR = 0.70 | 125.30 |
| 0.3504 | HR = 0.70 | 156.13 |
| 0.4068 | HR = 0.70 | 186.95 |
| 0.4606 | HR = 0.70 | 217.78 |
| 0.5113 | HR = 0.70 | 248.60 |
| 0.5588 | HR = 0.70 | 279.43 |
| 0.6030 | HR = 0.70 | 310.25 |
| 0.6439 | HR = 0.70 | 341.07 |
| 0.6815 | HR = 0.70 | 371.90 |
| 0.7159 | HR = 0.70 | 402.73 |
| 0.7472 | HR = 0.70 | 433.55 |
| 0.7756 | HR = 0.70 | 464.38 |
| 0.8013 | HR = 0.70 | 495.20 |
| 0.8244 | HR = 0.70 | 526.02 |
| 0.8451 | HR = 0.70 | 556.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).
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.
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,
},
})
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.