Survival analysis
How much time passes until an event happens – relapse to smoking, dropping out of school, returning to work, moving house – when some of the participants have not yet experienced it by the end of follow-up? Survival analysis uses their information too ("censored") instead of discarding them. The page computes Kaplan-Meier curves with the median and its confidence interval (on four scales), compares groups with the log-rank test (and also Breslow and Tarone-Ware), and examines several variables together in Cox regression – hazard ratios with confidence intervals and a check of the proportional hazards assumption. The output: APA-format tables, a chart with the number at risk, a results paragraph in English, Word, SPSS syntax, an R script and Excel with the life tables.
Loading the data
Paste columns directly from the sheet (including the header row) – select them in Excel, copy, and paste here:
Variables and settings
When to use?
When the dependent variable is time to event: there is a starting point (end of treatment, start of studies, release) and an event that may happen (relapse, dropout, re-arrest), and the question is how much time passes between them, and what affects it (Jesussek & Volk-Jesussek, 2025, pp. 396-397). The event is called a "failure" even when it is positive – returning to work, for example (Kleinbaum & Klein, 2012, p. 4). The name comes from medicine, but "the time studied does not have to be related to actual survival" (Jesussek & Volk-Jesussek, 2025, p. 398), and the methods are also used in economics, marketing and the social and behavioral sciences (Nag, 2022, p. 1) – employees leaving the organization, divorce (ibid., pp. 2-3), the time until returning to work after burnout. Cohen and colleagues (2003, p. 596) put it this way: how long it takes to change a dichotomous status – "to die, to recover, to marry or to succeed" – and whether this differs between groups.
The three common methods (Jesussek & Volk-Jesussek, 2025, p. 401) are the three parts of the output: the Kaplan-Meier curve – to describe; the log-rank test – to compare groups; Cox regression – to examine several variables together, including continuous ones. For one factor with two levels, log-rank is enough; when you also want to add age, for example – Cox (ibid., p. 425).
Censoring: why not simply regression
Every study ends at some point, and at its end there are participants for whom the event has not yet occurred; others left in the middle, moved house or stopped coming (Jesussek & Volk-Jesussek, 2025, pp. 400-401; Kleinbaum & Klein, 2012, pp. 6-7). For them it is known only that the time is longer than the follow-up time – this is right censoring, and it is the most common (Cohen et al., 2003, p. 597; Kleinbaum & Klein, 2012, p. 7). There is also left censoring (the event occurred before follow-up began) and interval censoring (it is known only that it occurred between two measurements; Kleinbaum & Klein, 2012, p. 8) – the page handles right censoring only. Omitting the censored cases, or entering their follow-up time as if it were the event time, biases the result: in the example of Cohen and colleagues, the mean of those who reached the event was 3.88 time units, compared with 5.16 if the study had continued until everyone reached it – an underestimate of 25% (pp. 596-597). Survival methods use the information of the censored cases as it is: up to the censoring time they are counted among those at risk, and after it they leave.
The assumption: censoring is unrelated to the chance of the event. Kleinbaum and Klein (Kleinbaum & Klein, 2012, pp. 37-43) distinguish between "independent" censoring (random within each subgroup), "random" and "non-informative" censoring – concepts that "are often confused" as if they were synonyms – and Kaplan-Meier, log-rank and Cox rely on the first. In practice: removing participants from follow-up because their condition worsened biases the estimates (Harrell, 2015, p. 402).
In the file: a time column (from each participant's starting point until the event or until censoring) and a status column – 1 for an event, 0 for censoring. One row per participant. Without a status column, all participants are treated as having experienced the event (Jesussek & Volk-Jesussek, 2025, p. 410) – on the page: "no status variable". This also reproduces the first example in the book, without censoring: 0.9 after 3 years, 0.7 after 4, 0.6 after 6 (ibid., pp. 405-406).
Kaplan-Meier curve
The curve shows, at each time, the probability that the event has not yet occurred – the "survival rate" (Jesussek & Volk-Jesussek, 2025, pp. 402-403). The computation: at each time at which an event occurred, divide those who survived it by those who were at risk just before it, (n − d)/n, and multiply by what there was up to then – the "product-limit" formula (Kleinbaum & Klein, 2012, pp. 63-67; Jesussek & Volk-Jesussek, 2025, pp. 407-408). A censored case does not lower the curve, but leaves the risk set for the following times. Thus the curve drops in steps only at event times, and "a steeper slope indicates a higher event rate" (Jesussek & Volk-Jesussek, 2025, p. 404).
The median – the time at which the curve drops to 50% – is the accepted number to summarize with; the mean is biased when there is censoring. If more than half of the group did not experience the event by the end of follow-up, the median was "not reached" – and that is a finding in itself. The page computes the median and its confidence interval as survfit does in R, and also a restricted mean (the area under the curve up to the last time) – like the SPSS Mean. The probability of surviving to a given time ("70% of fillings last more than five years"; Jesussek & Volk-Jesussek, 2025, p. 403) – in the setting "Probability of surviving to time".
The confidence interval for the curve – "both/and". The standard error is Greenwood's in all methods; the difference is the scale on which the interval is built. In the "simple" interval (plain), S ± 1.96·SE, which Kleinbaum and Klein teach (Kleinbaum & Klein, 2012, pp. 78-79), the limits can fall outside 0 and 1, and this is the "obvious shortcoming" on which all studies agree (Therneau & Grambsch, 2000, p. 16); they recommend the log or logit scale (p. 17), and Harrell (2015, pp. 412-413) – the log scale as the default. This is also the default of R and of the page; log-log (used by SAS and Stata) and logit – in the settings. The confidence interval of the median is derived from the limit curves, as in R; Kleinbaum and Klein (Kleinbaum & Klein, 2012, p. 80) describe the Brookmeyer-Crowley method, used by SAS – so the limits may differ slightly between programs.
Comparing groups: log-rank
The log-rank test examines whether two or more groups have the same survival curve: the null hypothesis – "the two groups have identical distribution curves" (Jesussek & Volk-Jesussek, 2025, p. 414). At each event time it compares the number of events in each group (O) with the number expected if the risk is equal – according to the group's share of those at risk (E) – and sums the differences over all times; the statistic follows a χ² distribution with the number of groups minus 1 degrees of freedom (Kleinbaum & Klein, 2012, pp. 68-72; Jesussek & Volk-Jesussek, 2025, pp. 415-419). Times at which participants were only censored do not enter the computation (Jesussek & Volk-Jesussek, 2025, p. 416). The common approximation Σ(O − E)²/E is slightly smaller than the exact statistic (Kleinbaum & Klein, 2012, p. 71) – the page computes the exact one. Log-rank is exactly the Score test of a Cox regression with the group as the only variable, so it assumes what Cox assumes – proportional hazards (Harrell, 2015, pp. 486-487; Therneau & Grambsch, 2000, p. 54).
SPSS offers two more tests from the same family, and the page computes all three. All are "weighted log-rank" tests: Breslow – called Wilcoxon in other programs – weights each time by the number of participants at risk, and therefore "places more emphasis on the information at the beginning of the curve"; Tarone-Ware – by the square root of that number, also favors the beginning, but less (Kleinbaum & Klein, 2012, p. 74). Log-rank gives all times equal weight, and it is the most powerful when the hazards are proportional. The test is chosen in advance, according to how the difference is expected to appear – not by "fishing" for the desired p value (ibid., p. 76). With three or more groups, the page adds pairwise comparisons, with the Holm correction – after a significant overall test (Harrell, 2015, p. 32).
Cox regression
Cox regression examines the effect of several variables together – continuous, binary or categorical – on the time to the event (Jesussek & Volk-Jesussek, 2025, p. 423). It does not predict the time itself but the hazard: the rate at which the event occurs at a given moment for those who have not yet experienced it – a "conditional failure rate", not a probability (Kleinbaum & Klein, 2012, pp. 11-12). The model assumes that the log of the hazard is a linear combination of the variables, which "shifts" a common baseline hazard whose shape is not specified – so it is "semi-parametric", and the hazard ratio is computed without estimating the baseline hazard (Kleinbaum & Klein, 2012, pp. 109-111). This is the approach of Cox (1972, as cited in Cohen et al., 2003, p. 598), the most common one; it relies only on the order of the event times (Harrell, 2015, p. 475; Kleinbaum & Klein, 2012, p. 130).
The output: for each variable B and HR = exp(B) – the hazard ratio. HR = 1.34 for the app versus group support means that at every moment, the rate at which app participants relapse to smoking is 34% higher; HR = 0.73 – 27% lower. For a continuous variable – per unit; it is better to report for a meaningful change (a decade of age, for example; Harrell, 2015, pp. 429, 509-510). "The most important values" in the table are the coefficient and its p value (Jesussek & Volk-Jesussek, 2025, p. 428). A categorical variable enters with dummy coding – each category versus the first – with a Wald test for all the categories together (Harrell, 2015, p. 481). And a constant hazard ratio is not a constant absolute benefit: the same HR means a small difference when the baseline hazard is low and a large one when it is high (ibid., pp. 430-431).
Model tests. The likelihood ratio (LR), Score and Wald tests examine whether all the coefficients are zero; LR "is generally considered the most reliable, and Wald the least" (Therneau & Grambsch, 2000, p. 53), and "when in doubt – use LR" (Kleinbaum & Klein, 2012, p. 104). When a coefficient tends to infinity (a group with no events), Wald breaks down and the other two do not (Therneau & Grambsch, 2000, pp. 58-61). Harrell's concordance index C – the proportion of pairs the model ranks correctly (Harrell, 2015, p. 505) – is computed on the same data, and is therefore optimistic (ibid., pp. 506-509).
Ties. When several participants experienced the event at the same time (with time measured in weeks or months this happens a lot), it must be decided how to divide the risk set among them. Breslow is "still the default in almost all programs", and its solution is "the least accurate" – it pulls the coefficients toward zero; Efron is "quite accurate" and just as fast (Therneau & Grambsch, 2000, pp. 48-53), "substantially more accurate" (Harrell, 2015, p. 477) – although in Harrell's example it was actually Breslow that came out closer to the exact computation (p. 479). R uses Efron, and SPSS, SAS and Stata use Breslow (Kleinbaum & Klein, 2012, p. 635). The page allows both, so that it can be compared with each of the programs.
How many events are needed. In survival analysis the "effective sample size" is the number of events, not the number of participants (Harrell, 2015, p. 73). Harrell (pp. 72-73) suggests at most one coefficient per 10-20 events – 15 on average – and counts every coefficient, including every dummy; the page warns below 10.
The proportional hazards assumption
"The central assumption of Cox regression": the hazard ratio between two groups is constant over time (Jesussek & Volk-Jesussek, 2025, p. 428) – if the app raises the hazard by 34%, this holds in the first week and in the fortieth week. Log-rank assumes this too (ibid., p. 415). "If the hazards cross, the assumption cannot hold" – but it can be violated even without them crossing (Kleinbaum & Klein, 2012, pp. 126, 165). And conversely: when there is no effect at all, the curves will "wiggle" around each other, and then "the assumption is not a problem" (Harrell, 2015, p. 434).
The page checks the assumption for each variable and for the model as a whole, like cox.zph in R – a score test for adding g(t)·x to the model: whether the effect of the variable changes over time. Almost all proportionality tests are such a test, and they differ only in the choice of g(t) (Therneau & Grambsch, 2000, p. 132). The default – the Kaplan-Meier 1 − S(t): almost identical to the ranking of the times, but "less sensitive to censoring patterns", and it spreads the times evenly and prevents extreme observations from dictating the result (ibid., p. 136). The test originated with Harrell as a correlation of the Schoenfeld residuals with the rank of time (Harrell, 2015, pp. 499, 517; Kleinbaum & Klein, 2012, pp. 181-183). The numbers in older books may differ slightly: up to version 3 of survival, cox.zph computed an approximation of the test (Therneau & Grambsch, 2000, p. 134).
The test "can miss" violations, and in a large sample even a negligible violation is significant – so first ask "does it matter" and "is it real" (Therneau & Grambsch, 2000, pp. 140-143), and it is recommended to use "at least two approaches" – a test and a plot (Kleinbaum & Klein, 2012, p. 187). When the assumption is violated: stratify by the variable (a separate baseline hazard for each group, with no HR for that variable), split the follow-up time into periods, or add a time-dependent variable (Kleinbaum & Klein, 2012, pp. 126, 204-207; Therneau & Grambsch, 2000, pp. 145-147; Harrell, 2015, pp. 501-503) – all of these are outside the page.
How to read the output?
Table 1 – for each group: the number of participants, events and censored cases, the median with a confidence interval, the restricted mean, and the probability of surviving to the time you chose. Figure 1 – the curves, with a small tick for each censored case and an "at risk" row below: when few are at risk, the end of the curve is unstable. Table 2 – the three tests for comparing the groups, with O and E for each group in the note; with three or more groups – pairwise comparisons too. On click – the full Kaplan-Meier table for each group.
When explanatory variables are selected – the Cox coefficients table (B, SE, Wald, p, HR and confidence interval), the model tests (−2LL, likelihood ratio, Score, Wald and the C index) and the proportionality check. At the end – a results paragraph in English and an "Interpreting the result" box that responds to the data.
Assumptions and warnings
Independent censoring – see above (Kleinbaum & Klein, 2012, p. 43; Jesussek & Volk-Jesussek, 2025, pp. 409, 415; Cohen et al., 2003, p. 598). A participant who stopped coming because they relapsed to smoking is not an "innocent" censored case. Independence between participants (Jesussek & Volk-Jesussek, 2025, p. 415). No competing risks: if another event prevents the event under study (death, when the event is returning to work), Kaplan-Meier assumes they are independent – an assumption that cannot be tested – and overstates the risk; the cumulative incidence curve (CIC) does not need this assumption (Kleinbaum & Klein, 2012, p. 447; Jesussek & Volk-Jesussek, 2025, p. 409). It is not on the page.
In Cox (Jesussek & Volk-Jesussek, 2025, pp. 428-429): proportional hazards; linearity of a continuous variable in the log of the hazard – and this is not solved by splitting the variable into groups, but with flexible functions (Harrell, 2015, pp. 19-21, 489-494); no multicollinearity; no extreme outliers; and no omitted interactions. And enough events – see above.
What the page does not do. Time-varying variables, recurrent events, competing risks, stratification, parametric models (Weibull, exponential – Nag, 2022, pp. 24, 33) and the discrete-time model. Cohen and colleagues (2003, pp. 599-600) describe the simple route to some of them: a file in which each participant has a row for each period, and logistic regression with time as a predictor – this way the values of the variables can also change from period to period.
And how do you do it in SPSS?
Analyze › Survival › Kaplan-Meier: the time in Time, the status in Status (with Define Event), the group in Factor; in Compare Factor – Log rank, Breslow, Tarone-Ware (Kleinbaum & Klein, 2012, pp. 610-611), and Pairwise over strata for the pairwise comparisons. Analyze › Survival › Cox Regression: the variables in Covariates, and in Categorical – the categorical ones, with Reference Category: First as on the page. SPSS computes Cox with Breslow for ties (Kleinbaum & Klein, 2012, p. 635) – select Breslow on the page to get the same numbers. There is no proportionality test in the menu: "SPSS does not easily allow" the test, and it is done in several steps – save the Schoenfeld residuals, rank the time and compute a correlation (ibid., pp. 615-617); the result is close to cox.zph but not identical. The syntax the page downloads builds KM and COXREG; it was not run in SPSS – in PSPP, the free alternative, these commands have not yet been implemented.
And how do you do it in R?
The survival package, written by Therneau (Therneau & Grambsch, 2000, p. 1): survfit for Kaplan-Meier, survdiff for log-rank, coxph for Cox and cox.zph for the proportionality check. The page is validated against the package, and the script below was run as is on the sample data file (the values are in the comments). The page also downloads an R script with your data embedded in it.
# Survival analysis – Professor Stat. This file is saved in UTF-8; every comment line starts with an English word
library(readxl) # reading Excel
library(survival) # Kaplan-Meier, log-rank, Cox
# Loading: row = participant; the sheet "Data"
d <- read_excel("C:/Rdata/DATA_survival_example_eng.xlsx", sheet = "Data")
d <- d[complete.cases(d[, c("weeks", "relapse", "program", "sex", "age", "prior_attempts")]), ] # 237 of 240
# Kaplan-Meier by program: median and confidence interval (Table 1)
fit <- survfit(Surv(weeks, relapse) ~ program, data = d)
print(fit, rmean = "individual") # Median: group 31 [22, –], app 20 [15, 31], individual not reached [31, –]
summary(fit, times = 26) # S(26): .545 [.444, .669], .421 [.324, .547], .645 [.547, .760]
plot(fit, col = 1:3, mark.time = TRUE, xlab = "weeks", ylab = "S(t)")
# Log-rank (Table 2)
survdiff(Surv(weeks, relapse) ~ program, data = d) # χ²(2) = 9.23, p = .010
# Cox regression – Efron (the default); ties = "breslow" gives the SPSS numbers
m <- coxph(Surv(weeks, relapse) ~ factor(program) + factor(sex) + age + prior_attempts, data = d)
summary(m) # LR χ²(5) = 12.93, p = .024; app vs. group HR = 1.34 [0.91, 1.99]
# Proportional hazards assumption
cox.zph(m, transform = "km") # Global: χ²(5) = 1.72, p = .886
And how do you do it in Python?
With the lifelines library, on which Nag builds (2022, p. vii): KaplanMeierFitter for Kaplan-Meier (p. 43), logrank_test for log-rank (pp. 52-53) and CoxPHFitter for Cox (p. 72); fit takes the time column and the status column (p. 16). Check the library's documentation for the defaults – the type of confidence interval for the curve and the handling of ties – before comparing with the page.
Reporting the results
Report: what the event is and what the starting point is; the length of follow-up and the number of censored cases; the median (or "not reached") with a confidence interval for each group, or the probability of surviving to a given time; the log-rank test with χ², degrees of freedom and p; and in Cox – the HR with a confidence interval for each variable, for a meaningful change (Harrell, 2015, pp. 509-510), the model test, the ties method, and the check of the proportionality assumption. The Kaplan-Meier chart is part of the report – the survival functions are "usually" presented in a graph, and the graph "can be as informative" as the analysis itself (Cohen et al., 2003, p. 599). The paragraph the page generates is a starting point.
Common mistakes
Omitting the censored cases, or analyzing their follow-up time as an event time – both ways shorten the times (Cohen et al., 2003, pp. 596-597).
Reporting a mean instead of a median when there is censoring, or reporting the end of follow-up as the "median" when the median was not reached.
Reading the end of the curve when few participants at risk remain in it – a drop of a whole step can be a single event.
Interpreting the HR as a ratio of probabilities or as a ratio of times: HR = 2 is not "twice the chance" and not "half the time"; the hazard is a rate, not a probability (Kleinbaum & Klein, 2012, p. 12).
Choosing the test by the result – running log-rank, Breslow and Tarone-Ware and reporting the significant one (Kleinbaum & Klein, 2012, p. 76).
Splitting a continuous variable into groups to compare curves and reporting it as a finding – "nothing could be more disastrous" (Harrell, 2015, p. 19); as a description – fine, but the association is tested in Cox with the continuous variable.
Ignoring crossing curves – log-rank loses power, and the Cox HR is an average that may not describe any period.
Coding the status in reverse – 1 for censored and 0 for an event. The page asks which value marks an event; check that the number of events in Table 1 makes sense.
References
American Psychological Association. (2020). Publication manual of the American Psychological Association (7th ed.). https://doi.org/10.1037/0000165-000
Cohen, J., Cohen, P., West, S. G., & Aiken, L. S. (2003). Applied multiple regression/correlation analysis for the behavioral sciences (3rd ed.). Lawrence Erlbaum Associates.
Harrell, F. E., Jr. (2015). Regression modeling strategies: With applications to linear models, logistic and ordinal regression, and survival analysis (2nd ed.). Springer. https://doi.org/10.1007/978-3-319-19425-7
Jesussek, M., & Volk-Jesussek, H. (2025). Statistics made easy: A clear and simple introduction (7th ed.). numiqo e.U.
Kleinbaum, D. G., & Klein, M. (2012). Survival analysis: A self-learning text (3rd ed.). Springer. https://doi.org/10.1007/978-1-4419-6646-9
Nag, A. (2022). Survival analysis with Python. CRC Press. https://doi.org/10.1201/9781003255499
Therneau, T. M., & Grambsch, P. M. (2000). Modeling survival data: Extending the Cox model. Springer. https://doi.org/10.1007/978-1-4757-3294-8
Cox (1972) – as cited in Cohen and colleagues (2003). Per the site rule, the reference list includes only sources read in the original; citations follow the printed page. Version 1 was built from chapters 24-25 of the numiqo book (in the folder: Jesussek & Volk-Jesussek, 2024, DATAtab) and from section 15.7 of Cohen and colleagues; version 2 – from the four books in the "Survival analysis" folder (for the Nag book the file name says 2021, and the copyright page – 2022). The computation on the page is validated against R 4.3.3 and the survival package 3.5-8 (survfit with the four types of confidence interval, survdiff, coxph, concordance, cox.zph), and against the numbers printed in the books: the remission data and the Evans County data in Kleinbaum and Klein (2012, pp. 62-80, 95, 156 – Kaplan-Meier, Greenwood, log-rank, Wilcoxon, Tarone-Ware and Cox), Pike's rat data in Harrell (2015, p. 480 – Efron & Breslow), and the test data of Therneau and Grambsch (2000, pp. 323-327).