Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pairwise.wilcox.test - reformat output

Tags:

r

This is the normal output from the test:

attach(airquality)
pw <- pairwise.wilcox.test(Ozone, Month, p.adj = "bonf")
pw

data:  Ozone and Month 

     May    Jun    Jul    Aug   
Jun 1.0000 -      -      -     
Jul 0.0003 0.1414 -      -     
Aug 0.0012 0.2591 1.0000 -     
Sep 1.0000 1.0000 0.0074 0.0325

I recently had to conduct a test with 10 levels of a factor. While the lower triangular format of the pairwise.wilcox.test is useful and concise, I thought it would be convenient to arrange it in a simlar way to the Tukey HSD output where each pairwise combination is listed along with it's asociated p value. This was my attempt to do this:

pw.df <- as.data.frame(pw$p.value)
pw.diff <- vector("character")
pw.pval <- vector("numeric")
for (i in 1:ncol(pw.df) )
  for (j in i:length(pw.df) ) {
    pw.diff <- c(pw.diff,paste(colnames(pw.df[i]),"-",rownames(pw.df)[j]))
    pw.pval <- c(pw.pval,pw.df[j,i])
  }


# order them by ascending p value
v <- order(pw.pval,decreasing = F)
pw.df <- data.frame(pw.diff[v],pw.pval[v])


# display those that are significant at the 5% level
pw.df[pw.df$pw.pval<0.05,]

  pw.diff.v.  pw.pval.v.
1  May - Jul 0.000299639
2  May - Aug 0.001208078
3  Jul - Sep 0.007442604
4  Aug - Sep 0.032479550

If anyone has some tips/tricks/advice on how to make this easier and/or more elegant I would be grateful.

like image 966
Robert Long Avatar asked Jan 19 '23 10:01

Robert Long


1 Answers

I would use the reshape or reshape2 package for this task, specifically the melt() command. The object returned by pairwise.wilcox.test contains the data of interest in the third slot, so something like melt(pw[[3]]) should do the trick:

    X1  X2       value
1  Jun May 1.000000000
2  Jul May 0.000299639
3  Aug May 0.001208078
4  Sep May 1.000000000
5  Jun Jun          NA
....
like image 73
Chase Avatar answered Feb 11 '23 15:02

Chase