Search code examples
rsizeeffectgtsummary

How to generate effect size [90%CI] in the summary table using R package “gtsummary”? New ES package calculation and qualitative indice in the table


Once again I would like to thank Daniel Sjoberg and other collaborators for the constant implementation of functionality in the gtsummary package. For me, one of the most efficient suites in R for processing and reporting results in tables / in line.

A while ago I asked for help on including effect size [90%CI] in the analytical tables generated by the gtsummary package. However, in this new post I intend to change the ES calculation package for giving me a vast repertoire of indexes and also for having their qualitative magnitude. I tried to implement this other package in new code. However, this message is returned:

There was an error for variable 'age': Error in .deal_with_cohens_d_arguments (x, y, data): Please provide data argument.

I believe I am not able to configure the function (CohenD object). Could someone please help me with my code?

I copied it below:

CohenD <- function(data, variable, by, ...) {
  # Cohen's d, Hedges's g (correction=TRUE or FALSE) and Glass’s delta
  ES <- effectsize::cohens_d(data[[variable]] ~ as.factor(data[[by]]),
                             ci=.90,
                             pooled_sd=TRUE,
                             paired=FALSE,
                             correction=TRUE)
 
  # Formatting statistic with CI
  est <- style_sigfig(abs(ES$Cohens_d))
  ci_lower <- style_sigfig(ES$CI_low)
  ci_upper <- style_sigfig(ES$CI_high)
 
  # Returning estimate with CI together
  str_glue("{est} ({ci_lower, ci_upper})")
}

Table <-
  trial %>%
  select(trt, age) %>%
  tbl_summary(by = trt, missing = "no", label = list (age ~ "Age (yrs)"),
              statistic = list(all_continuous() ~ "{mean} ± {sd}"),
              digits = list(all_continuous() ~ c(1,1))) %>%
  bold_labels() %>%
  italicize_levels() %>%
  add_p(test = everything() ~ t.test, pvalue_fun = partial(style_pvalue, digits = 2)) %>%
  add_stat(
    fns = everything() ~ CohenD,
    fmt_fun = NULL,
    header = "**ES (90% CI)**"
  ) %>%
  modify_footnote(add_stat_1 ~ "Hedges's g (90% CI)") %>%
  modify_header(label = "**Variables**", stat_by = "**{level}** (N= {n})")
Table

Would it be possible to include a new column in Table or to join with the ES +/- CI, already provided by this function, the qualitative magnitude of the observed ES (interpret a value based on a set of rules)? The suggestion comes for this feature:

effectsize::interpret_d(ES$Cohens_d, rules = "cohen1988")

Cheers, Cristiano


Solution

  • The issue you're experiencing is in your user-defined function CohenD(): it did not like the way you were passing the formula. In the example below, I corrected the syntax. I also included the interpretation of the effect size.

    library(gtsummary)
    library(tidyverse)
    
    # function that returns either Cohen's D or the 1988 interpretation of its size
    CohenD <- function(data, variable, by, ...) {
      # Cohen's d, Hedges's g (correction=TRUE or FALSE) and Glass’s delta
      ES <- effectsize::cohens_d(data[[variable]], factor(data[[by]]),
                                 ci=.90,
                                 pooled_sd=TRUE,
                                 paired=FALSE,
                                 correction=TRUE)
      
      # Formatting statistic with CI
      est <- style_sigfig(ES$Cohens_d)
      ci_lower <- style_sigfig(ES$CI_low)
      ci_upper <- style_sigfig(ES$CI_high)
      
      # Returning estimate with CI together
      tibble(
        cohen_d = stringr::str_glue("{est} ({ci_lower}, {ci_upper})"),
        interpret_d = stringr::str_glue("{effectsize::interpret_d(ES$Cohens_d, rules = 'cohen1988')}")
      )
      
    }
    
    tbl <-
      trial %>%
      select(trt, age, marker) %>%
      tbl_summary(by = trt, missing = "no", statistic = all_continuous() ~ "{mean} ± {sd}") %>%
      add_p(test = everything() ~ t.test) %>%
      add_stat(fns = everything() ~ CohenD) %>%
      modify_header(cohen_d = "**ES (90% CI)**", interpret_d = "**Interpretation**")
    

    enter image description here Created on 2021-04-15 by the reprex package (v2.0.0)