unusual regression can’t reply
Here’s a dataset. 432 folks have been launched from jail and have been adopted for one yr. They have been being watched for a single occasion: re-arrest. Some have been re-arrested in week 8, some in week 30. Most (318 of them) reached the top of the examine having by no means been re-arrested in any respect.
The straightforward query that was being requested on this examine was how lengthy till somebody re-offends?
We can’t simply common the arrest occasions. Three-quarters of the folks by no means received arrested, in order that they haven’t any arrest time to common. We can’t throw these folks away both. As a result of if we solely analyse those who failed, we are going to find yourself concluding that everybody finally re-offends, which is each false and grim. And we additionally can’t code the survivors as arrested after week 52, as a result of they weren’t. They have been both arrested at some week after the 52nd week, that we by no means received to see, or perhaps by no means.
That final scenario, which is we all know somebody lasted a minimum of this lengthy, however we have no idea how lengthy in whole, known as censoring. It’s the total cause survival evaluation exists as its personal area. Peculiar linear regression has no method to characterize “the reply is a minimum of 52.” It needs a quantity. Survival evaluation is the set of instruments constructed for the case the place, for a giant chunk of our knowledge, the clock was nonetheless working after we stopped watching.
This submit builds on that single drawback. We are going to begin with three core concepts we have to perceive Survival Evaluation. Then we are going to transfer on to the next three actions.
1. First, we are going to estimate a survival curve straight from knowledge with Kaplan-Meier.
2. Then spend most of our time on the Cox proportional hazards regression.
3. And eventually, we are going to match one mannequin in Python, studying its output as hazard ratios.
Three Core Ideas in Survival Evaluation
Virtually every thing in survival evaluation is constructed from three objects as detailed under.
1. The occasion and the time: Let’s choose one well-defined occasion. It may be dying, re-arrest, machine failure, subscription cancellation or a mortgage default. For every topic, we file two issues: how lengthy they have been noticed, i.e., the period, and whether or not the statement ended as a result of the occasion occurred or as a result of we stopped watching, i.e., the occasion indicator, which is binary coded as 1 or 0. These two columns are the important thing data of each mannequin under.
2. The survival operate, S(t): That is the chance {that a} topic makes it previous time t with out the occasion. It begins at 1 (at time zero) and decays towards 0 over time. For instance,
Survival at 12 months is 0.7″ means 70% are anticipated to nonetheless be event-free at a yr.
3. The hazard operate, h(t): The hazard is the instantaneous charge of the occasion at time t, on condition that we have now survived as much as t. Informally, we will say it like this:
Of the individuals who’ve made it this far, what fraction fail proper now?
The excellence between S(t) and h(t) issues. Survival is cumulative; hazard is momentary. Our total chance of nonetheless being alive at 80 is low (survival is small), however our hazard on the actual instantaneous we flip 80 is a special amount totally. Survival is how a lot water is left within the tank. Hazard, then again, is how briskly it’s draining proper now. They’re linked by a little bit of calculus. Hazard is failure density divided by survival. So, integrating the hazard provides us again survival.
Now, chances are you’ll suppose, why do we have to obsess over the hazard somewhat than simply modelling survival immediately? As a result of the hazard is the pure place to hold covariates. It’s straightforward to say “monetary assist multiplies our charge of re-arrest by 0.68 at each second.” That sentence is an announcement in regards to the hazard, and it’s precisely what the Cox mannequin will give us.
Kaplan-Meier: a survival curve with no assumptions
Earlier than we mannequin something, we will estimate S(t) immediately from knowledge. The Kaplan-Meier estimator (Kaplan & Meier, 1958, one of many most-cited papers in Statistics) does this with out assuming any specific form of the curve.
The concept may be very easy. Let’s stroll ahead in time. At every second the place an occasion really occurs, we have a look at how many individuals have been nonetheless in danger simply earlier than and what number of failed. Multiply collectively these “fraction who survived this instantaneous” numbers as we go. Censored folks contribute to the at-risk depend proper up till they go away, after which quietly drop out with out ever inflicting a downward step.
Now let’s apply this to our recidivism dataset. The precise dataset used within the article is the Rossi recidivism dataset, which is included with the lifelines Python library. It comes from a 1980 randomized experiment by Rossi, Berk, and Lenihan. The examine adopted 432 launched prisoners for one yr and recorded whether or not they have been re-arrested, plus covariates like race, training and so on. We are going to cut up the folks into two teams: those that obtained monetary assist after launch, and those that didn’t. Within the authentic examine, this assist was assigned randomly, so the comparability is truthful.
from lifelines import KaplanMeierFitter
from lifelines.datasets import load_rossi
import matplotlib.pyplot as plt
df = load_rossi() # 432 rows: 'week', 'arrest' (1=occasion), + covariates
kmf = KaplanMeierFitter()
for worth, label in [(0, "No financial aid"), (1, "Financial aid")]:
g = df[df.fin == value]
kmf.match(g["week"], g["arrest"], label=label)
kmf.plot_survival_function()
plt.ylabel("S(t): chance of remaining un-arrested")
plt.present()

