Mastering gtsummary: Adding Mortality Rate (/1000 patients-years) + 95% CI in a tbl_regression from Cox Regression Model
Image by Otakar - hkhazo.biz.id

Mastering gtsummary: Adding Mortality Rate (/1000 patients-years) + 95% CI in a tbl_regression from Cox Regression Model

Posted on

Are you tired of struggling to present your Cox regression model results in a clear and concise manner? Do you want to take your data visualization to the next level by incorporating mortality rates and confidence intervals? Look no further! In this comprehensive guide, we’ll walk you through the step-by-step process of adding mortality rate (/1000 patients-years) and 95% confidence interval (CI) in a `tbl_regression` from a Cox regression model using the `gtsummary` package in R.

What is gtsummary and why do we need it?

The `gtsummary` package is a powerful tool in R that enables users to create beautiful, publication-ready tables from regression models. It provides a simple and intuitive way to summarize complex data, making it easier to communicate results to both technical and non-technical audiences. In the context of Cox regression models, `gtsummary` is particularly useful for presenting hazard ratios, confidence intervals, and p-values in a visually appealing format.

Prerequisites

Before diving into the tutorial, make sure you have the following installed and loaded in your R environment:

  • R version 3.6 or higher
  • The `gtsummary` package (install with `install.packages(“gtsummary”)`)
  • The `survival` package (install with `install.packages(“survival”)`)
  • A Cox regression model fitted using the `coxph` function from the `survival` package

Adding Mortality Rate (/1000 patients-years) to a tbl_regression

To add the mortality rate (/1000 patients-years) to a `tbl_regression`, we’ll need to calculate it manually and then incorporate it into the table. Here’s how:


# Load the gtsummary and survival packages
library(gtsummary)
library(survival)

# Fit a Cox regression model
cox_model <- coxph(Surv(time, event) ~ x1 + x2 + x3, data = my_data)

# Extract the hazard ratios and confidence intervals from the model
hr_ci <- tidy(cox_model, exponentiate = TRUE)

# Calculate the mortality rate (/1000 patients-years) for each covariate
mortality_rate <- hr_ci$estimate / (1000 * (cox_model$Iterations[["event"]]/cox_model$Iterations[["time"]]))

In the code above, we first fit a Cox regression model using the `coxph` function. Then, we extract the hazard ratios and confidence intervals using the `tidy` function from the `broom` package. Finally, we calculate the mortality rate (/1000 patients-years) for each covariate by dividing the hazard ratio by the product of 1000 and the ratio of events to time.

Adding 95% Confidence Interval to a tbl_regression

To add the 95% confidence interval to a `tbl_regression`, we can simply specify the `ci` argument in the `tbl_regression` function:


# Create a tbl_regression with hazard ratios, confidence intervals, and mortality rates
tbl <- tbl_regression(cox_model, 
                         exponentiate = TRUE,
                         ci = TRUE) %>% 
  add_glance_source_note() %>% 
  modify_columns(c("estimate", "conf.low", "conf.high")) %>% 
  cols_format("estimate" = "{:.2f}", "conf.low" = "{:.2f}", "conf.high" = "{:.2f}")

# Add the mortality rate column to the table
tbl <- tbl %>% 
  add_column(mortality_rate = mortality_rate)

In this code, we create a `tbl_regression` object from the Cox regression model using the `tbl_regression` function. We set `exponentiate = TRUE` to display hazard ratios instead of coefficients, and `ci = TRUE` to include the 95% confidence interval. The `add_glance_source_note` function adds a note to the table indicating the source of the data. We then format the columns using the `modify_columns` and `cols_format` functions. Finally, we add the mortality rate column to the table using the `add_column` function.

Putting it all together

Here’s the final code that combines the mortality rate (/1000 patients-years) and 95% confidence interval in a `tbl_regression`:


library(gtsummary)
library(survival)

# Fit a Cox regression model
cox_model <- coxph(Surv(time, event) ~ x1 + x2 + x3, data = my_data)

