创建具有子级别(长列表)的 table 摘要统计信息(p.value)

Create a table of summary statistics (with p.value) with sub-levels (long list)

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

我什至可以生成单独的 table 并堆叠它们。不过,我想要一个更理性的策略。

代码

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

以下是获取方法 table:

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**")

reprex package (v1.0.0)

创建于 2021-03-05