I have a Cox regression which employs strata()
and a tt()
.
Is there any package which helps to produce a well looking, informative output of the results in table format? While ggforest()
handles tt()
it does not handle strata()
I am looking for any way to produce a suitable output other than to print summary(coxph)
. No matter whether its Latex or HTML.
The only solution so far is to build the table myself, but that does not really look appealing enough to be put into a paper....
Here is the dummy data set:
set.seed(132456)
'dummy survival data'
df<-data.frame(id=seq(1,1000,1), event=rep(0,1000),time=floor(runif(1000,7,10)),group=floor(runif(1000,0,2)),
var1 = rnorm(1000, 1, 3), var2 = seq(1,1000))
'set events for a few random subjects'
id_list<-c(as.numeric(floor(runif(500,1,1000))))
df$event[df$id %in% id_list]<-1
'set survival times for events'
t_list<-c(as.numeric(floor(runif(394,1,5))))
df2<-df[df$event==1,]
df2$time<-t_list
'combine data'
df<-rbind(df,df2)
summary(df)
'Set up surfit '
require(survminer)
KM_fit<-coxph(Surv(time , event) ~ tt(var2) + strata(group)+ var1 ,data= df)
ggforest()
returns the following error:
> ggforest(KM_fit)
Error in `[.data.frame`(data, , var) : undefined columns selected
additional warning:
In .get_data(model, data = data) :
The `data` argument is not provided. Data will be extracted from model fit.
The ggforest()
function works if strata
is left out. But the model I deal with uses it.... so no solution.
The following returns a very basic table, which if i knew a bit more about lay-outing could be used, but really its not pretty!
# Prepare the columns
beta <- coef(KM_fit)
se <- sqrt(diag(KM_fit$var))
p <- 1 - pchisq((beta/se)^2, 1)
CI <- round(exp(confint(KM_fit)), 2)
# Bind columns together, and select desired rows
res <- cbind(beta, se = exp(beta), CI, p)
# Print results in a LaTeX-ready form
knitr::kable(
xtable(res)
)
Grateful for any hints and tricks!
Thanks a bunch!
also tried finalfit()
without success....
Here are a few options, depending on what you want to include in your final table.
coxph(Surv(time , event) ~ tt(var2) + strata(group)+ var1 ,data= df) %>%
finalfit::fit2df() %>%
knitr::kable()
coxph(Surv(time , event) ~ tt(var2) + strata(group)+ var1 ,data= df) %>%
finalfit::fit2df(condense = FALSE) %>%
knitr::kable()
coxph(Surv(time , event) ~ tt(var2) + strata(group)+ var1 ,data= df) %>%
broom::tidy(exp = TRUE)
Edit
The digits
argument is for use with the condensed output. If you are outputting values as numeric they do not get rounded until you print. So here are the two options.
> coxph(Surv(time , event) ~ tt(var2) + strata(group)+ var1 ,data= df) %>%
+ finalfit::fit2df(digits = c(3,3,3)) %>%
+ knitr::kable()
|explanatory |HR |
|:-----------|:----------------------------|
|tt(var2) |0.998 (0.995-1.001, p=0.211) |
|var1 |1.006 (0.983-1.029, p=0.616) |
>
> coxph(Surv(time , event) ~ tt(var2) + strata(group)+ var1 ,data= df) %>%
+ finalfit::fit2df(condense = FALSE) %>%
+ knitr::kable(digits = c(0, 2, 3, 4, 5))
|explanatory | HR| L95| U95| p|
|:-----------|----:|-----:|------:|-------:|
|tt(var2) | 1.00| 0.995| 1.0010| 0.21097|
|var1 | 1.01| 0.983| 1.0294| 0.61612|
>
Edit 2
Labeling variables is easy in Finalfit. Trouble is tt()
is not currently supported.
df %>%
mutate(
var1 = ff_label(var1, "Pretty var1"),
var2 = ff_label(var2, "Also very pretty var2"),
group = factor(group) %>%
ff_label("Group (strata)")
) %>%
finalfit("Surv(time, event)", c("var1", "var2", "strata(group)"), column = TRUE)
Dependent: Surv(time, event) all HR (univariable) HR (multivariable)
Pretty var1 Mean (SD) 0.9 (3.1) 1.01 (0.98-1.03, p=0.636) 1.01 (0.98-1.03, p=0.646)
Also very pretty var2 Mean (SD) 504.5 (288.3) 1.00 (1.00-1.00, p=0.479) 1.00 (1.00-1.00, p=0.484)
Group (strata) 0 714 (51.2) - -
1 680 (48.8) - -
As you say, you could just edit the simple table by hand
coxph(Surv(time , event) ~ tt(var2) + strata(group)+ var1 ,data= df) %>%
finalfit::fit2df(condense = FALSE) %>%
mutate(
explanatory = c("Pretty var2 (time dependent)", "Also pretty var1")
) %>%
knitr::kable(digits = c(3,3,3,3,3))
|explanatory | HR| L95| U95| p|
|:----------------------------|-----:|-----:|-----:|-----:|
|Pretty var2 (time dependent) | 0.998| 0.995| 1.001| 0.211|
|Also pretty var1 | 1.006| 0.983| 1.029| 0.616|