ggpmisc icon indicating copy to clipboard operation
ggpmisc copied to clipboard

`stat_multcomp()` Enhance multiple comparisions and contrasts

Open aphalo opened this issue 2 years ago • 7 comments

Add "staircase" pairwise contrasts. Test with additional model fit functions.

aphalo avatar Aug 13 '23 13:08 aphalo

For multiple comparisons, I note you have allowed the use of a number of corrections, e.g Bonferroni. It seems like it would be useful to be able to note the methodology in a caption. @JosiahCle, you might have some ideas on this based on your experience automating captions?

image

markbneal avatar Nov 12 '23 20:11 markbneal

This is a good point, but would be easier to use a text label in the plotting area, at least together with letters, and a subscript to P when the P-value is shown. Stats in principle pass data to a geom that adds a plot layer. So, even if possible, it would not follow the grammar of graphics.

aphalo avatar Nov 22 '23 21:11 aphalo

This is a good point, but would be easier to use a text label in the plotting area, at least together with letters, and a subscript to P when the P-value is shown. Stats in principle pass data to a geom that adds a plot layer. So, even if possible, it would not follow the grammar of graphics.

I just implemented labels as shown below. Do you think this would work instead of the suggested caption?

library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
#> Registered S3 method overwritten by 'ggpmisc':
#>   method                  from   
#>   as.character.polynomial polynom

p1 <- ggplot(mpg, aes(factor(cyl), hwy)) +
   geom_boxplot(width = 0.33)

p1 +
  stat_multcomp(adjusted.type = "bonferroni")


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = 10)


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = 0)


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = -1)


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm")


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "bonferroni", mc.critical.p.value = 0.01)

Created on 2023-11-23 with reprex v2.0.2

aphalo avatar Nov 22 '23 23:11 aphalo

Those options work, and are better than staying silent on whether an adjustment has been implemented to determine the p value. In the multiple pair wise comparisons, it feels like it starts to take up realestate with the full bonferroni when saying once would be enough, but I take your point that it doesn't fit neatly with grammar of graphics. Your abbreviation goes someway to making it tidy.

If I was coding for transparency, I could imagine before the plot I set a variable: Adjustment_method <- "bonferonni"

Then in the plot call, I use that variable as a parameter to stat_multcomp, and as a parameter for my caption.

markbneal avatar Nov 23 '23 07:11 markbneal

Thanks! Yes, I agree with you that the option of setting the caption manually is a good one, and should be supported. The abbreviations are generated with R function abbreviate(). I think the default should be to add the label, but allow the user to skip it. Thus a couple of tweaks to the code.

Now a negative value as argument, such as adj.method.tag = -3 uses an abbreviation of word "adjusted": abbreviate("adjusted", -adj.method.tag). Now adj.method.tag = 0 or adj.method.tag = FALSE when using letters to show significant differences removes the label so that, for example, the information can be shown in the caption instead of on the plot.

library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
#> Registered S3 method overwritten by 'ggpmisc':
#>   method                  from   
#>   as.character.polynomial polynom

p1 <- ggplot(mpg, aes(factor(cyl), hwy)) +
  geom_boxplot(width = 0.33)

p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = -3)


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = 0)


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm")


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm", adj.method.tag = -3)


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm", adj.method.tag = 0)

Created on 2023-11-23 with reprex v2.0.2

aphalo avatar Nov 23 '23 08:11 aphalo

This is only implemented for expressions. After additional testing I will add the equivalent code for LaTeX and Markdown labels.

aphalo avatar Nov 23 '23 16:11 aphalo

Add "staircase" pairwise contrasts. These and other arbitrary pairwise contrasts are now implemented by accepting a numeric matrix describing arbitrary sets of pairwise contrasts.

aphalo avatar Nov 24 '23 09:11 aphalo

Adding a 'ggplot2' caption from within a statistic is not consistent with the Grammar of Graphics. Seems also difficult to implement without using fragile coding tricks. Geoms are expected to only add plot layers.

aphalo avatar May 04 '24 10:05 aphalo