R-从含地层的Survfit中提取汇总表

R-从含地层的Survfit中提取汇总表,r,dataframe,survival-analysis,R,Dataframe,Survival Analysis,我不熟悉R和生存分析,我有兴趣将survfit的结果导出到一个数据框架中,其中有地层 该场地提供了一个很好的解决方案,但不适用于地层()。如何使用包含地层类型的额外列附加(或堆叠)每个地层。 所提供链接中的解决方案不适用于地层分组 library(survival) data(lung) mod <- with(lung, survfit(Surv(time, status)~ 1)) res <- summary(mod) str(res) # Extract the colum

我不熟悉R和生存分析,我有兴趣将survfit的结果导出到一个数据框架中,其中有地层

该场地提供了一个很好的解决方案,但不适用于地层()。如何使用包含地层类型的额外列附加(或堆叠)每个地层。 所提供链接中的解决方案不适用于地层分组

library(survival)
data(lung)
mod <- with(lung, survfit(Surv(time, status)~ 1))
res <- summary(mod)
str(res)

# Extract the columns you want
cols <- lapply(c(2:6, 8:10) , function(x) res[x])
# Combine the columns into a data frame
tbl <- do.call(data.frame, cols)
str(tbl)
库(生存)
数据(肺)

mod它基本上与您在那里看到的相同,只是增加了一列

res <- summary( survfit( Surv(futime, fustat)~rx, data=ovarian))
cols <- lapply(c(2:6, 8:11) , function(x) res[x])
tbl <- do.call(data.frame, cols)
head(tbl)
#   time n.risk n.event n.censor      surv strata   std.err     upper     lower
# 1   59     13       1        0 0.9230769   rx=1 0.0739053 1.0000000 0.7890186
# 2  115     12       1        0 0.8461538   rx=1 0.1000683 1.0000000 0.6710952
# 3  156     11       1        0 0.7692308   rx=1 0.1168545 1.0000000 0.5711496
# 4  268     10       1        0 0.6923077   rx=1 0.1280077 0.9946869 0.4818501
# 5  329      9       1        0 0.6153846   rx=1 0.1349320 0.9457687 0.4004132
# 6  431      8       1        0 0.5384615   rx=1 0.1382642 0.8906828 0.3255265

res另一个选项是使用
ggfortify

library(survival)
library(ggfortify)

# fit a survival model
mod <- survfit(Surv(futime, fustat) ~ rx, data = ovarian)
# extract results to a data.frame
res <- fortify(mod)

str(res)

'data.frame':   26 obs. of  9 variables:
$ time    : int  59 115 156 268 329 431 448 477 638 803 ...
$ n.risk  : num  13 12 11 10 9 8 7 6 5 4 ...
$ n.event : num  1 1 1 1 1 1 0 0 1 0 ...
$ n.censor: num  0 0 0 0 0 0 1 1 0 1 ...
$ surv    : num  0.923 0.846 0.769 0.692 0.615 ...
$ std.err : num  0.0801 0.1183 0.1519 0.1849 0.2193 ...
$ upper   : num  1 1 1 0.995 0.946 ...
$ lower   : num  0.789 0.671 0.571 0.482 0.4 ...
$ strata  : Factor w/ 2 levels "rx=1","rx=2": 1 1 1 1 1 1 1 1 1 1 ...

library(ggplot2)

ggplot(data = res, aes(x = time, y = surv, color = strata)) +
       geom_line() + 
       # plot censor marks
       geom_point(aes(shape = factor(ifelse(n.censor >= 1, 1, NA)))) + 
       # format censor shape as "+"
       scale_shape_manual(values = 3) + 
       # hide censor legend 
       guides(shape = "none")