Determine 1. Kaplan-Meier curves by financial-aid group. Each downward step is a re-arrest; the shaded bands are 95% confidence intervals. The help group (blue) stays larger all through. By week 52, about 22% of the help group had been re-arrested versus 31% of the no-aid group.
To check whether or not that hole is greater than noise, the usual software is the log-rank check. It compares the noticed variety of occasions in every group in opposition to what we might anticipate if the 2 curves have been actually the identical.
from lifelines.statistics import logrank_test
outcomes = logrank_test(
df[df["fin"] == 1]["week"],
df[df["fin"] == 0]["week"],
event_observed_A=df[df["fin"] == 1]["arrest"],
event_observed_B=df[df["fin"] == 0]["arrest"],
)
outcomes.p_value
On this knowledge, it returns p ≈ 0.05, which is correct on the border. It is a good reminder that Kaplan-Meier plus log-rank is an outline of 1 variable at a time. It cannot regulate for age, prior file, or the rest. The second you need to management for covariates, we’d like a Cox mannequin.
The Cox mannequin: Regression on the hazard
Once we need one thing like regression, e.g., plug in covariates, get out their results, however for the hazard, the naive transfer is to put in writing down a full system for h(t) and estimate every thing. However h(t) has a form over time, and we often do not know what that form is and no actual need to commit to at least one. Cox’s perception was that we will estimate the covariate results with out ever specifying the baseline form. The mannequin is specified as proven under:

Learn it as two items multiplied collectively:
- h₀(t), the baseline hazard: the danger over time for a hypothetical topic with all covariates at zero. The form is left utterly unspecified. It may be any form. That is the “nonparametric” half.
- exp(βᵀx), the covariate impact: a single quantity that scales your entire baseline up or down relying on private traits. That is the “parametric” half.
As a result of the mannequin is part-nonparametric and part-parametric, it’s referred to as a semiparametric mannequin.
Right here is the half that makes Cox mannequin very helpful – We will take two topics and kind the ratio of their hazards. The baseline h₀(t) is equivalent for each, so after we kind the ratio, that baseline seems on the highest and backside and cancels out:

