Cox regression output in xtable - choosing rows/columns and adding a confidence interval
Asked Answered
C

2

6

I wan't to export the output from a cox regression to a table that I then can put into my article. I guess the best way to go about it is with 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)

Now I want to do the following to the output:

  • Add confidence interval (CI) of 95 %
  • Select certain rows, say age and log(protime)
  • Round the exp(B) & CI to three decimals
  • Remove the column with z & regular coef

Thanks in advance!

Cittern answered 15/10, 2011 at 21:9 Comment(0)
T
9

I'd approach this by first taking a look at how the survival package constructs the table it prints by default.

To find the function that does that printing, examine the class of your fit object, and then look for a print method for that class:

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

After taking a look at print.coxph, here's what I came up with:

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)
Twostep answered 15/10, 2011 at 22:36 Comment(1)
Thanks, exactly the simple solution that I was looking for :)Cittern
R
3
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}

This shows what you see with str on the summary-object

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"
Repulsion answered 15/10, 2011 at 22:37 Comment(3)
You can also use the digits argument to xtable rather than sending it through round.Codycoe
I think summary already rounded to 2 digits. (But there is rarely enough precision to justify more than 2 digits anyway.)Repulsion
Thanks for the effort, chose Josh's answer since it solves all my issues. @DWin - I specified 3 digits because summary rounds to 2 digits - I have a very large dataset and I haven't yet decided on the amount of decimals although I completely agree that many decimals give a false perception of statistics being a more precise than they are.Cittern

© 2022 - 2024 — McMap. All rights reserved.