简体   繁体   中英

Presenting Tukey HSD pairwise p-values in a table

I'm running a posthoc Tukey HSD on my data, which has ten factor levels. The table is massive and I was hoping to just present the p-values to the reader, in a pairwise table, leaving the 45 row-ed table for the appendix.

Here is an example dataset:

set.seed(42)
x <- rnorm(100,1,2)
category <- letters[1:10]
data <- cbind.data.frame(x, category)
summary(data.aov <- aov(x~category, data = data))
data.hsd<-TukeyHSD(data.aov)
data.hsd.result<-data.frame(data.hsd$category)
data.hsd.result

The result is a table of 45 rows. Instead, I'd like a table with the factor levels as row and column names, with the p-value in the cell, showing if the two are significantly different. Xs or underscores or whatever could represent repeated or unnecessary comparisons. Something like this:

         a    b       c       d      e     f      ...   j
    a    X    0.97    1     0.99   0.89   0.99    ...   0.99
    b    X     X    0.99    0.89   0.94   0.92    ...   0.97
    c    X     X      X     0.85   0.93   0.96    ...   0.98
    |   ...   ...    ...     ...   ...    ...     ...   ...
    i    X     X      X       X     X      X      ...   0.84

and so on.

Is there a way to produce a table like this automatically?

You want the p-values in the upper-triangular matrix form. That's a bit unnatural for R since it fills its matrices by column, but it easy enough to fix. First check that you are getting the correct order:

> resm <- matrix(NA, 10, 10)
> resm[lower.tri(resm) ] <- rownames(data.hsd.result)
> resm
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10]
 [1,] NA    NA    NA    NA    NA    NA    NA    NA    NA    NA   
 [2,] "b-a" NA    NA    NA    NA    NA    NA    NA    NA    NA   
 [3,] "c-a" "c-b" NA    NA    NA    NA    NA    NA    NA    NA   
 [4,] "d-a" "d-b" "d-c" NA    NA    NA    NA    NA    NA    NA   
 [5,] "e-a" "e-b" "e-c" "e-d" NA    NA    NA    NA    NA    NA   
 [6,] "f-a" "f-b" "f-c" "f-d" "f-e" NA    NA    NA    NA    NA   
 [7,] "g-a" "g-b" "g-c" "g-d" "g-e" "g-f" NA    NA    NA    NA   
 [8,] "h-a" "h-b" "h-c" "h-d" "h-e" "h-f" "h-g" NA    NA    NA   
 [9,] "i-a" "i-b" "i-c" "i-d" "i-e" "i-f" "i-g" "i-h" NA    NA   
[10,] "j-a" "j-b" "j-c" "j-d" "j-e" "j-f" "j-g" "j-h" "j-i" NA   
> t(resm)
      [,1] [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10]
 [1,] NA   "b-a" "c-a" "d-a" "e-a" "f-a" "g-a" "h-a" "i-a" "j-a"
 [2,] NA   NA    "c-b" "d-b" "e-b" "f-b" "g-b" "h-b" "i-b" "j-b"
 [3,] NA   NA    NA    "d-c" "e-c" "f-c" "g-c" "h-c" "i-c" "j-c"
 [4,] NA   NA    NA    NA    "e-d" "f-d" "g-d" "h-d" "i-d" "j-d"
 [5,] NA   NA    NA    NA    NA    "f-e" "g-e" "h-e" "i-e" "j-e"
 [6,] NA   NA    NA    NA    NA    NA    "g-f" "h-f" "i-f" "j-f"
 [7,] NA   NA    NA    NA    NA    NA    NA    "h-g" "i-g" "j-g"
 [8,] NA   NA    NA    NA    NA    NA    NA    NA    "i-h" "j-h"
 [9,] NA   NA    NA    NA    NA    NA    NA    NA    NA    "j-i"
[10,] NA   NA    NA    NA    NA    NA    NA    NA    NA    NA   

So it's just:

resm <- matrix(NA, 10, 10)
resm[lower.tri(resm) ] <-round(data.hsd.result$p.adj, 3)
t(resm)
      [,1]  [,2] [,3] [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
 [1,]   NA 0.974 1.00    1 0.885 0.997 0.985 0.673 0.559 1.000
 [2,]   NA    NA 0.99    1 1.000 1.000 1.000 0.999 0.997 0.999
 [3,]   NA    NA   NA    1 0.938 0.999 0.995 0.772 0.666 1.000
 [4,]   NA    NA   NA   NA 0.990 1.000 1.000 0.921 0.856 1.000
 [5,]   NA    NA   NA   NA    NA 1.000 1.000 1.000 1.000 0.988
 [6,]   NA    NA   NA   NA    NA    NA 1.000 0.991 0.974 1.000
 [7,]   NA    NA   NA   NA    NA    NA    NA 0.998 0.993 1.000
 [8,]   NA    NA   NA   NA    NA    NA    NA    NA 1.000 0.914
 [9,]   NA    NA   NA   NA    NA    NA    NA    NA    NA 0.846
[10,]   NA    NA   NA   NA    NA    NA    NA    NA    NA    NA

Adding row and column names to a matrix is rather trivial with the functions: rownames<- and colnames<- . See their shared help page for worked examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM