gpt4 book ai didi

r - 将 coxph 摘要从 R 导出到 csv

转载 作者:行者123 更新时间:2023-12-02 16:41:57 27 4
gpt4 key购买 nike

如何将 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

因为它是一个矩阵,我们可以使用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"

关于r - 将 coxph 摘要从 R 导出到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61482594/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com