如何使用 gtsummary 库以不同方式构建描述性统计 table

How to build differently a descriptive statistics table by using the gtsummary library

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

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

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

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

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

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 创建