【问题标题】:Create a table of summary statistics (with p.value) with sub-levels (long list)创建带有子级别(长列表)的汇总统计表(带有 p.value)
【发布时间】:2021-03-05 13:57:15
【问题描述】:

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

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

代码

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

【问题讨论】:

    标签: r summary inference gtsummary


    【解决方案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**")
    

    reprex package (v1.0.0) 于 2021-03-05 创建

    【讨论】:

    • 很好,丹尼尔。我有点烦你了!然而,我每天都在学习 R 和使用 gtsummary 进行分析上投入更多的精力。这太棒了!
    • 嗨丹尼尔,你好吗?上周我无法按常规工作。现在我又捡起来了。我注意到当我输入我的真实数据库时,国家名称并没有被“复制”。它返回一个代码编号和一个“未知”子行。我试图创建一个从 tibble 到 data.frame 格式的新 df 对象。但是,它没有用。请问,你能指导我吗?
    • missing = "no" 添加到tbl_summary() 调用中
    • 表格库小插图中有一个示例
    • 谢谢丹尼尔。对我来说,回答的一切都令人满意。它是可重现的!
    猜你喜欢
    • 1970-01-01
    • 2020-12-29
    • 2018-10-30
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多