R语言surv()函数:删失数据分类与输入设置咨询
Hey there! Let's walk through your survival analysis questions one by one—I’ve spent plenty of time tinkering with the survival package in R, so this should be right up my alley.
Most of your calls are spot-on, but let’s clarify one key mix-up between interval censoring and other statuses, plus reinforce the difference between censoring and truncation:
- ob1: Right censored (0) ✅ Correct—no event occurred by the end of the study (2010), so this is standard right censoring.
- Left censored: Your definition (event happened before study start) is exactly right. For example, if we only learn an event occurred pre-1999 but don’t know when, that’s left censored (status 2).
- Event (marked x): Status 1 ✅ Correct—this applies when we have an exact time for the outcome (like death or disease onset).
- Interval censored: Your description needs a tweak. Interval censoring only applies when we know the event happened between two time points but don’t have the exact time (e.g., ob5 was alive in 2005, dead when we checked again in 2008—we can’t pin down the exact death date). If ob5 left the study early without an event, that’s right censored (0), not interval.
- Left truncated: ob3, ob4, ob5 ✅ Correct—these individuals joined the study after it started (1999), so we can’t observe anything about them pre-entry (this is truncation, not censoring—important distinction for model fitting!).
surv() Inputs The Surv() function is flexible, but you need to match the input structure to each observation type. Let’s use your 1999-2010 study window to build a concrete example data frame, then create the survival object:
First, here’s how to map each observation:
| Obs | Entry Time | Exit Time | Status | Notes |
|---|---|---|---|---|
| ob1 | 1999 | 2010 | 0 | Right censored (study end, no event) |
| ob2 | NA | 1999 | 2 | Left censored (event pre-1999) |
| ob3 | 2002 | 2007 | 1 | Left truncated, event occurred in 2007 |
| ob4 | 2003 | 2009 | 0 | Left truncated, left study early without event |
| ob5 | 2005 | 2008 | 3 | Left truncated, interval censored (event between 2005-2008) |
Now the R code:
library(survival) # Build your dataset surv_df <- data.frame( obs = paste0("ob", 1:5), entry = c(1999, NA, 2002, 2003, 2005), exit = c(2010, 1999, 2007, 2009, 2008), status = c(0, 2, 1, 0, 3) ) # Create the survival object # Use type = "interval" to handle truncation and interval censoring surv_obj <- with(surv_df, Surv(time = entry, time2 = exit, event = status, type = "interval")) # Check the result surv_obj
The type = "interval" argument is critical here—it lets Surv() interpret the entry/exit pair as a time window, paired with the status code to define what happened in that window.
This is just a standard convention in the survival package, rooted in how survival analysis was first implemented statistically. Right censoring is the most common scenario (most subjects don’t experience the event by study end), so it gets the simplest code: 0 = no event observed (censored). The other codes are reserved for less frequent cases: 1 = event, 2 = left censored, 3 = interval censored. You can customize these codes with the event argument, but sticking to the default makes your code easier for others to read.
First, a quick reality check: every observation should have an exit time—either the event time, the last follow-up time, or the study end date. If you have a record without an exit time, that’s a data cleaning issue:
- Treat it as right censored, using the study end date (or last known follow-up date) as
exit, with status0.
If your entire dataset has no events at all (all censored), you can’t fit standard survival models like Cox regression—these models need events to estimate risk ratios. Instead, you can only summarize the distribution of survival times (e.g., median survival time equals the study end date) and double-check if you’re missing event data or if the study period was too short.
Mark interval censored data only when you can’t pinpoint the exact event time, but know it fell between two dates. Common scenarios include periodic follow-ups (e.g., annual check-ins where you miss the exact event date between visits).
How to mark it:
- Use
Surv(time = start_of_interval, time2 = end_of_interval, event = 3, type = "interval") - The
startis the last time you confirmed the subject was event-free;endis the first time you confirmed the event occurred.
Example code:
# Interval censored example: 2 subjects with events between follow-ups interval_df <- data.frame( id = 1:2, last_alive = c(2001, 2004), first_dead = c(2005, 2007), status = c(3, 3) # 3 = interval censored ) # Create survival object interval_surv <- with(interval_df, Surv(last_alive, first_dead, status, type = "interval")) interval_surv
Note: If the event happened before the study started (e.g., interval is NA to 1999), that’s left censoring (status 2), not interval censoring (status 3).
内容的提问来源于stack exchange,提问作者Juan

