如何使用 R 包“gtsummary”在摘要 table 中添加效果大小?

How to add the effect size in the summary table using R package “gtsummary”?

我正在尝试使用“gtsummary”包的 add_stat 函数将 wilcox 检验的有效大小添加到摘要 table 中。 我的数据如下:

Type <- c ("FND", "FND", "FND", "FND", "FND", "FND", "FND", "FND","FND", "FND",
                           "HC","HC","HC","HC","HC","HC","HC","HC","HC","HC")
Component1 <- c(2,3,2,2,1,0,1,2,1,2,1,0,0,0,1,1,2,0,1,1)
Component2 <- c(1,3,3,3,2,0,2,3,3,2,2,0,0,0,0,1,2,1,1,0)
Component3 <- c(0,1,3,2,0,1,2,2,0,1,0,0,1,1,0,1,1,0,0,0)

data_components <- data.frame(Type, Component1, Component2, Component3)

data_components_tbl <- data_components %>%
  tbl_summary(
    by = Type,
    type = list(Component1 ~ "continuous", Component2 ~ "continuous", Component3 ~ "continuous"), #define Components as continuous for analyse mean
    statistic = list(all_continuous() ~ "{mean} ({sd})",
                     all_categorical() ~ "{n} / {N} ({p}%)"),
    digits = all_continuous() ~ 2,
    label = list(Component1 ~ "Subjective sleep quality",
                 Component2 ~ "Sleep latency",
                 Component3 ~ "Sleep duration")
  ) %>%
  add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
  modify_header(update = list(label ~ "**Variable**")) %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Group**") %>%
  modify_footnote(
    all_stat_cols() ~ "Mean (SD)")%>%
  bold_labels()
data_components_tbl 

我试过这个功能:

my_ES_test <- function(data, variable, by, ...) {
  (data%>%  
     rstatix::wilcox_effsize(data[[variable]] ~ as.factor(data[[by]])))$effsize
}


data_components_tbl <- data_components %>%
  tbl_summary(
    by = Type,
    type = list(Component1 ~ "continuous", Component2 ~ "continuous", Component3 ~ "continuous"), #define Components as continuous for analyse mean
    statistic = list(all_continuous() ~ "{mean} ({sd})",
                     all_categorical() ~ "{n} / {N} ({p}%)"),
    digits = all_continuous() ~ 2)%>%
  add_p(pvalue_fun = ~style_pvalue(.x, digits = 2)) %>%
  add_stat(fns = everything() ~ my_ES_test()) %>%
  modify_header(update = list(label ~ "**Variable**")) %>%
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Group**") %>%
  modify_footnote(
    all_stat_cols() ~ "Mean (SD)")%>%
  bold_labels()
data_components_tbl

我想我没有为 my_ES_test 函数使用正确的语法。有什么办法吗?

感谢帮助! 亲爱的 aylaxla

我对你的ES函数​​做了一点修改。见下文!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.4.0.9000'

my_ES_test <- function(data, variable, by, ...) {
  rstatix::wilcox_effsize(data, as.formula(glue::glue("{variable} ~ {by}")))$effsize
}
my_ES_test(trial, "age", "trt")
#> Effect size (r) 
#>      0.02633451

tbl <-
  trial %>%
  select(age, marker, trt) %>%
  tbl_summary(
    by = trt, 
    statistic = all_continuous() ~ "{mean} ({sd})",
    missing = "no"
  ) %>%
  add_stat(fns = all_continuous() ~ my_ES_test) %>%
  modify_header(add_stat_1 ~ "**Wilcoxon ES**")

reprex package (v2.0.0)

于 2021-04-26 创建