r-x表中的Cox回归输出-选择行/列并添加置信区间



我不想将cox回归的输出导出到一个表中,然后可以将该表放入我的文章中。我想最好的方法是使用xtable:

library(survival)
data(pbc)
fit.pbc <- coxph(Surv(time, status==2) ~ age + edema + log(bili) + 
    log(protime) + log(albumin), data=pbc)
summary(fit.pbc)
library(xtable)
xtable(fit.pbc)

现在我想对输出做以下操作:

  • 添加95%的置信区间
  • 选择某些行,比如年龄和日志(protime(
  • 四舍五入exp(B(&CI到三位小数
  • 用z&正则系数

提前感谢!

我首先来看看survival包是如何构造默认打印的表的。

要找到进行打印的函数,请检查适合对象的类,然后为该类寻找打印方法:

class(fit.pbc)
# [1] "coxph"
grep("coxph", methods("print"), value=TRUE)
# [1] "print.coxph"         "print.coxph.null"   
# [3] "print.coxph.penal"   "print.summary.coxph"

看了print.coxph之后,我想到了以下内容:

cox  <- fit.pbc
# Prepare the columns
beta <- coef(cox)
se   <- sqrt(diag(cox$var))
p    <- 1 - pchisq((beta/se)^2, 1)
CI   <- round(confint(cox), 3)
# Bind columns together, and select desired rows
res <- cbind(beta, se = exp(beta), CI, p)
res <- res[c("age", "log(protime)"),]
# Print results in a LaTeX-ready form
xtable(res)
xtable(round(summary(fit.pbc)$conf.int[c(1,3),],3))
#-----------------------------#
% latex table generated in R 2.13.1 by xtable 1.5-6 package
% Sat Oct 15 18:36:04 2011
begin{table}[ht]
begin{center}
begin{tabular}{rrrrr}
  hline
 & exp(coef) & exp(-coef) & lower .95 & upper .95 \ 
  hline
age & 1.04 & 0.96 & 1.02 & 1.06 \ 
  log(bili) & 2.37 & 0.42 & 2.02 & 2.79 \ 
   hline
end{tabular}
end{center}
end{table}

这显示了您在摘要对象上看到的str

str(summary(fit.pbc))
# snipped
 $ conf.int    : num [1:5, 1:4] 1.0404 2.4505 2.3716 10.8791 0.0815 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:5] "age" "edema" "log(bili)" "log(protime)" ...
  .. ..$ : chr [1:4] "exp(coef)" "exp(-coef)" "lower .95" "upper .95"

最新更新