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,
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>
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)