sts
Shows the share still surviving as time passes, and tests groups against each other.
What we have
sts list, with Greenwood standard errors and a log-log intervalsts graph, one curve per groupsts test, the log-rank testby()andat(); every number matches R'ssurvivalpackage (checked)
Coming later
- Nelson-Aalen cumulative hazard
- tests other than the log-rank —
wilcoxon,peto,tware,coxandstrataare refused
How you write it
sts list | graph | test [, by(var) at(times)]
Square brackets mean optional. A vertical bar means choose one.
What it does
Three things under one word.
sts listprints the survivor curve: at each time, what share are still event-free, with a confidence interval. Addat(12 24 36)for one row per interval, showing how many were still being watched at its start and how many left during it. This is the Kaplan-Meier estimator.sts graph, by(dept)draws the same thing as a chart, one line per group. Curves that separate early and stay apart are the picture you are looking for.sts test deptasks whether the groups' curves really differ (the log-rank test), and shows the events observed against those expected in each group.
Try it
Every example below was run after sysuse vira.sample03, clear.
. stset tenure_months, failure(left)
Survival-time data settings
Failure event: left != 0 & left < .
Observed time interval: (0, tenure_months]
Exit on or before: failure
--------------------------------------------------------------------------
4,000 total observations
0 exclusions
--------------------------------------------------------------------------
4,000 observations remaining, representing
1,652 failures in single-record/single-failure data
87,299 total analysis time at risk and under observation
At risk from t = 0
Last observed exit t = 60
The same thing in pandas and in R
pandas
import numpy as np import pandas as pd time = pd.to_numeric(df["tenure_months"], errors="coerce") status = pd.to_numeric(df["left"], errors="coerce") valid = time.notna() & (time > 0) df["_st"] = valid.astype(int) df["_d"] = ((status.notna() & (status != 0)) & valid).astype(int) df["_t"] = time.where(valid) df["_t0"] = np.where(valid, 0.0, np.nan)
R
library(survival) surv <- Surv(tenure_months, left) # This R has NOT been run against a real R yet: read it as documentation.
. sts list, by(dept) at(12 24 36)
Failure _d: left
Analysis time _t: tenure_months
-> dept = Sales
Beg. Net Survivor Std.
Time Total Fail Lost function Error [95% conf. int.]
------------------------------------------------------------------------------
12 1,204 309 203 0.7216 0.0136 0.6940 0.7472
24 692 171 137 0.5226 0.0163 0.4901 0.5541
36 384 104 98 0.3626 0.0175 0.3284 0.3967
------------------------------------------------------------------------------
-> dept = Operations
Beg. Net Survivor Std.
Time Total Fail Lost function Error [95% conf. int.]
------------------------------------------------------------------------------
12 1,022 172 176 0.8169 0.0127 0.7905 0.8403
24 674 104 143 0.6753 0.0165 0.6418 0.7064
36 427 57 127 0.5684 0.0191 0.5300 0.6048
------------------------------------------------------------------------------
-> dept = Technology
Beg. Net Survivor Std.
Time Total Fail Lost function Error [95% conf. int.]
------------------------------------------------------------------------------
12 904 161 168 0.8045 0.0139 0.7755 0.8301
24 575 98 129 0.6508 0.0180 0.6141 0.6848
36 348 54 108 0.5309 0.0210 0.4889 0.5711
------------------------------------------------------------------------------
-> dept = Finance
Beg. Net Survivor Std.
Time Total Fail Lost function Error [95% conf. int.]
------------------------------------------------------------------------------
12 325 43 50 0.8587 0.0201 0.8140 0.8933
24 232 27 56 0.7450 0.0269 0.6877 0.7934
36 149 12 50 0.6697 0.0319 0.6029 0.7279
------------------------------------------------------------------------------
-> dept = Support
Beg. Net Survivor Std.
Time Total Fail Lost function Error [95% conf. int.]
------------------------------------------------------------------------------
12 545 90 93 0.8235 0.0170 0.7874 0.8541
24 362 53 79 0.6873 0.0223 0.6413 0.7286
36 230 30 81 0.5790 0.0263 0.5256 0.6285
------------------------------------------------------------------------------
The same thing in pandas and in R
pandas
# Kaplan-Meier: S(t) = product(1 - d/n); Greenwood variance; log-log interval
R
library(survival)
fit <- survfit(Surv(tenure_months, left) ~ dept, data = df,
conf.type = "log-log")
summary(fit, times = c(12, 24, 36))
# This R was run in a real R (4.6.1) and gave the same numbers.
. sts test dept
Log-rank test for equality of survivor functions
dept | Events observed Events expected
-------------+------------------------------------
Sales | 636 439.44
Operations | 368 450.39
Technology | 343 374.50
Finance | 102 152.52
Support | 203 235.14
-------------+------------------------------------
Total | 1,652 1652.00
chi2(4) = 129.89
Pr>chi2 = 0.0000
The same thing in pandas and in R
pandas
# log-rank test: sum over failure times of (observed - expected) per group
R
library(survival) survdiff(Surv(tenure_months, left) ~ dept, data = df) # This R was run in a real R (4.6.1) and gave the same numbers.
The full built-in help (help sts)
stset timevar, failure(eventvar[==#])
sts list | graph | test [, by(var) at(times)]
stcox covariates [, breslow | efron]
Survival analysis: how long until something happens, when for some subjects
it has not happened yet. Someone still employed when the study ends has told
you they lasted at least that long -- dropping them, or counting them as
stayers, both give the wrong answer. That is what "censored" means.
. stset months, failure(left) declares the time and the event
. sts list, by(dept) at(12 24 36) Kaplan-Meier survivor function
. sts graph, by(dept) the same as a chart
. sts test dept log-rank test across departments
. stcox age i.dept hazard ratios
stset makes _st (usable), _d (event), _t (time). failure(left) counts
any non-zero value; failure(status==2) only that value. A time of
zero, negative or missing is excluded, and it says how many
sts list survivor function with Greenwood standard errors and a log-log
95% interval. With at(), one row per interval: how many were at
risk at its start, and how many failed and were censored in it
sts test the log-rank test, with observed and expected events per group
stcox Breslow ties by default, as in Stata; add efron for Efron
Not supported, and refused rather than ignored: delayed entry, several records
per subject, time-varying covariates, stratified and parametric models,
weights, Nelson-Aalen, and tests other than log-rank.
Every number matches R's survival package (survfit, survdiff, coxph); none has
been compared with a real Stata run.