3

Firstly - love gtsummary! It's revolutionised how I do stats for all my papers and made me dive fully into R.

Wondering if there is a way to do between-group comparisons using ANOVA with gtsummary?

Here's an example: enter image description here

We have 3 groups here and 5 features. I can generate this table using tbl_summary and add_p. That gives me the overall p value for group differences per features, but I then need to manually go in and add those signifiers (* , † and ‡) which represent significant between group difference (Group 1 vs 2; Group 1 vs 3 etc...).

I have to use SPSS for this, for which I use the following SPSS code to do ANOVA with post-hoc Bonferroni correction.

ONEWAY A B C D E BY group_factor
  /STATISTICS DESCRIPTIVES
  /MISSING ANALYSIS
  /POSTHOC=BONFERRONI ALPHA(0.05).

The output of SPSS here is a basically a table that generates for each feature the P value when comparing group 1 vs 2, group 2 vs 3 etc...

I then have to manually read for significant between-group results and then add signifiers to the original gtsummary table.

Is there a way this could be automated or incorporated into gtsummary? It will save me literally hours of manual tedious work!

JayPeerachai
  • 3,499
  • 3
  • 14
  • 29

1 Answers1

4

gtsummary tables are highly customizable, and you can add these footnotes. I've included an example below where the pairwise comparisons are visually inspected and the footnotes manually added. But you could wrap this up in a function and apply the footnotes programmatically.

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

# build table with one-way ANOVA results
tbl1 <- 
  trial %>%
  select(marker, grade) %>%
  tbl_summary(by = grade, missing = "no") %>%
  add_p()

enter image description here

# calculate pairwise comparison p-values
pairwise.t.test(trial[["marker"]], trial[["grade"]], p.adj = "none")
#> 
#>  Pairwise comparisons using t tests with pooled SD 
#> 
#> data:  trial[["marker"]] and trial[["grade"]] 
#> 
#>     I    II  
#> II  0.01 -   
#> III 0.64 0.04
#> 
#> P value adjustment method: none

# add footnotes for sig comparisons
tbl2 <-
  tbl1 %>%
  modify_table_styling(
    column = stat_1,
    rows = variable == "marker",
    footnote = "Mean significantly different from Grade II"
  ) %>%
  modify_table_styling(
    column = stat_2,
    rows = variable == "marker",
    footnote = "Mean significantly different from Grade III"
  ) %>%
  modify_table_styling(
    column = stat_3,
    rows = variable == "marker",
    footnote = "Mean significantly different from Grade II"
  )

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

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • Thank you for this! Really appreciate it! I have spent quite a lot of time trying to make it work for multiple variables. For example, there are significant difference in ‘ttdeath’ between groups and I am trying to make it work with the modift_table_styling code you shared but struggling to make it work. It adds additional footnotes to “marker” instead of in “ttdeath”. Sorry my R skills are probably not very good! Added code in another comment. – Oliver Wood Nov 05 '21 at 11:45
  • Sorry I am limited by 500 chat limit here, and I see on your website contact details you prefer for questions to be posted on here over email so they can help everyone searching for a similar solution. I have put my [full comment on here](https://pastebin.com/8ENBM7rL) if you could please read and response. Alternatively if I can contact you via email that'll be really helpful - I'll then post the 'solution' on here in a separate question? – Oliver Wood Nov 05 '21 at 11:50
  • Hello Oliver! I gave writing the automating function a shot, and also found it tricky. I unfortunately don't have the time to write a general function that solves the issues for a generic number of levels – Daniel D. Sjoberg Nov 05 '21 at 15:46
  • Hi Dan, thanks for the response and for the help. Could I please ask how I can use the manual approach and modify_table_styling() to work on multiple variables? Is that something straightforward or requiring something complex (which I completely understand I obviously I can't possible ask for help with). Thanks again for help with this. – Oliver Wood Nov 05 '21 at 16:47
  • Can you give more details? I am not sure what you're asking... – Daniel D. Sjoberg Nov 05 '21 at 17:23