问题
How to export summary of cox_proportional hazard model from R to csv. I ran a test by function coxph. by survival package Now i want to export its summary to csv, how to do that.
c <- coxph(Surv(x~y))
summary(c)
回答1:
From the ?coxph
examples, I'll use:
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
If we look at the str
ucture of that:
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"
we see that there is a coefficients
property we can use.
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
Since it's a matrix
, we can use write.csv
or write.table
or any of its kin:
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"
Edit: for your extension of doing this on a list of models.
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"
回答2:
I think you need to use the function tidy()
from broom package. Beware that in agreement with the tidy concept, columns are called in a different way w.r.t. the original summary of the Cox model.
You can read more about the package 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" )
Created on 2020-04-28 by the reprex package (v0.3.0)
来源:https://stackoverflow.com/questions/61482594/export-coxph-summary-from-r-to-csv