创建一个带有子级别(长列表)的汇总统计表(带有 p.value)

时间:2021-03-05 13:57:15

标签: r summary inference gtsummary

我需要对 21 个国家/地区的列表进行推论分析,比较性别之间的结果(数字变量)。我已经创建了一个包含以下变量的轴长数据集:性别、国家、结果(数字)。 我正在使用 gtsummary::tbl_strata 和 gtsummary::tbl_summary。我无法创建嵌套来单独运行每个国家/地区。此外,输出已返回国家的 n(%) 个计数(宽格式表格);计算结果变量作为整体。 我把我想要的表格结构放在下面。

enter image description here

我什至可以生成单独的表格并将它们堆叠起来。但是,我想要一个更合理的策略。

代码

library(tidyverse)
library(gtsummary)

# dataframe
df <- 
  data.frame(
    Country = c("Country 1", "Country 2", "Country 3", 
               "Country 1", "Country 2", "Country 3",
               "Country 1", "Country 2", "Country 3",
               "Country 1", "Country 2", "Country 3"),
    Gender = c("M", "M", "M",
                "W", "W", "W",
               "M", "M", "M",
               "W", "W", "W"), 
    Results = c(53, 67, 48,
          56, 58, 72, 
          78, 63, 67,
          54,49,62))
df

# Table
Table <- df %>%
  select(c('Gender',
           'Country',
           'Results')) %>%
  tbl_strata(
    strata = Country,
    .tbl_fun =
      ~.x %>%
  tbl_summary(by = Gender, 
              missing = "no") %>%
  bold_labels() %>%
  italicize_levels() %>%
  italicize_labels())
Table

1 个答案:

答案 0 :(得分:1)

获取该表的方法如下:

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

df <- 
  data.frame(
    Country = c("Country 1", "Country 2", "Country 3", 
                "Country 1", "Country 2", "Country 3",
                "Country 1", "Country 2", "Country 3",
                "Country 1", "Country 2", "Country 3"),
    Gender = c("M", "M", "M",
               "W", "W", "W",
               "M", "M", "M",
               "W", "W", "W"), 
    Results = c(53, 67, 48,
                56, 58, 72, 
                78, 63, 67,
                54,49,62))


theme_gtsummary_mean_sd()
tbl <-
  df %>%
  nest(data = -Country) %>%
  rowwise() %>%
  mutate(
    tbl = 
      data %>%
      tbl_summary(
        by = Gender,
        type = Results ~ "continuous",
        statistic = Results ~ "{mean} ± {sd}",
        label = list(Results = Country)
      ) %>%
      add_p() %>%
      modify_header(list(
        label ~ "**Country**",
        all_stat_cols() ~ "**{level}**"
      )) %>%
      list()
  ) %>%
  pull(tbl) %>%
  tbl_stack() %>%
  modify_spanning_header(all_stat_cols() ~ "**Gender**")

enter image description here reprex package (v1.0.0) 于 2021 年 3 月 5 日创建