tags:

views:

70

answers:

1

I used to have 2 factor by 2 level experiment that got made into a 3 factor by 2 level experiment.

By using paste I could make 4 unique groups from my two factors and run a fisher test with the outcome being whether an organism lived or died.

fisher.test(mortal$alv.dead,paste(mortal$Strain,mortal$capsule))

But then when I wanted to investigate pairwise comparisions between the individual groups I had to make some inelegant filtering so that only two groups entered the analysis at a time. Now that I have more groups it is too tedious to hand code each paring. So here is the fisher test to test all the groups in one analysis

fisher.test(mortal$alv.dead,paste(mortal$Strain,mortal$capsule,mortal$cassette))

How do I set up a method that creates and tests all possible pairings?

+2  A: 

Fairly easy using the function combn(). The only thing you should take into account, is the fact that combn will not return the names of the groups correctly when you put the fisher.test() call inside the function.

Thus we need to adjust the element in the list accordingly :

Some toy data:

mortal <- data.frame(
      alv.dead = sample(c("alv","dead"),30,replace=T),
      train = sample(letters[1:3],30,replace=T),
      capsule = sample(letters[4:5],30,replace=T),
      cassette = sample(letters[6:7],30,replace=T)
      )

Some extra variables

mortal$groups <- paste(mortal$train,mortal$capsule,mortal$cassette,sep="")
unique.groups <- unique(mortal$groups)

And the trick :

combn(unique.groups,2,function(x){
    id <- mortal$groups %in% x
    test <- fisher.test(table(mortal$alv.dead[id],mortal$groups[id]))
    test$data.name <-
      paste(
        unique(
          as.character(mortal$groups[id])
        ),collapse="-")
    return(test)}
  ,simplify=FALSE)
Joris Meys
Dear Joris, you are a genius! Wow that worked well. I did not know about the combn function. I can think of so many applications. The simplify argument is interesting. If left to False it good to read and leisurely think about the data but when wants to quickly compare them the simplify=TRUE is amazing because it puts all the data in an array.
Farrel