How to show eta squared in a table summary

Viewed 73

Tried to follow a previous post (How to add the effect size in the summary table using R package “gtsummary”?) to include the eta squared effect size in a summary table but column kept coming up blank.

Created effect size function:

my_ES_test <- function(data, variable, by, ...) {
  aovmod = aov(variable ~ by, data = data)
  lsr::etaSquared(aovmod)[1,1]
}

Then tried to add it to our (admittedly, a little complex) summary table:

factor_table2 <- data_comp%>% 
  select(Group, F1, F2, F3, F4, H1, H2, H3, E1, E2, E3, K1, K2, Global, eqMMSE)%>% 
  tbl_summary(by = Group ,missing="always",missing_text = "Missing",
                          type = all_continuous() ~ "continuous2",
                          statistic = all_continuous() ~ c("{mean} ({sd})","{median} ({p25}-{p75})"))%>%
  add_p(test = list(all_continuous() ~ "aov", all_categorical() ~ "kruskal.test"),pvalue_fun = ~style_pvalue(.x,digits =2))%>% 
  add_n() %>%
  modify_header(update = list(
                  label ~ "",
                  n ~ "*N*",
                  stat_1 ~ "**CDR = 0** <br> *({n})*",
                  stat_2 ~ "**CDR = 0.5** <br> *({n})*",
                  stat_3 ~ "**CDR > 0.5** <br> *({n})*"
                  ))%>% 
  bold_labels()%>%
  italicize_levels()%>%
  modify_spanning_header(all_stat_cols()~"**Clinical Dementia Rating**")%>%
  add_stat(fns = all_continuous() ~ my_ES_test) %>%
  modify_header(add_stat_1 ~ "**Eta squared**") %>%
  as_gt()
factor_table2

Resulting table: enter image description here

Would also love to change the Missing counts to %s if possible!

1 Answers

It looks like there was an error in your definition of the eta function. I've updated it, and added it to a simple tbl_summary().

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.0'

my_ES_test <- function(data, variable, by, ...) {
  aovmod = aov(data[[variable]] ~ data[[by]])
  lsr::etaSquared(aovmod)[1,1]
}

# test eta function
my_ES_test(trial, "age", "grade")
#> [1] 0.003380277

tbl <- 
  trial %>% 
  select(grade, age, marker) %>% 
  tbl_summary(
    by = grade, 
    missing = "no",
    type = all_continuous() ~ "continuous2",
    statistic = 
      all_continuous() ~ c("{mean} ({sd})",
                           "{median} ({p25}, {p75})", 
                           "{N_miss} ({p_miss}%)")
  ) %>%
  add_stat_label(label = all_continuous() ~ c("Mean (SD)", "Median (IQR)", "Missing")) %>%
  add_stat(fns = all_continuous() ~ my_ES_test) %>%
  modify_header(add_stat_1 ~ "**\U03B7\U00B2**")

enter image description here Created on 2022-01-06 by the reprex package (v2.0.1)

PS You've created a beautiful table!

Related