What censoring does to your data
Today let's understand what censoring means in data, and why it quietly ruins an ordinary average if you do not know it is there.
Maya runs Pulse, a fitness app that sells a monthly subscription. She wants to know one thing: how many months does a subscriber typically stay before cancelling? So she picks 24 subscribers who all signed up on the same day, and follows every one of them for a fixed 12-month window, writing down the month each one cancels.
By month 12, 15 of them have cancelled, each on a known month. The other 9 are still subscribed. Maya does not know when, or even if, those 9 will ever cancel. All she knows is that each of them has lasted at least 12 months so far.
Here is what she has on record for all 24 subscribers.
Look at that table. Fifteen rows have a clean number for month. Nine do not, they just say 12+. So what is the average time to cancel, when 9 of your 24 numbers are not finished yet?
Averaging only the subscribers who already cancelled
The simplest thing to try is to just use the subscribers whose story is finished. Keep only the 15 subscribers who have a real cancellation month on record, drop the 9 who are still subscribed, and average what is left.
pulse holds all 24 subscribers: month is the number Maya actually recorded for each one, and status is 1 if that number is a real cancellation and 0 if the subscriber is still subscribed. Filtering to status == 1 keeps exactly the 15 subscribers who already have a real cancellation month, and their average month is 6.4.
That is Maya's first guess: subscribers cancel after about 6.4 months, on average. It only uses the 15 subscribers with a finished number, and it says nothing at all about the other 9.
Treating month 12 as everyone's cancellation month
A different fix keeps all 24 subscribers instead of dropping any of them. For the 9 who are still subscribed, this fix treats month 12, the last month Maya actually watched them, as if it were their cancellation month.
Pulse's own month column already does exactly that. For the 9 still-subscribed subscribers, Maya cannot write down a cancellation month that has not happened yet, so the only honest number she has on record for them is 12, the length of time she has watched them so far. This keeps those exact same month numbers, just without dropping any of the 24 rows this time.
All 24 subscribers average out to 8.5 months. That is Maya's second guess, higher than the first one, because it keeps all 24 numbers instead of throwing 9 of them away.
Comparing both guesses against the real average
This dataset happens to be simulated for this lesson, which means there is a number Maya herself could never see: the true cancellation month for every subscriber, including the 9 who are still subscribed in her own records. Averaged over all 24 true cancellation months, the real answer is about 18.6 months.
Here is how Maya's two guesses stack up against that real number.
Both guesses land far below 18.6, but not by the same amount. Dropping the 9 still-subscribed subscribers removes exactly the 9 subscribers who, it turns out, last the longest of all 24. Every one of their true cancellation months falls past month 12, which is longer than any of the 15 who already cancelled. Capping them at month 12 is closer to the truth, 8.5 against 6.4, because it still counts all 9 of them, it just understates how long each one actually lasts.
Quick check: why both averages come out too low
Which of these correctly explains why dropping the 9 still-subscribed subscribers (6.4) comes out worse than capping them at month 12 (8.5)?
Censoring is partial information, not missing data
Those 9 still-subscribed subscribers have a name for what is happening with their data. They are called right-censored. Right-censoring means you know a subscriber lasted at least some known length of time, but you do not know exactly when, or whether, the event will happen. It is called right-censoring because the part you are missing sits to the right of what you observed, further along in time, past the point where Maya stopped watching.
Compare that to a genuinely missing value. Suppose Maya's spreadsheet had a blank cell for subscriber 6's month, because someone forgot to type it in, even though subscriber 6 had actually cancelled back in month 5. That blank cell would tell you nothing: no real fact, no lower bound, nothing to work with. Right-censoring is not that, since every one of the 9 censored rows already holds one true, usable fact.
That is exactly why both of Maya's guesses went wrong. Dropping a right-censored row throws away a true fact you already have. Capping it at 12 throws away the other half of that fact, that the real number is even bigger than 12. The right way to use a right-censored row is to use the one fact it actually gives you, "at least this long", not to erase it or round it down to the cutoff.
The survival function: how many are still subscribed at month t
Now put a name to the question this whole lesson has been circling: at any given month, what fraction of the original 24 subscribers are still subscribed? That fraction is called the survival function, written \(S(t)\), the fraction of the original 24 who have not cancelled by month \(t\).
You can compute \(S(t)\) directly from the 24 rows, without touching the hidden true cancellation months that Maya herself can never see. At month 0, nobody has cancelled yet, so \(S(0) = 1\). By month 3, 3 subscribers have cancelled (months 1, 1 and 2), leaving 21 of 24 still subscribed, so \(S(3) = 21/24 \approx 0.875\). By month 6, 8 have cancelled (add months 4, 4, 5, 6 and 6), leaving 16 of 24, so \(S(6) = 16/24 \approx 0.667\). By month 9, 12 have cancelled (add 7, 7, 9 and 9), leaving 12 of 24, so \(S(9) = 12/24 = 0.5\). And by month 12, all 15 eventual cancellations have happened, leaving the 9 still-subscribed subscribers, so \(S(12) = 9/24 = 0.375\).
Notice what that calculation never needed: the hidden true cancellation months. \(S(t)\) only ever asks about months up to 12, and 12 months is exactly as far as Maya ever watched anyone, so the recorded month and status columns are all it takes.
Here is \(S(t)\) traced across the whole 12-month window.
The curve only ever goes down or stays flat, because once a subscriber cancels, they do not come back, and 24 is a fixed starting point that never grows.
The hazard function, and how it rebuilds the survival function
\(S(t)\) asks what fraction of the original 24 are still subscribed. The hazard function, \(h(t)\), asks a narrower question: of the subscribers who are still subscribed entering month \(t\), what statisticians call being at risk, what fraction cancel during that one month?
A subscriber is at risk entering month 9 if they have not cancelled, and have not dropped out of view, before month 9. That includes anyone whose recorded month is 9 or later, whether they go on to cancel then or stay subscribed all the way to 12.
14 subscribers are at risk entering month 9, and 2 of them cancel that month, so \(h(9) = 2/14 \approx 0.143\).
Here is what makes \(S(t)\) and \(h(t)\) two views of one process, not two separate ideas. The fraction still subscribed past month 9 is just the fraction still subscribed past month 8, times the fraction of those who make it through month 9 without cancelling: \(S(9) = S(8) \times (1 - h(9))\).
\(S(8) = 14/24 \approx 0.583\), the same kind of direct count used for \(S(6)\) and \(S(9)\). Multiply it by \((1 - h(9))\), about 0.857, and you get exactly 0.5, which is \(S(9)\) computed the direct way two steps ago. That is not a coincidence. A survival function is a running total built one month-by-month hazard at a time, which is why multiplying them back together reproduces it exactly.
Quick check: reading a censored subscriber correctly
A 25th subscriber joins a similar study. They have been active for 10 months when the study ends, without having cancelled. What is true about their cancellation month?
Your turn: compute a hazard and a survival value
Try this yourself on month 11. pulse is still loaded with all 24 subscribers. Fill in the two NA lines below: first \(h(11)\), the same way you computed \(h(9)\), then \(S(11)\), the same way you computed \(S(6)\) and \(S(9)\).
Show answer
# Compute h(11) and S(11) from the same 24 Pulse subscribers
at_risk_11 <- sum(pulse$month >= 11)
events_11 <- sum(pulse$month == 11 & pulse$status == 1)
h11 <- events_11 / at_risk_11
h11
#> [1] 0.08333333
cancelled_by_11 <- sum(pulse$month <= 11 & pulse$status == 1)
S11 <- (24 - cancelled_by_11) / 24
S11
#> [1] 0.4583333Replace the first NA with events_11 divided by at_risk_11. Replace the second NA with 24 minus cancelled_by_11, divided by 24. Press Check when you have both.
12 subscribers are at risk entering month 11, and only 1 of them cancels that month, which is why \(h(11)\) is so much smaller than \(h(9)\) was.
References
- Kaplan, E.L., and Meier, P. (1958). Nonparametric Estimation from Incomplete Observations. Journal of the American Statistical Association, 53(282), 457-481. The original paper naming right-censoring and building toward the estimator this course covers next.
- Kleinbaum, D.G., and Klein, M. (2012). Survival Analysis: A Self-Learning Text (3rd ed.). Springer. A step-by-step introduction to the survival and hazard functions, aimed at readers meeting them for the first time.
- Klein, J.P., and Moeschberger, M.L. (2003). Survival Analysis: Techniques for Censored and Truncated Data (2nd ed.). Springer. A fuller treatment of censoring mechanisms beyond the right-censoring covered in this lesson.
- Cox, D.R., and Oakes, D. (1984). Analysis of Survival Data. Chapman and Hall. The classical text connecting the survival and hazard functions to the regression models this course builds toward.
- Collett, D. (2015). Modelling Survival Data in Medical Research (3rd ed.). CRC Press. Worked medical examples of the same right-censoring this lesson introduces with a subscription dataset.
What this leaves you, and what comes after it
Here is Maya's honest answer. The real average time to cancel among her 24 subscribers is about 18.6 months, not the 6.4 months she got by dropping the still-subscribed, and not the 8.5 months she got by capping them at 12. Both guesses were wrong for the same reason: 9 of the 24 subscribers were never finished observations to begin with. They were right-censored, each one carrying a true fact, "lasted at least this long", and no fix that drops that fact or rounds it down to the cutoff can recover what it is hiding.
From now on, whenever you meet a dataset with a fixed follow-up window, a trial that ends on a date, a warranty period, any observation period with a cutoff, check for this exact pattern before you average anything: rows that end in a real event, and rows that just ran out of time. The survival function \(S(t)\) and the hazard function \(h(t)\) you built by hand in this lesson are the two tools built to handle that pattern honestly.
Here is a first look at what a full survival curve looks like, traced here across two different treatment arms, not Maya's subscribers.
That staircase shape is what the next lesson builds from scratch, one event at a time: the Kaplan-Meier estimator.