gtsummary:通过两个分类变量汇总一个连续变量

gtsummary: summarizing a continuous variables by two categorical variables

我正在尝试通过两个分类变量总结一个连续变量,如下所示。我无法正确执行此操作。我想知道是否有办法通过 gtsummary 包获得它。谢谢

library("gtsummary")
library("tidyverse")
set.seed(123)
sex <- sample(c("Male", "Female"), size=100, replace=TRUE)
age <- rnorm(n=100, mean=20 + 4*(sex=="F"), sd=0.1)
height <- sample(c("Tall", "short"), size=100, replace=TRUE)
bmi <- rnorm(n=100, mean=10 + 4*(sex=="Female") + 2*(height=="Tall"), sd=1)

dsn <- data.frame(sex, age, bmi, height)


tab <- dsn %>% 
  dplyr::select(age, sex) %>% 
  tbl_summary(by = sex) %>% 
  bold_labels() 
tab

 #Characteristic         Female, N = 43           Male, N = 57  
                   ────────────────────────────────────────────────────────────────
                     age              20.00 (19.93, 20.06)   19.99 (19.94, 20.03)  
                   ────────────────────────────────────────────────────────────────
                     Statistics presented: median (IQR)                            


tab1 <- dsn %>% 
  filter(height == "Tall") %>% 
  dplyr::select(bmi, sex) %>% 
  tbl_summary(by = sex,
              label = list(bmi ~ "....   Tall"))
tab1

 #Characteristic         Female, N = 22           Male, N = 35  
                   ────────────────────────────────────────────────────────────────
                     ....  Tall       15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                   ────────────────────────────────────────────────────────────────
                     Statistics presented: median (IQR) 

tab2 <- dsn %>% 
  filter(height == "Tall") %>% 
  dplyr::select(bmi, sex) %>% 
  tbl_summary(by = sex,
              label = list(bmi ~ "....   Short"))
tab2

 #Characteristic         Female, N = 22           Male, N = 35  
                   ────────────────────────────────────────────────────────────────
                     ....  Short      15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                   ────────────────────────────────────────────────────────────────
                     Statistics presented: median (IQR)  

# I am trying to obtain the table below
tbl_stack(
  list(tab1, tab2, tab),
  group_header = c("BMI", "", ""))

   #Group   Characteristic         Female, N = 22           Male, N = 35  
               ────────────────────────────────────────────────────────────────────────
                 BMI     ....  Tall       15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                         ....  Short      15.54 (15.32, 16.38)   12.09 (11.53, 12.87)  
                         age              20.00 (19.93, 20.06)   19.99 (19.94, 20.03)  
               ────────────────────────────────────────────────────────────────────────
                 Statistics presented: median (IQR)  

#Is there an easy way to do this using the gtsummary package

我们在 gtsummary 中没有这样的功能。但是,我确实写了一个并将其添加到我编写的另一个名为 bstfun 的包中(仅 GitHub)。这是一些 gtsummary 函数开始的地方,它们可能会在以后迁移到包中。

无论如何,这是您获得想要的 table 的方法。

devtools::install_github("ddsjoberg/bstfun")
library(bstfun)

tbl <-
  trial %>%
  tbl_2way_summary(grade, trt, marker)