1
votes

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 comparisons 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?

1
@mbq : it's really a programming question, so it can be answered here as well. I'm not going to talk about the correctness of the approach, which isn't not really the point of the question.Joris Meys
@Joris I agree; this is rather an ad.mbq

1 Answers

3
votes

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)