Search code examples
rggpubrrstatix

R: problems plotting p-values from conover test (Dunn's works fine)


I am having some trouble with an error message I can't solve.

I use the following code in to run a Kruskal Wallis in R and then do a pairwise comparison using Dunn's:

res.kruskal <- mydata %>% kruskal_test(values ~ group)
res.kruskal
stat.test <- mydata %>% dunn_test(values ~ group, p.adjust.method = "hochberg") 
stat.test
stat.test <- stat.test %>% add_xy_position(x = "group")

I then use this result to plot out the significances like so:

ggboxplot(mydata, x = "group", y = "values, fill = "group") +
  stat_pvalue_manual(stat.test, hide.ns = FALSE)

And get is nice looking plot (hooray!)

enter image description here

But the Cover Test is apparently more powerful and preferred over Dunn's... (any opinions on this would be welcome too!). Running the following code returns an error:

res.kruskal <- immdatamed %>% kruskal_test(LplastinTL ~ group)
res.kruskal
attach(immdatamed)
stat.test <- conover.test(LplastinTL, group, method = "hochberg") 
stat.test
stat.test <- stat.test %>% add_xy_position(x = "group")
detach(immdatamed)

stat.test <- stat.test %>% add_xy_position(x = "group")
Error in asserttat_group_columns_exists(test) : 
  data should contain group1 and group2 columns

I can't figure this out... I can run the Conover test fine but can't solve the above error to get it to work with ggboxplot.

I would actually prefer to use the more aesthetically pleasing ggplot & geom_boxplot but can't get that to interface with Dunn_test at all...

Any solutions would be most welcome!

Thank you

P.S. I was using this with ggplot: http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots but I cont want a wilcoxon or t.test for pairwise comparison...


Solution

  • This is very long to explain what I am doing since I expect you are inexperienced with both R and the stats. I won't even cover why Conover vs Dunn thats more a stats exchange thing see here

    Please in the future be clear about what packages you are using, you never mention rstatix for example. Please include a sample of your data.

    # https://stackoverflow.com/questions/61922336/r-problems-plotting-p-values-from-conover-test-dunns-works-fine
    library(dplyr)
    library(ggpubr)
    library(rstatix)
    
    # I'm going to get the Conover test from this package 
    
    require(DescTools)
    
    # You didn't provide data I will use mtcars
    
    mydata <- mtcars %>% select(mpg, gear)
    mydata$gear <- factor(mydata$gear)
    
    # Here's what you were doing
    
    stat.test.dunn <- mydata %>% dunn_test(mpg ~ gear, p.adjust.method = "hochberg") 
    stat.test.dunn <- stat.test.dunn %>% add_xy_position(x = "gear")
    stat.test.dunn
    #> # A tibble: 3 x 13
    #>   .y.   group1 group2    n1    n2 statistic       p   p.adj p.adj.signif
    #>   <chr> <chr>  <chr>  <int> <int>     <dbl>   <dbl>   <dbl> <chr>       
    #> 1 mpg   3      4         15    12      3.76 1.69e-4 5.06e-4 ***         
    #> 2 mpg   3      5         15     5      1.65 9.98e-2 2.00e-1 ns          
    #> 3 mpg   4      5         12     5     -1.14 2.54e-1 2.54e-1 ns          
    #> # … with 4 more variables: y.position <dbl>, groups <named list>, xmin <int>,
    #> #   xmax <int>
    
    ggboxplot(mydata, x = "gear", y = "mpg", fill = "gear") +
      stat_pvalue_manual(stat.test.dunn, hide.ns = FALSE)
    

    Here's how we can "fake it out"

    
    stat.test.Conover <- DescTools::ConoverTest(mpg ~ gear, mydata, method = "hochberg" ) 
    stat.test.Conover
    #> 
    #>  Conover's test of multiple comparisons : hochberg  
    #> 
    #>     mean.rank.diff    pval    
    #> 4-3      13.658333 8.5e-05 ***
    #> 5-3       7.966667  0.0767 .  
    #> 5-4      -5.691667  0.1434    
    #> ---
    #> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    stat.test.Conover[[1]]
    #>     mean rank diff         pval
    #> 4-3      13.658333 8.490683e-05
    #> 5-3       7.966667 7.666748e-02
    #> 5-4      -5.691667 1.433731e-01
    
    things_we_want <- rstatix::add_significance(data = as.data.frame(stat.test.Conover[[1]]))
    things_we_want
    #>   mean rank diff         pval pval.signif
    #> 1      13.658333 8.490683e-05        ****
    #> 2       7.966667 7.666748e-02          ns
    #> 3      -5.691667 1.433731e-01          ns
    
    stat.test.Conover <- stat.test.dunn
    stat.test.Conover
    #> # A tibble: 3 x 13
    #>   .y.   group1 group2    n1    n2 statistic       p   p.adj p.adj.signif
    #>   <chr> <chr>  <chr>  <int> <int>     <dbl>   <dbl>   <dbl> <chr>       
    #> 1 mpg   3      4         15    12      3.76 1.69e-4 5.06e-4 ***         
    #> 2 mpg   3      5         15     5      1.65 9.98e-2 2.00e-1 ns          
    #> 3 mpg   4      5         12     5     -1.14 2.54e-1 2.54e-1 ns          
    #> # … with 4 more variables: y.position <dbl>, groups <named list>, xmin <int>,
    #> #   xmax <int>
    stat.test.Conover$p.adj <- things_we_want$pval
    stat.test.Conover$p.adj.signif <- things_we_want$pval.signif
    stat.test.Conover
    #> # A tibble: 3 x 13
    #>   .y.   group1 group2    n1    n2 statistic       p   p.adj p.adj.signif
    #>   <chr> <chr>  <chr>  <int> <int>     <dbl>   <dbl>   <dbl> <chr>       
    #> 1 mpg   3      4         15    12      3.76 1.69e-4 8.49e-5 ****        
    #> 2 mpg   3      5         15     5      1.65 9.98e-2 7.67e-2 ns          
    #> 3 mpg   4      5         12     5     -1.14 2.54e-1 1.43e-1 ns          
    #> # … with 4 more variables: y.position <dbl>, groups <named list>, xmin <int>,
    #> #   xmax <int>
    
    ggboxplot(mydata, x = "gear", y = "mpg", fill = "gear") +
      stat_pvalue_manual(stat.test.Conover, hide.ns = FALSE)