You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

1. Is Your ob1-ob5 Classification Correct?

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!).
2. Converting Observations to 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:

ObsEntry TimeExit TimeStatusNotes
ob1199920100Right censored (study end, no event)
ob2NA19992Left censored (event pre-1999)
ob3200220071Left truncated, event occurred in 2007
ob4200320090Left truncated, left study early without event
ob5200520083Left 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.

3. Why is Right Censored Marked as "0"?

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.

4. Inputs for No Event/Observation End, and Handling No Events

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 status 0.

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.

5. When and How to Mark Interval Censored Data

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 start is the last time you confirmed the subject was event-free; end is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:45:37