Add OR, with beta/log(OR), and SE in gtsummary

Viewed 73

I want to add OR before log(OR) in the table below:

library(tidyverse)
library(gtsummary)

dat <- 
  iris %>% 
  filter(Species != "setosa") 
  
glm(Species ~ ., family = binomial(), data = dat) %>% 
  tbl_regression() %>% 
  modify_column_hide(columns = ci) %>% 
  modify_column_unhide(columns = std.error)

enter image description here

Any idea or it's not possible? Thanks.

1 Answers

You can use modify_table_body() to add a new column with the OR. Once the new column has been added, you'll need to add a column header and indicate a function to style/format the new column.

library(tidyverse)
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.2'
dat <- 
  iris %>% 
  filter(Species != "setosa") 

tbl <-
  glm(Species ~ ., family = binomial(), data = dat) %>% 
  tbl_regression() %>% 
  # add OR to `.$table_body`
  modify_table_body(
    ~.x %>%
      mutate(estimate_exp = exp(estimate), .before = estimate)
  ) %>%
  # style new column with header and formatting function
  modify_header(estimate_exp ~ "**OR**") %>%
  modify_fmt_fun(estimate_exp ~ style_sigfig) %>%
  # hide CI and show SE
  modify_column_hide(columns = ci) %>% 
  modify_column_unhide(columns = std.error)

enter image description here Created on 2021-09-18 by the reprex package (v2.0.1)

Related