2

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

1 Answer 1

1

I added an example including Cramer's V in the output table. Happy Programming!

library(gtsummary)

my_ES_test <- function(data, variable, by, ...) {
  rstatix::cohens_d(data, as.formula(glue::glue("{variable} ~ {by}")))$effsize
}
my_cramer_v <- function(data, variable, by, ...) {
  table(data[[variable]], data[[by]]) %>%
    rstatix::cramer_v()
}

gtTable <- 
  mtcars %>% 
  select(hp, vs, am) %>% 
  tbl_summary(by = vs) %>% 
  add_p() %>% 
  add_stat(
    fns = list(all_continuous() ~ my_ES_test,
               all_categorical() ~ my_cramer_v)) %>% 
  modify_header(add_stat_1 ~ "**Effect size**")

enter image description here Created on 2021-09-07 by the reprex package (v2.0.1)

FYI, there is built-in support for Cohen's D so you don't need to create a custom method. You can see a complete list here https://www.danieldsjoberg.com/gtsummary/reference/tests.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.