The fitting-hand aspect has no t in it. The unknown, time-varying baseline is gone. So, now the hazard ratio is identical at week 1, week 20, and week 52. We by no means needed to specify the baseline’s form. That cancellation is your entire magic trick.
Cox then turned this into a technique referred to as the partial chance. At every occasion time, the mannequin asks a query:
Amongst everybody nonetheless in danger proper now, how more likely was it that this specific individual failed, somewhat than one of many others?
Censored folks match naturally into this setup. They keep within the at-risk set till they go away the examine, then quietly drop out. They by no means want an occasion. They nonetheless contribute data by telling us they have been event-free as much as that time.
Two issues value naming earlier than we match it:
- Ties : The partial chance assumes we will order the occasions. When two occasions land in the identical week, software program applies a correction (often Efron’s technique, which is the fashionable default and extra correct than Breslow’s).
- exp(β) is the hazard ratio. A coefficient of β = 0 means exp(β) = 1, i.e. no impact. β < 0 means exp(β) < 1, a protecting issue that lowers the hazard. β > 0 raises it. We nearly all the time report the exponentiated model.
Becoming the Cox mannequin in Python and deciphering hazard ratios
The lifelines library makes becoming a Cox mannequin easy.
from lifelines import CoxPHFitter
cph = CoxPHFitter()
cph.match(df, duration_col="week", event_col="arrest")
cph.print_summary()
The estimated hazard ratios are introduced under:
| Covariate | Hazard ratio exp(β) | 95% CI | p-value |
|---|---|---|---|
| Monetary assist | 0.68 | 0.47 – 1.00 | 0.047 |
| Age (per yr) | 0.94 | 0.90 – 0.99 | 0.009 |
| Prior convictions (every) | 1.10 | 1.04 – 1.16 | 0.001 |
| Race | 1.37 | 0.75 – 2.50 | 0.31 |
| Work expertise | 0.86 | 0.57 – 1.31 | 0.48 |
| Married | 0.65 | 0.31 – 1.37 | 0.26 |
| On parole | 0.92 | 0.63 – 1.35 | 0.67 |
To summarize:
- Monetary assist, HR 0.68. Receiving assist is related to a 32% decrease hazard of re-arrest at any given second. That is the remedy impact the unique experiment was constructed to seek out.
- Age, HR 0.94. Every further yr of age lowers the hazard by about 6%. Older releasees re-offend extra slowly.
- Prior convictions, HR 1.10. Every prior raises the hazard about 10%, and it multiplies: 5 priors is roughly 1.10⁵ ≈ 1.6× the hazard.
Hazard ratio shouldn’t be a distinction in survival chance. It’s a multiplier on the momentary charge, assumed fixed throughout the entire follow-up. This raises our subsequent query: is it really fixed? The remainder of the submit explains this.
The idea within the title: Proportional hazards
The mannequin known as proportional hazards for a cause. Let’s look once more on the ratio that made every thing work:

The fitting-hand aspect has no ‘t’ in it. That’s the proportional hazards (PH) assumption acknowledged exactly: the hazard ratio between any two topics is fixed over time. Monetary assist lowers the hazard by 32% in week 2 and by precisely 32% in week 50. The 2 teams’ hazards transfer up and down collectively. One is all the time a set a number of of the opposite. Their survival curves can by no means cross.
Typically that’s true. Typically it’s not. A remedy may assist enormously at first and put on off. A threat issue may solely chew in the long term. When the actual hazard ratio drifts with time, a plain Cox mannequin averages it right into a single quantity that’s fallacious at each ends. So, we have now to examine.
The examine most individuals ought to use relies on Schoenfeld residuals (Schoenfeld, 1982; and the scaled model with its formal check from Grambsch & Therneau, 1994). At every occasion time, the Schoenfeld residual measures the hole between the covariate worth of the one that really failed and the common covariate worth amongst everybody in danger. If the PH assumption holds, these residuals ought to present no development in opposition to time. If we will see a slope, the impact is altering over time, and proportionality is damaged.
cph.check_assumptions(df, p_value_threshold=0.05, show_plots=True)
On our knowledge the check flags two clear violations. Variable ‘age’ and ‘wexp’ failed the non-proportional check with p-values 0.0007 and 0.0063 respectively.

