Jim*_*myR 5 r dataframe survival-analysis
我是R和生存分析的新手,我有兴趣将幸存的结果输出到数据框中.
这个网站提供了一个很好的解决方案,但不是一个有分层的(https://stat.ethz.ch/pipermail/r-help/2014-October/422348.html).如何使用包含分层类型的额外列附加(或堆叠)每个层.提供的链接中的解决方案不适用于分层分组
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)
Run Code Online (Sandbox Code Playgroud)
谢谢先进,R新手
pic*_*ick 10
它与你在那里基本相同,只是一个额外的列
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
Run Code Online (Sandbox Code Playgroud)
我看到的最简单的方法是使用包tidy()中的函数broom。
library(survival)\nlibrary(dplyr)\n#> \n#> Attache Paket: \'dplyr\'\n#> The following objects are masked from \'package:stats\':\n#> \n#> filter, lag\n#> The following objects are masked from \'package:base\':\n#> \n#> intersect, setdiff, setequal, union\nlibrary(broom)\n\nsurvfit( Surv(futime, fustat)~rx, data=ovarian) %>% \n tidy()\n#> # A tibble: 26 x 9\n#> time n.risk n.event n.censor estimate std.error conf.high conf.low strata\n#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> \n#> 1 59 13 1 0 0.923 0.0801 1 0.789 rx=1 \n#> 2 115 12 1 0 0.846 0.118 1 0.671 rx=1 \n#> 3 156 11 1 0 0.769 0.152 1 0.571 rx=1 \n#> 4 268 10 1 0 0.692 0.185 0.995 0.482 rx=1 \n#> 5 329 9 1 0 0.615 0.219 0.946 0.400 rx=1 \n#> 6 431 8 1 0 0.538 0.257 0.891 0.326 rx=1 \n#> 7 448 7 0 1 0.538 0.257 0.891 0.326 rx=1 \n#> 8 477 6 0 1 0.538 0.257 0.891 0.326 rx=1 \n#> 9 638 5 1 0 0.431 0.340 0.840 0.221 rx=1 \n#> 10 803 4 0 1 0.431 0.340 0.840 0.221 rx=1 \n#> # \xe2\x80\xa6 with 16 more rows\nRun Code Online (Sandbox Code Playgroud)\n由reprex 包(v0.3.0)于 2021-08-04 创建
\n另一种选择是使用该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 ...
Run Code Online (Sandbox Code Playgroud)
这种方法的优点是你可以获得完整的数据(即26个观察而不是12个),你可以用你的生存曲线绘制ggplot.
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")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5523 次 |
| 最近记录: |