NextStatNextStat

Applied Quickstart: Survival & Pharma

From install to first Cox PH fit in 3 minutes

This quickstart walks you through survival analysis with NextStat — Kaplan-Meier curves, log-rank tests, and Cox Proportional Hazards — using Python. No ROOT, no HEP knowledge required.

1. Install

pip install nextstat

2. Prepare Data

Survival data needs three arrays: observation times, event indicators (1 = event, 0 = censored), and optionally a covariate matrix.

import numpy as np

# Example: 200 patients, 3 covariates
np.random.seed(42)
n = 200
times  = np.random.exponential(10, n)
events = np.random.binomial(1, 0.7, n)
x      = np.random.randn(n, 3)  # age, dosage, biomarker

3. Kaplan-Meier Curve

import nextstat

km = nextstat.kaplan_meier(times, events, conf_level=0.95)

print(f"N = {km['n']}, events = {km['n_events']}")
print(f"Median survival: {km['median']:.2f}")

# Survival steps with confidence intervals
for t, s, lo, hi in zip(km['time'][:5], km['survival'][:5],
                         km['ci_lower'][:5], km['ci_upper'][:5]):
    print(f"  t={t:.2f}  S(t)={s:.3f}  95% CI [{lo:.3f}, {hi:.3f}]")

4. Log-Rank Test

Compare survival between two groups (e.g. treatment vs control).

groups = np.where(x[:, 1] > 0, "treatment", "control")
lr = nextstat.log_rank_test(times, events, groups)

print(f"chi² = {lr['chi_squared']:.2f}, p = {lr['p_value']:.4f}")

5. Cox Proportional Hazards

import nextstat.survival as surv

fit = surv.cox_ph.fit(
    times, events, x,
    ties="efron",
    robust=True,
)

print("Log-hazard ratios:", fit.params)
print("Robust SE:",         fit.robust_se)
print("Hazard ratios CI:")
print(fit.hazard_ratio_confint())

6. Diagnostics

# Check proportional hazards assumption
residuals = surv.schoenfeld_residuals(fit)

# Concordance index
print(f"C-index: {fit.concordance:.3f}")

Next Steps