【问题标题】:How to build differently a descriptive statistics table by using the gtsummary library如何使用 gtsummary 库以不同方式构建描述性统计表
【发布时间】:2021-10-28 13:19:20
【问题描述】:

我已经为我正在处理的数据集构建了以下描述性统计表:

我已经通过使用您可以在此处找到的代码获得它。

sum_stats = function(data, group, value, alpha=0.05)data %>%
  group_by(!!enquo(group)) %>%
  summarise(
    n = n(),
    q1 = quantile(!!enquo(value),1/4,8),
    min = min(!!enquo(value)),
    mean = mean(!!enquo(value)),
    median = median(!!enquo(value)),
    q3 = quantile(!!enquo(value),3/4,8),
    max = max(!!enquo(value)),
    sd = sd(!!enquo(value)),
    stderr = sd/sqrt(n),
    kurtosis = e1071::kurtosis(!!enquo(value)),
    skewness = e1071::skewness(!!enquo(value)),
    LCL = mean - qt(1 - (0.05 / 2), n - 1) * stderr,
    UCL = mean + qt(1 -(0.05 / 2), n - 1) * stderr,
    #SW.stat = ShapiroTest(!!enquo(value), alpha)$statistic,
    #SW.p = ShapiroTest(!!enquo(value), alpha)$p.value,
    #SW.test = ShapiroTest(!!enquo(value), alpha)$test,
    nout = length(boxplot.stats(!!enquo(value))$out)
  )

nested_out <- out %>% 
  mutate(COND = factor(COND)) %>%
  group_by(signals) %>% 
  nest() 

stats_nested <- nested_out %>% group_by(signals) %>%
  mutate(stats = map(data, ~sum_stats(.x, COND, value))) %>% 
  unnest(stats) %>% 
  dplyr::select(-'data') %>% 
  flextable() %>% 
  merge_v(j = 'signals') %>% 
  colformat_double(digits = 2)

如果我想通过使用gtsummary() package 获得相同的输出,我应该怎么做?

在这里您可以找到数据集的简短摘录

structure(list(ID = c("01", "01", "01", "01", "01", "01"), GR = c("RP", 
"RP", "RP", "RP", "RP", "RP"), SES = c("V", "V", "V", "V", "V", 
"V"), COND = c("NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", "NEG-CTR", 
"NEG-CTR"), signals = c("P3(400-450).FCz", "P3(400-450).Cz", 
"P3(400-450).Pz", "LPPearly(500-700).FCz", "LPPearly(500-700).Cz", 
"LPPearly(500-700).Pz"), value = c(-11.6312151716924, -5.16524399006139, 
11.8802266972569, -11.7785042972793, -5.96429031525769, 8.23981597718437
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

提前感谢那些愿意回答的人。

【问题讨论】:

    标签: r gtsummary


    【解决方案1】:

    gtsummary 包通常会将多个统计信息放在单个列中。如果您希望将每个统计信息放在单独的列中,您可以为每个汇总统计信息重复调用tbl_summary()。然后,您将拥有tbl_summary() 对象的列表,您可以将它们与tbl_merge() 合并到一个表中。下面的例子!

    library(gtsummary)
    #> #Uighur
    packageVersion("gtsummary")
    #> [1] '1.5.0'
    
    # iterate over summary statistics
    c("mean", "sd", "min", "max") %>%
      lapply(
        function(.x) {
          tbl_summary(
            data = trial, 
            statistic = everything() ~ paste0("{", .x, "}"),
            include = c(age, marker),
            missing = "no"
          ) %>%
            modify_header(all_stat_cols() ~ glue::glue("**{.x}**"))
        }
      ) %>%
      # merge all tables to get one column per summary statistic
      tbl_merge() %>%
      # remove the default headers and footnotes
      modify_spanning_header(everything() ~ NA) %>%
      modify_footnote(everything() ~ NA) %>%
      as_kable() # convert to kable to display on SO
    
    Characteristic mean sd min max
    Age 47 14 6 83
    Marker Level (ng/mL) 0.92 0.86 0.00 3.87

    reprex package (v2.0.1) 于 2021 年 10 月 28 日创建

    【讨论】:

    • 嗯...我尝试替换有趣的变量,但它不起作用
    • 正如您在我的示例表中可以看到的那样,在您建议的适当示例中,有两个因子变量(信号和每个信号的不同条件),您也应该考虑到这一点。谢谢
    • 晚上好,我认为最好的办法是将数据集传递为宽格式。但是我还没有弄清楚如何根据每个条件(实际上是三个)对这些统计数据进行分组。
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多