How to present interaction variables horizontally in tbl_regression in R

Viewed 40

I want to present the coefficient of interaction horizontally rather than vertically with tbl_regression:

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.6.0'

tbl1 <- trial %>% 
  mutate(early=case_when(stage%in%c("T1","T2")~1,T~0)) %>% 
  glm(response ~ age * early , family = binomial, data=.) %>%
  tbl_regression(
    exponentiate = TRUE  )
tbl1

tbl2 <- trial %>% 
  mutate(late=case_when(stage%in%c("T3","T4")~1,T~0)) %>% 
  glm(response ~ age * late, family = binomial, data=.) %>%
  tbl_regression(
    exponentiate = TRUE  )
tbl2

tbl_stack (list(tbl1,tbl2))

Created on 2022-07-20 by the reprex package (v2.0.1)

I want to have the variables presented horizontally (similar to tbl_merg, but the values are from within the same module), in order to stack different values beneath it.

enter image description here

1 Answers

This requires some customization, but is certainly possible.

library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.6.1'

tbl1 <- 
  trial %>% 
  mutate(early = dplyr::case_when(stage %in% c("T1","T2") ~ 1, TRUE ~ 0)) %>% 
  glm(response ~ age * early , family = binomial, data=.) %>%
  tbl_regression(exponentiate = TRUE)

tbl_final <- 
  1:3 %>%
  purrr::map(
    function(i) {
      tbl1 %>%
        modify_table_body(
          ~ .x %>%
            dplyr::mutate(
              label = label[1],
              variable = variable[1],
              row_type = row_type[1]
            ) %>%
            dplyr::filter(dplyr::row_number() %in% i) 
        )
    }
  ) %>%
  tbl_merge(tab_spanner = c("**Variable**", "**Early**", "**Interaction**"))

enter image description here Created on 2022-07-20 by the reprex package (v2.0.1)

Related