0

I want to convert a dunn.test output with the melt function from the reshape2 package, but I lose the pairwise group names. How to keep them in the "melted" data frame?

Using the melt for the pairwise.wilcox.test output, with the code:

pw <- pairwise.wilcox.test(tabel2$RS.14_2, tabel2$lot, p.adj = "bonf")
melt(pw[[3]])

outputs the next result:

   Var1 Var2        value
1  3StL 1StL          NaN
2    IP 1StL 6,930275e-36
3   ISR 1StL 5,929457e-21
4   ISU 1StL 3,427444e-33
5   StM 1StL 1,506569e-06
6  3StL 3StL           NA
7    IP 3StL 2,267872e-32
8   ISR 3StL 2,113804e-18
9   ISU 3StL 8,588033e-30
10  StM 3StL 1,297147e-05
11 3StL   IP           NA
...

I want the same for the dunn.test output, but after:

out <- dunn.test(tabel2$RS.14_2,tabel2$lot)
z <- melt(out$Z)
p <- melt(out$P)
p.adj <- melt(out$P.adjusted)
out <- cbind(round(z, digits = 3), round(p, digits = 3), round(p.adj, digits = 3))
colnames(out) <- c("z", "p", "p.ajust")
out

the standard output looks as here:

                           Comparison of x by group                            
                                (No adjustment)                                
Row Mean-|
Col Mean |       1StL       3StL         IP        ISR        ISU
---------+-------------------------------------------------------
    3StL |   0.000000
         |     0.5000
         |
      IP |  -12.68147  -12.15550
         |     0.0000     0.0000
         |
     ISR |  -8.891610  -8.522826   3.710811
         |     0.0000     0.0000     0.0001
         |
     ISU |  -12.09842  -11.59663   0.570894  -3.139917
         |     0.0000     0.0000     0.2840     0.0008
         |
     StM |  -3.227439  -3.107366   8.579912   5.069266   8.039813
         |     0.0006     0.0009     0.0000     0.0000     0.0000

and the "melted" output like here, so without expected row (pairwise) names:

         z     p p.ajust
1    0,000 0,500   0,500
2  -12,681 0,000   0,000
3  -12,156 0,000   0,000
4   -8,892 0,000   0,000
5   -8,523 0,000   0,000
6    3,711 0,000   0,000
7  -12,098 0,000   0,000
8  -11,597 0,000   0,000
9    0,571 0,284   0,284
10  -3,140 0,001   0,001
11  -3,227 0,001   0,001
12  -3,107 0,001   0,001
13   8,580 0,000   0,000
14   5,069 0,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, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1)), .Names = c("lot", "RS.14_2"), class = "data.frame", row.names = c(NA, -582L))
Yurié
  • 2,148
  • 3
  • 23
  • 40

1 Answers1

1

Information you are looking for is simply not returned from a dunn.test. You can try to do something like this:

library(dunn.test)
#' Return result of dunn.test with groups attached
#' 
#' @param x - a numeric vector, or a list of numeric vectors.
#' @param g - a factor variable, numeric vector, or character vector
#' @param ...
#' @return data.frame
dunn.test.with.names <- function(x, g = NA, ...) {
    if(length(g) == 1 && any(is.na(g))) {
        as.data.frame(dunn.test(x = x, ...))
    } else {
        dt <- dunn.test(x = x, g = g, ...)
        rnames <- levels(factor(g))

        # Generate all combinations of g
        grid <- expand.grid(
            rnames[-length(rnames)], rnames[-1],
            stringsAsFactors = FALSE
        )

        # Filter and assign dimnames
        grid <- grid[grid[, 1] < grid[, 2], ][, c(2, 1)]
        dimnames(grid) <- list(
            as.character(1:nrow(grid)),
            c("Var1", "Var2")
        )

        # Combine results
        cbind(
            grid,
            round(as.data.frame(dt), digits = 3)
        )
    }
}

It is a rather ugly and only a little bit tested solution.

Note

According to the dunn.test author (see comments) this information is no longer up to to date with version 1.2.3 (published: 2015-02-25).

zero323
  • 322,348
  • 103
  • 959
  • 935
  • Maybe it is ugly, but it works! Thank you very much! – Yurié Dec 19 '14 at 13:06
  • I found a little discordance: `melt` reads values from the original data frame by rows, but your code builds variable pairs vertically. Please, look here: http://goo.gl/7dXBWI. – Yurié Dec 19 '14 at 13:58
  • Yeah, like I said ugly and untested. I've took another look and `dunn.test` output and as far as I can tell you don't need `melt` at all. – zero323 Dec 19 '14 at 17:41
  • Perfect!!! Nice work! Thank you! As a bonus :), can you suggest how to extract the Kruskal-Wallis values from the `dunn.test` output as a table row? – Yurié Dec 19 '14 at 17:53
  • No way ;) Seriously though, I've spend some time trying to figure out what is going inside but a whole library is a pretty much one big function which handles everything including formatting and printing. There is a nested function kwallis.test which covers lines 12:106 in R/dunn.test.R and seems to do most of the work, but further downstream you can find all kind of corrections and I won't even pretend I understand what is going on – zero323 Dec 19 '14 at 18:25
  • Never mind! You already helped me a lot! About your function: the rows of the output are numbered as 1, 6, 7, 11, 12, 13, 16 and so. Can this be corrected? – Yurié Dec 19 '14 at 20:07
  • @alexis I want to ask the package author: how to extract the Kruskal-Wallis values from the `dunn.test` output? – Yurié Dec 19 '14 at 21:34
  • 1
    Regarding rownames it is quite easy. You could have done it yourself buy I've pushed a fix. – zero323 Dec 20 '14 at 02:31
  • 1
    @lurie dunn.test has been updated in v 1.2.2 to include the Kruskal-Wallis statistic among the returned values. – Alexis Jan 02 '15 at 19:25
  • 1
    @Alexis, thank you for your responsiveness! I need to extract the KW *chi2* value and the update that you made is welcome. But how about the *chi2* related *p-value*? I need to extract it too. Also, I think that for users that uses your function in their own functions the uncontrolled output on their screens is an inconvenience, as in my case. I mean the *Comparison of x by g* matrix, outputted by the `dunn.test` function even if I want that output only in a variable, not to output it on the screen. Thank you! Regards, – Yurié Feb 21 '15 at 14:54
  • @lurie stackoverflow is an inappropriate forum for this topic. Kindly use the contact information provided in the documentation for dunn.test(). – Alexis Mar 04 '15 at 19:10
  • down-voted because the information is now out of date. – Alexis Mar 04 '15 at 19:11