user12310746
user12310746

Reputation: 279

Apply survfit() to a list of survival objects

I've simulated some right-censored data for survival analysis, which I've stored into a list. I want to apply the survfit function over this list. I'm trying with lapply but am running into some problems.

Some data:

set.seed(1)
sim_rightcens <- function(n, rate, a, b) {
  
  ## Failure time ~ Exp(scale = 0.4)
  death_time <- rexp(n, rate = rate)
  
  ## Censor time ~ Unif(a = 0, b = 2)
  censor_time <- runif(n, min = a, max = b)
  
  ## Obs time = min(censor_time, death_time)
  observed_time <- pmin(death_time, censor_time)
  
  ## di
  status <- as.numeric(death_time <= censor_time) 
  
  df <- cbind(observed_time, status) 
  return(df)
  
}

1000 right-censored datasets:

cens.list <- replicate(1000, sim_rightcens(n = 200, rate = 0.4, a = 0, b = 2), simplify = FALSE)

My list of right-censored data:

library(survival) 

surv_object.list <- lapply(cens.list, Surv)

What I've tried so far:


lapply(surv_object.list, function(x) survfit(formula(paste0(x, " ~ 1"))))

Solution:

surv_object.list <- lapply(cens.list, function(x) Surv(x[, 1], x[, 2]))

lapply(surv_object.list, function(x) survfit(x ~ 1))

Upvotes: 1

Views: 274

Answers (1)

jay.sf
jay.sf

Reputation: 72623

You may neglect the formula generation.

library(survival) 
lapply(surv_object.list, function(x) survfit(x ~ 1))
# [[1]]
# Call: survfit(formula=x ~ 1)
# 
# n  events  median 0.95LCL 0.95UCL 
# 200.000  52.000   0.953   0.148   0.446 
# 
# [[2]]
# Call: survfit(formula=x ~ 1)
# 
# n  events  median 0.95LCL 0.95UCL 
# 200.000  56.000   0.429   0.111   1.136 
# 
# [[3]]
# Call: survfit(formula=x ~ 1)
# 
# n  events  median 0.95LCL 0.95UCL 
# 200.000  51.000   0.100   0.697   0.132 
# 
# [[4]]
# Call: survfit(formula=x ~ 1)
# 
# n  events  median 0.95LCL 0.95UCL 
# 200.000  66.000   0.360   1.284   0.353 
# 
# [[5]]
# Call: survfit(formula=x ~ 1)
# 
# n  events  median 0.95LCL 0.95UCL 
# 200.000  62.000   0.248   0.428   0.378 

Data:

set.seed(1)
cens.list <- replicate(5, sim_rightcens(n=200, rate=0.4, a=0, b=2), simplify=FALSE)
surv_object.list <- lapply(cens.list, survival::Surv)

Upvotes: 1

Related Questions