This instance exhibits the actual outcome on an actual, well-known dataset. It’s precisely the type of factor that stays invisible if we match the mannequin, learn the p-values, and stroll away. The coefficient for age (HR 0.94) isn’t fallacious however incomplete. It’s mainly collapsing a genuinely time-varying impact into one quantity.
What to do when proportionality breaks
Discovering a violation isn’t a useless finish. It’s often essentially the most attention-grabbing discovering within the evaluation, and there are three customary remediation methods.
1. Stratify: If a variable violates PH however we solely want to regulate for it (not estimate its impact), then we will put it in a strata. Stratification matches a separate baseline hazard for every stage of that variable and by no means forces its impact to be proportional.
cph_strat = CoxPHFitter()
cph_strat.match(
df,
duration_col="week",
event_col="arrest",
strata=["wexp"]
)
cph_strat.print_summary()
After stratifying on work expertise to repair the proportional hazards violation, the important thing outcomes maintain i.e., monetary assist (HR 0.68), older age (HR 0.94 per yr), and prior convictions (HR 1.09 every) stay statistically vital, whereas race, marital standing, and parole standing usually are not. The mannequin’s concordance of 0.61 signifies modest potential to rank who will get re-arrested sooner.
2. Let the impact range with time. If we really care how the impact adjustments, we have to add an interplay between the covariate and a operate of time. This lets its hazard ratio rise or fall over follow-up. In lifelines that is CoxTimeVaryingFitter with the info cut up at occasion occasions. Mainly, we’re becoming β(t) as a substitute of a single β.
3. Verify the purposeful kind first. The Schoenfeld check is delicate to a misspecified covariate. If age’s true impact is nonlinear (threat falling quick then leveling off), coming into it as a straight line can journey the PH check even when hazards are genuinely proportional. Earlier than reaching for time-varying fashions, attempt a squared time period on the offending variable.
Key takeaways
Censoring is a characteristic, not lacking knowledge. The entire level of those strategies is to make use of the partial data in censored topics appropriately. For those who ever end up dropping the individuals who didn’t have the occasion, you might be mainly introducing precisely the bias the sphere was invented to keep away from.
Kaplan-Meier to see, Cox to regulate. Plot the KM curves first. They’re assumption-light and so they construct instinct for the form of survival. Transfer to Cox when that you must management for covariates. Report hazard ratios with confidence intervals, not simply p-values.
A hazard ratio multiplies your threat, it doesn’t subtract from it. An HR of 0.68 means assist cuts the speed of re-arrest to about two-thirds at each second. It doesn’t let you know how many individuals keep away from arrest, or how for much longer they keep free. Additionally, it quietly assumes that two-thirds holds the entire time, which is the factor it’s a must to examine.
All the time check proportional hazards. It’s two traces of code and it’s the single factor that almost all separates a defensible survival evaluation from a fragile one. When it fails, you’ll be able to stratify, mannequin the time variation, or repair the purposeful kind.
Concordance, not R², for match. The concordance index measures how effectively the mannequin ranks who fails sooner.
Survival evaluation is basically simply regression that’s trustworthy about what it doesn’t know but. Grasp the hazard ratio, respect the belief in its title, and also you’ll get solutions that maintain up lengthy after the examine ends.
References
Foundational
- Cox, D. R. (1972). Regression Fashions and Life-Tables. Journal of the Royal Statistical Society, Collection B, 34(2), 187–220.
- Kaplan, E. L., & Meier, P. (1958). Nonparametric Estimation from Incomplete Observations. Journal of the American Statistical Affiliation, 53(282), 457–481.
Diagnostics and the Cox mannequin in depth
- Therneau, T. M., & Grambsch, P. M. (2000). Modeling Survival Knowledge: Extending the Cox Mannequin. Springer.
- Grambsch, P. M., & Therneau, T. M. (1994). Proportional Hazards Checks and Diagnostics Primarily based on Weighted Residuals. Biometrika, 81(3), 515–526.
- Schoenfeld, D. (1982). Partial Residuals for the Proportional Hazards Regression Mannequin. Biometrika, 69(1), 239–241.