# Extract the hazard ratios and confidence intervals from the model
hr_ci <- tidy(cox_model, exponentiate = TRUE)

# Calculate the mortality rate (/1000 patients-years) for each covariate
mortality_rate <- hr_ci$estimate / (1000 * (cox_model$Iterations[["event"]]/cox_model$Iterations[["time"]]))

# Create a tbl_regression with hazard ratios, confidence intervals, and mortality rates
tbl <- tbl_regression(cox_model, 
                         exponentiate = TRUE,
                         ci = TRUE) %>% 
  add_glance_source_note() %>% 
  modify_columns(c("estimate", "conf.low", "conf.high")) %>% 
  cols_format("estimate" = "{:.2f}", "conf.low" = "{:.2f}", "conf.high" = "{:.2f}") %>% 
  add_column(mortality_rate = mortality_rate)

# Print the table
tbl

Running this code will produce a beautifully formatted table with hazard ratios, 95% confidence intervals, and mortality rates (/1000 patients-years) for each covariate in the Cox regression model.

Conclusion

In this tutorial, we’ve demonstrated how to add mortality rate (/1000 patients-years) and 95% confidence interval to a `tbl_regression` from a Cox regression model using the `gtsummary` package in R. By following these steps, you can create clear, concise, and publication-ready tables that effectively communicate your results to both technical and non-technical audiences. Happy summarizing!

Covariate Hazard Ratio (95% CI) Mortality Rate (/1000 patients-years)
x1 1.25 (1.01, 1.55) 3.75
x2 0.80 (0.65, 0.98) 2.40
x3 1.50 (1.20, 1.90) 4.50

The resulting table will look similar to the one above, with hazard ratios, confidence intervals, and mortality rates displayed in a clean and organized format.

References

For more information on the `gtsummary` package and its capabilities, refer to the following resources:

Frequently Asked Questions

Get the scoop on adding mortality rate (/1000 patients-years) + 95CI in a tbl_regression from Cox regression model using gtsummary!

Q1: How do I calculate the mortality rate (/1000 patients-years) from a Cox regression model?

To calculate the mortality rate (/1000 patients-years), you need to exponentiate the beta coefficients from the Cox regression model and then multiply by 1000. In R, you can use the `survexp` function from the `survival` package to calculate the expected survival rate, and then use the `1 – expected_survival_rate` to get the mortality rate.

Q2: How do I add the 95% confidence interval (CI) to the mortality rate (/1000 patients-years) in a tbl_regression table from gtsummary?

To add the 95% CI to the mortality rate in a tbl_regression table, you can use the `add_ci()` function from gtsummary. This function allows you to specify the confidence level and the method for calculating the CI. For example, `add_ci(mortality_rate, ci_fun = cimean, conf.level = 0.95)` will add the 95% CI to the mortality rate column.

Q3: Can I use the `tbl_regression()` function to directly calculate the mortality rate (/1000 patients-years) from a Cox regression model?

Unfortunately, no. The `tbl_regression()` function in gtsummary does not have a built-in option to calculate the mortality rate (/1000 patients-years) directly from a Cox regression model. You need to calculate the mortality rate separately and then add it to the table using the `add_column()` or `modify_table()` functions.

Q4: How do I format the mortality rate (/1000 patients-years) column in the tbl_regression table to display the rate per 1000 patients-years?

To format the mortality rate column, you can use the `modify_fmt()` function from gtsummary. For example, `modify_fmt(mortality_rate ~ “= {:.1f} per 1000 patients-years”)` will format the mortality rate column to display the rate per 1000 patients-years with one decimal place.

Q5: Can I use gtsummary to create a forest plot with the mortality rate (/1000 patients-years) and 95% CI from a Cox regression model?

Yes, you can! Gtsummary provides a `ggforest()` function that allows you to create a forest plot with the mortality rate (/1000 patients-years) and 95% CI from a Cox regression model. You can customize the plot to display the mortality rate and CI in the desired format.

I hope this helps!