How to melt pairwise.wilcox.test output using dplyr?

后端 未结 2 1220
野性不改
野性不改 2020-12-12 06:52

I want to apply pairwise.wilcox.test for multiple independent variables at a time and then want to have the output in long format. For a particular Wavelength,

相关标签:
2条回答
  • 2020-12-12 07:21

    I could able to solve the problem using rstatix package which "provides a simple and intuitive pipe friendly framework, coherent with the 'tidyverse' design philosophy for performing basic statistical tests".

    library(tidyverse)
    library(rstatix)
    
    as_tibble(df)%>% 
      pivot_longer(cols = -Class, names_to = "Wavelengths", values_to = "value") %>% 
      mutate(Class = as.factor(Class)) %>% 
      group_by(Wavelengths) %>% 
      pairwise_wilcox_test(value~Class, p.adjust.method="bonf")
    

    which returns the following output

    #> # A tibble: 40 x 10
    #>   Wavelengths .y.   group1 group2    n1    n2 statistic     p p.adj
    #> * <chr>       <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl> <dbl>
    #> 1 WV_350      value 1      2          5     5      20   0.151 1    
    #> 2 WV_350      value 1      3          5     5      25   0.008 0.079
    #> 3 WV_350      value 1      4          5     5      25   0.008 0.079
    #> 4 WV_350      value 1      5          5     5      25   0.008 0.079
    #> 5 WV_350      value 2      3          5     5      25   0.008 0.079
    #> 6 WV_350      value 2      4          5     5      25   0.008 0.079
    #> 7 WV_350      value 2      5          5     5      25   0.008 0.079
    #> 8 WV_350      value 3      4          5     5      10   0.69  1    
    #> 9 WV_350      value 3      5          5     5      21.5 0.075 0.749
    #> 10 WV_350      value 4      5          5     5      22   0.056 0.556
    #> # ... with 30 more rows, and 1 more variable: p.adj.signif <chr>
    
    0 讨论(0)
  • 2020-12-12 07:29

    A bit verbose and Im sure it could be made more efficient:

    library(tidyverse)
    library(broom)
    res <- 
      tbl_df(df)%>% 
      pivot_longer(cols = -Class, names_to = "Wavelengths", values_to = "value") %>% 
      group_by(Wavelengths) %>% 
      summarise(pw_wt = list(pairwise.wilcox.test(value,as.factor(Class),
                                                  p.adjust.method = "bonf")$p.value)) %>% 
      ungroup() %>% 
      mutate(pw_wt_t = map(pw_wt, broom::tidy)) %>% 
      unnest(pw_wt_t)
    
    0 讨论(0)
提交回复
热议问题