Virata manual

stcox

Explains how long people last, using several columns at once.

What we have

  • hazard ratios, with Breslow ties by default and efron on request
  • factor notation, and a match with R's coxph (checked)

Coming later

  • weights, which are refused
  • stratified and parametric models
  • a test of the proportional-hazards assumption

How you write it

stcox covariates [, breslow | efron]

Square brackets mean optional. A vertical bar means choose one.

What it does

Cox proportional hazards regression. Where sts compares whole groups, this adjusts for several things together: age, department and grade at the same time.

Results come as hazard ratios. A ratio of 1.3 means that at any moment those people are 1.3 times as likely to have the event as the comparison group; a ratio below 1 means they last longer. The model never has to say what the underlying risk over time looks like, which is why it is so widely used.

Ties — two people leaving on the same day — are handled Stata's way (Breslow) by default; add efron for the more accurate treatment when ties are common.

Watch out. It assumes the ratio between groups stays roughly constant over time (proportional hazards). If one group's curve crosses another's, that assumption has failed and a single hazard ratio is hiding the story.

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.
. stcox age i.level
Cox regression with Breslow method for ties

No. of subjects  =        4,000    Number of obs    =        4,000
No. of failures  =        1,652    LR chi2(5)       =        67.10
Time at risk     =       87,299    Prob > chi2      =       0.0000
Log likelihood   =  -12565.2971

------------------------------------------------------------------------------
          _t | Haz. ratio  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
         age |   1.002756   .00319522    0.86    0.388     .9965129    1.009038
     level=2 |   .8415329   .05045858   -2.88    0.004      .748226    .9464756
     level=3 |   .6638695   .05131911   -5.30    0.000      .570535    .7724728
     level=4 |   .4582762   .06198606   -5.77    0.000      .351556     .597393
     level=5 |   .5444055    .1171195   -2.83    0.005     .3571093    .8299346
------------------------------------------------------------------------------
The same thing in pandas and in R

pandas

import patsy
from statsmodels.duration.hazard_regression import PHReg
usable = df[df["_st"] == 1]
X = patsy.dmatrices('_t ~ age + C(level)', usable, return_type="dataframe")[1].drop(columns=["Intercept"], errors="ignore")
t, d = usable["_t"], usable["_d"]
model = PHReg(t, X, status=d, ties="breslow").fit()

R

library(survival)
model <- coxph(Surv(tenure_months, left) ~ age + factor(level),
    data = df, ties = "breslow")
summary(model)

# This R was run in a real R (4.6.1) and gave the same numbers.
The full built-in help (help stcox)
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.

Nearby

stset sts