Home > OS >  How to compute means and sd using compare_means in ggpubr
How to compute means and sd using compare_means in ggpubr

Time:12-19

compare_means is a straightforward function which I consider very useful:

library(ggpubr)
data("ToothGrowth")
df <- ToothGrowth

res <- compare_means(len ~ supp,
                     group.by = "dose",
                     data = df, 
                     method = "wilcox.test", paired = FALSE)

However, to the best of my knowledge, it is not possible to obtain means and standard deviations (or standard errors) in the relative table of results.

> res
# A tibble: 3 × 9
   dose .y.   group1 group2       p p.adj p.format p.signif method  
  <dbl> <chr> <chr>  <chr>    <dbl> <dbl> <chr>    <chr>    <chr>   
1   0.5 len   OJ     VC     0.0232  0.046 0.023    *        Wilcoxon
2   1   len   OJ     VC     0.00403 0.012 0.004    **       Wilcoxon
3   2   len   OJ     VC     1       1     1.000    ns       Wilcoxon
> 

Which is the best way to obtain group 1 and group 2 means and SD/SE with few code lines? I would like to have means (SD) instead of groups' labels OJ/VC.

Based on the documentation, there are no specific arguments helpful to this aim.

CodePudding user response:

You should use an another function desc_statby.

We have a nice example:

library(ggpubr)
data("ToothGrowth")

res <- desc_statby(ToothGrowth, measure.var = "len",
                   grps = c("dose", "supp"))
head(res[, 1:10])

  dose supp length  min  max median  mean   iqr     mad       sd
1  0.5   OJ     10  8.2 21.5  12.25 13.23 6.475 4.29954 4.459709
2  0.5   VC     10  4.2 11.5   7.15  7.98 4.950 3.55824 2.746634
3  1.0   OJ     10 14.5 27.3  23.45 22.70 5.350 3.92889 3.910953
4  1.0   VC     10 13.6 22.5  16.50 16.77 2.025 1.70499 2.515309
5  2.0   OJ     10 22.4 30.9  25.95 26.06 2.500 2.07564 2.655058
6  2.0   VC     10 18.5 33.9  25.95 26.14 5.425 4.59606 4.797731

You can take necessary info from both tables and make a result one table...

CodePudding user response:

Something like this?

library(dplyr)
library(tidyr)

df %>% 
  group_by(supp, dose) %>% 
  summarise(mean = mean(len), sd = sd(len)) %>% 
  pivot_wider(
    names_from = supp,
    values_from = c(mean, sd)
  ) %>% 
  right_join(res, by="dose") %>% 
  select(-c(group1, group2, .y.))
 dose mean_OJ mean_VC sd_OJ sd_VC       p p.adj p.format p.signif method  
  <dbl>   <dbl>   <dbl> <dbl> <dbl>   <dbl> <dbl> <chr>    <chr>    <chr>   
1   0.5    13.2    7.98  4.46  2.75 0.0232  0.046 0.023    *        Wilcoxon
2   1      22.7   16.8   3.91  2.52 0.00403 0.012 0.004    **       Wilcoxon
3   2      26.1   26.1   2.66  4.80 1       1     1.000    ns       Wilcoxon
> 
  • Related