Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regression coefficient with 95%CI in one column using tbl_regression function in R?

Tags:

r

regression

glm

To test whether there is an association between disease groups (categorical_variable) and a disease (outcome; dichotomous) I am running a logistic regression. To check for confounding by other variables I am running 3 models with various amounts of covariates.

To display the ORs and CIs i am using the tbl_regression function from the package gtsummary (and I used the dplyr function mutate to display CIs in round brackets). However, this displays the CI in a seperate column from the OR, but I want them in the same column with the CI in round brackets after the OR.

My code:

library(gtsummary)
library(dplyr)

Model 1 <-  
  glm(data=wide_dataset, formula=outcome ~ categorical_variable, 
      family=binomial(link="logit") %>%
  tbl_regression(
    exponentiate = TRUE) %>% 
  # remove the p-value column
  modify_column_hide(column=p.value) %>%
  # CI in round brackets:
  modify_table_body(mutate, 
                    ci=gsub("(\\d\\.\\d{,4})(, )(\\d\\.\\d{,4})"
                            ,"\\(\\1 \\3\\)",
                            ci))

Model 1

Thanks in advance!

like image 648
tcvdb1992 Avatar asked Jan 31 '26 20:01

tcvdb1992


1 Answers

It's much easier to put the odds ratio and the CI in the same column using the dev version of the package (will be released to CRAN next week).

If you use the JAMA journal theme, the OR and the CI will automatically be combined into a single column.

remotes::install_github("ddsjoberg/gtsummary")
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.3.7.9016'

# set the JAMA theme to display OR and CI in same column
theme_gtsummary_journal("jama")
#> Setting theme `JAMA`

tbl <-
  glm(response ~ age + grade, data = trial, family = binomial) %>%
  tbl_regression(exponentiate = TRUE) %>%
  modify_column_hide(p.value)

enter image description here Created on 2021-04-07 by the reprex package (v2.0.0)

This code could also be used without setting the JAMA journal theme.

tbl <-
  glm(response ~ age + grade, data = trial, family = binomial) %>%
  tbl_regression(exponentiate = TRUE) %>%
  # merge OR and CI into single column
  modify_table_styling(
    column = estimate,
    rows = !is.na(estimate),
    cols_merge_pattern = "{estimate} ({conf.low} to {conf.high})"
  ) %>%
  modify_header(estimate ~ "**OR (95% CI)**") %>%
  modify_column_hide(c(ci, p.value))

It sounds like you want to compare multiple models. Here's how you can put them in the same table.

list(tbl, tbl, tbl) %>%
  tbl_merge(tab_spanner = paste0("**Model ", 1:3, "**"))

enter image description here

like image 192
Daniel D. Sjoberg Avatar answered Feb 02 '26 11:02

Daniel D. Sjoberg