将survfit()应用于生存对象列表
我为生存分析模拟了一些右删失数据,并将其存储在一个列表中。我想在此列表上应用将survfit()应用于生存对象列表,r,R,我为生存分析模拟了一些右删失数据,并将其存储在一个列表中。我想在此列表上应用survfit函数。我正在尝试使用lappy,但遇到了一些问题 一些数据: 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
survfit
函数。我正在尝试使用lappy
,但遇到了一些问题
一些数据:
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)
}
解决方案:
surv_object.list <- lapply(cens.list, function(x) Surv(x[, 1], x[, 2]))
lapply(surv_object.list, function(x) survfit(x ~ 1))
surv_object.list您可以忽略公式的生成
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
数据:
set.seed(1)
当我使用相同的种子和相同的复制次数运行此代码时,我会得到不同的输出和此警告消息:在日志(xx)中:NaNs生成了
@user12310746我在数据生成中犯了一些错误,现在可以工作了。
lapply(surv_object.list, function(x) survfit(formula(paste0(x, " ~ 1"))))
surv_object.list <- lapply(cens.list, function(x) Surv(x[, 1], x[, 2]))
lapply(surv_object.list, function(x) survfit(x ~ 1))
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
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)