I would like to display results of two regression analyses next to each other, let's say a logistic and a COX regression. Variables are presented in rows and corresponding data for p, OR/HR and confidence interval in columns. So, the column names don't match: it's OR on the left, HR on the right.
I tried cbind, but I'm encountering the following problems:
(1) What if one variable (row) is only present in one table due to variable selection? In that case, there would have to a blank row in the other table upon combination of the two.
(2) After both tables are combined, how can you add a header spanning over the four columns of each table in order to label them as logistic or COX?
The output that I'm looking for that is to be displayed as a latex table via xtable:
| Logistic regression | COX regression
| p | OR | 2.5% | 97.5% | p | HR | 2.5% | 97.5%
v1 | 0.849 | 0.936 | ... | ... | | | |
v2 | 0.249 | 0.595 | ... | ... | 0.026 | 1.916 | ... | ...
v3 | | | | | 0.023 | 0.140 | ... | ...
Here is some sample code:
library(xtable);library(survival)
# Creating a sample data frame
set.seed(1234)
event <- as.numeric(round(rnorm(10,5,5))>1);tte<-abs(round(rnorm(10,5,5)))
v1 <- round(rnorm(10,5,5));v2 <- round(rnorm(10,2,5));v3 <- round(rnorm(10,1,5))
df<-data.frame(event, tte, v1, v2, v3)
# Some logistic regression ...
LogReg <- glm(event~v1+v2,family=binomial,data=df)
LogRegTable<-round(cbind(summary(LogReg)$coef[, "Pr(>|z|)"], exp(coef(LogReg)), exp(confint(LogReg))),3)
colnames(LogRegTable)<-c("p","OR","2.5%","97.5%")
LogRegTable<-LogRegTable[!rownames(LogRegTable)=="(Intercept)",]
# ... and some COX regression
CoxReg <- coxph(Surv(df$tte, df$event)~v2+v3,data=df)
CoxRegTable<-round(cbind(summary(CoxReg)$coef[, "Pr(>|z|)"], exp(coef(CoxReg)), exp(confint(CoxReg))),3)
colnames(CoxRegTable)<-c("p","HR","2.5%","97.5%")
# There we go
LogRegTable
CoxRegTable
# Now, how to get them in one table?
xtable(
cbind(LogRegTable, CoxRegTable)
)
# ... messes up the correct row names
merge()
would be better thancbind
. If you want fancy tables, consider using a package likextables
orstargazer
to make LaTeX output (or write code to make the LaTeX exactly how you want it). There's simply not enough information here to make more specific recommendations (consider adding sample data and code you've tried) – MrFlick