如何使用R包“gtsummary”在汇总表中添加效果大小?

问题描述

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

Type <- c ("FND","FND","HC","HC")
Component1 <- c(2,3,2,1,1)
Component2 <- c(1,0)
Component3 <- c(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,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 函数使用正确的语法。有没有办法做到这一点?

感谢帮助! 亲爱的艾拉拉

解决方法

我对你的 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**")

enter image description here reprex package (v2.0.0) 于 2021 年 4 月 26 日创建

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...