Question
I am working with a gtsummary table that includes statistical tests for both means and frequencies. I want to include effect sizes to the table and came across this post which shows how to do it for Wilcox test effect size. The code also works for Cohen's d, but I am struggling to modify it for Cramer's V. It seems the cramer_v() function needs to call a ready-made contingency table, rather than working with raw data. Is there any way to calculate and include Cramer's V for the chi-square in the gtsummary table?
Example
library(tidyverse)
library(rstatix)
library(gtsummary)
theme_gtsummary_mean_sd()
my_ES_test <- function(data, variable, by, ...) {
rstatix::cohens_d(data, as.formula(glue::glue("{variable} ~ {by}")))$effsize
}
gtTable <- mtcars %>%
select(hp, vs, am) %>%
tbl_summary(by = vs) %>%
add_p() %>%
add_stat(fns = all_continuous() ~ my_ES_test) %>%
modify_header(add_stat_1 ~ "Effect size")
print(gtTable)
contTable <- table(mtcars$vs, mtcars$am)
chisq.test(contTable, correct = FALSE)
#>
#> Pearson's Chi-squared test
#>
#> data: contTable
#> X-squared = 0.90688, df = 1, p-value = 0.3409
cramer_v(contTable)
#> [1] 0.1042136
