Reputation: 23
I'm trying to add the effective size of the wilcox test to a summary table using the add_stat function of the "gtsummary" package. My data looks like:
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
I've tried with this function:
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
I think I didn't use the right syntax for the my_ES_test function. Is there any way to do this?
Thanks for help! Dear aylaxla
Upvotes: 2
Views: 1046
Reputation: 11595
I made a slight modification to your ES function. See below!
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**")
Created on 2021-04-26 by the reprex package (v2.0.0)
Upvotes: 2