将 coxph 摘要从 R 导出到 csv

Export coxph summary from R to csv

如何将 cox_proportional 危险模型的摘要从 R 导出到 csv。我 运行 通过函数 coxph 进行测试。 通过生存包 现在我想把它的摘要导出到csv,怎么办。

c <- coxph(Surv(x~y)) 
summary(c)

根据 ?coxph 示例,我将使用:

library(survival)
test1 <- list(time=c(4,3,1,1,2,2,3), 
              status=c(1,1,1,0,1,1,0), 
              x=c(0,2,1,1,1,0,0), 
              sex=c(0,0,0,0,1,1,1)) 
mdl <- coxph(Surv(time, status) ~ x + strata(sex), test1)
mdl_summ <- summary(mdl)
mdl_summ
# Call:
# coxph(formula = Surv(time, status) ~ x + strata(sex), data = test1)
#   n= 7, number of events= 5 
# Warning: partial match of 'coef' to 'coefficients'
#     coef exp(coef) se(coef)     z Pr(>|z|)
# x 0.8023    2.2307   0.8224 0.976    0.329
#   exp(coef) exp(-coef) lower .95 upper .95
# x     2.231     0.4483    0.4451     11.18
# Concordance= 0.667  (se = 0.167 )
# Rsquare= 0.144   (max possible= 0.669 )
# Likelihood ratio test= 1.09  on 1 df,   p=0.3
# Wald test            = 0.95  on 1 df,   p=0.3
# Score (logrank) test = 1.05  on 1 df,   p=0.3

如果我们看一下 str 的结构:

str(mdl_summ)
# List of 14
#  $ call        : language coxph(formula = Surv(time, status) ~ x + strata(sex), data = test1)
#  $ fail        : NULL
#  $ na.action   : NULL
#  $ n           : int 7
#  $ loglik      : num [1:2] -3.87 -3.33
#  $ nevent      : num 5
#  $ coefficients: num [1, 1:5] 0.802 2.231 0.822 0.976 0.329
#   ..- attr(*, "dimnames")=List of 2
#   .. ..$ : chr "x"
#   .. ..$ : chr [1:5] "coef" "exp(coef)" "se(coef)" "z" ...
#  $ conf.int    : num [1, 1:4] 2.231 0.448 0.445 11.18
#   ..- attr(*, "dimnames")=List of 2
#   .. ..$ : chr "x"
#   .. ..$ : chr [1:4] "exp(coef)" "exp(-coef)" "lower .95" "upper .95"
#  $ logtest     : Named num [1:3] 1.087 1 0.297
#   ..- attr(*, "names")= chr [1:3] "test" "df" "pvalue"
#  $ sctest      : Named num [1:3] 1.051 1 0.305
#   ..- attr(*, "names")= chr [1:3] "test" "df" "pvalue"
#  $ rsq         : Named num [1:2] 0.144 0.669
#   ..- attr(*, "names")= chr [1:2] "rsq" "maxrsq"
#  $ waldtest    : Named num [1:3] 0.95 1 0.329
#   ..- attr(*, "names")= chr [1:3] "test" "df" "pvalue"
#  $ used.robust : logi FALSE
#  $ concordance : Named num [1:2] 0.667 0.167
#   ..- attr(*, "names")= chr [1:2] "C" "se(C)"
#  - attr(*, "class")= chr "summary.coxph"

我们看到有一个 coefficients 属性 我们可以使用。

class(mdl_summ$coefficients)
# [1] "matrix"
mdl_summ$coefficients
#                                 coef exp(coef)  se(coef)         z  Pr(>|z|)
# x                          0.7811819  2.184052 0.7975689 0.9794538 0.3273558
# survival::strata(sex)sex=1 0.9337832  2.544116 1.4081100 0.6631465 0.5072367

因为它是 matrix,我们可以使用 write.csvwrite.table 或其任何同类:

write.csv(mdl_summ$coefficients, "surv.csv")
readLines("surv.csv")
# [1] "\"\",\"coef\",\"exp(coef)\",\"se(coef)\",\"z\",\"Pr(>|z|)\""                                   
# [2] "\"x\",0.802317911238375,2.23070551803984,0.822376639082848,0.975608830685119,0.329258346777417"

编辑:为了您在模型列表上的扩展。

testlist <- list(a=test1, b=test1) # in your code, use `split(DF, DF$Group)`
mdls <- sapply(testlist, function(z) coxph(Surv(time, status) ~ x + strata(sex), data = z), simplify = FALSE)
mdls_summ <- lapply(mdls, summary)
lapply(mdls_summ, `[[`, "coefficients")
# $a
#        coef exp(coef)  se(coef)         z  Pr(>|z|)
# x 0.8023179  2.230706 0.8223766 0.9756088 0.3292583
# $b
#        coef exp(coef)  se(coef)         z  Pr(>|z|)
# x 0.8023179  2.230706 0.8223766 0.9756088 0.3292583
ign <- Map(function(dat, nm) write.csv(dat$coefficients, paste0(nm, ".csv")),
           mdls_summ, names(mdls_summ))
list.files(pattern = "*.csv")
# [1] "a.csv"    "b.csv"

我认为您需要使用 broom 包中的函数 tidy()。请注意,根据 tidy 概念,列的调用方式不同 w.r.t。 Cox 模型的原始摘要。

您可以阅读有关该软件包的更多信息 here

library(survival)
library(broom)

data("lung")

res.cox <- coxph(Surv(time, status) ~ sex, data = lung)
out = tidy(res.cox)

class(out)
#> [1] "tbl_df"     "tbl"        "data.frame"
out
#> # A tibble: 1 x 7
#>   term  estimate std.error statistic p.value conf.low conf.high
#>   <chr>    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
#> 1 sex     -0.531     0.167     -3.18 0.00149   -0.859    -0.203

write.csv(out, file = "~/Desktop/out.csv" )

reprex package (v0.3.0)

于 2020-04-28 创建