2

如何将 cox_proportional 危害模型的摘要从 R 导出到 csv。我通过函数 coxph 进行了测试。通过生存包现在我想将其摘要导出到 csv,如何做到这一点。

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

2 回答 2

3

我认为您需要使用tidy()broom中的功能。请注意,与tidy概念一致,列的调用方式与 Cox 模型的原始摘要不同。

您可以在此处阅读有关该软件包的更多信息。

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 包(v0.3.0)于 2020 年 4 月 28 日创建

于 2020-04-28T15:03:47.033 回答
3

?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

由于它是 a matrix,我们可以使用write.csvorwrite.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"
于 2020-04-28T14:59:38.930 回答