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
