【问题标题】:How to add the effect size in the summary table using R package “gtsummary”?如何使用 R 包“gtsummary”在汇总表中添加效果大小?
【发布时间】:2021-07-19 14:28:42
【问题描述】:

我正在尝试使用“gtsummary”包的 add_stat 函数将 wilcox 测试的有效大小添加到汇总表中。 我的数据如下:

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 函数使用正确的语法。有没有办法做到这一点?

感谢您的帮助! 亲爱的艾拉拉

【问题讨论】:

    标签: r size effect gtsummary


    【解决方案1】:

    我对您的 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 年 4 月 26 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-11
      • 2023-01-08
      • 2020-10-08
      • 2023-04-07
      • 2020-08-19
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      相关资源
      最近更新 更多