'Running post-hoc test over a list of ANOVA results

I'm trying to run a post hoc test (dunnTest) on several kruskal.test results that are stored in a list.

Example

testDF<- iris

test3<- map(names(testDF)[1:4], ~ kruskal.test(reformulate('Species', response=.x), data=testDF))

This works great.

The end goal is to have an efficient way to generate a list of the pairwise comparisons for each variable across each species using the dunnTest with the 'bonferroni' method IF the p-value of the kruskal.test was significant. I have tried various combinations of lapply, sapply, do.call, and map. The most common error I have been getting is that "x must be numeric", but I am unsure of how to specify the correct part of each test result within the list.

I have also just tried putting the kruskal.test inside the dunnTest- both within the map function, but have had no luck.

 map(names(testDF)[1:3], ~ dunnTest(kruskal.test(reformulate('Species', response=.x), data=testDF)))

This link was almost helpful, but with my function it gave an over-complicated nested output with the same information nested within each item in the list.

test_model<- lapply(testDF, function(x){
  map(names(testDF)[1:4], ~ kruskal.test(reformulate('Species', response=.x), data=testDF))
})
View(test_model)

My next guess is to use some combination of ifelse and map to apply a dunnTest over each list item if p-value of kruskal.test is <0.05:

test5<- ifelse(test_model[[i]]$p.value<= 0.05, function(x){
  map(names(testDF)[1:4], ~ dunnTest(reformulate('Species', response=.x), data=testDF))
})

But I don't think test_model[[i]]$p.value is a valid call.

Any help would be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source