我可以将TukeyHSD的结果安排到表格中吗?

时间:2017-07-14 08:32:22

标签: r

Tukey在R中的事后测试返回结果

            diff         lwr        upr     p adj
2-1  2.125000e-01 -0.13653578  0.5615358 0.4873403
3-1  2.250000e-01 -0.12403578  0.5740358 0.4219408
4-1  3.875000e-01  0.03846422  0.7365358 0.0206341
5-1  6.875000e-01  0.33846422  1.0365358 0.0000020
6-1  2.250000e-01 -0.12403578  0.5740358 0.4219408
3-2  1.250000e-02 -0.31064434  0.3356443 0.9999974
4-2  1.750000e-01 -0.14814434  0.4981443 0.6147144
5-2  4.750000e-01  0.15185566  0.7981443 0.0006595
6-2  1.250000e-02 -0.31064434  0.3356443 0.9999974
4-3  1.625000e-01 -0.16064434  0.4856443 0.6866539
5-3  4.625000e-01  0.13935566  0.7856443 0.0009888
6-3  1.776357e-15 -0.32314434  0.3231443 1.0000000
5-4  3.000000e-01 -0.02314434  0.6231443 0.0844160
6-4 -1.625000e-01 -0.48564434  0.1606443 0.6866539
6-5 -4.625000e-01 -0.78564434 -0.1393557 0.0009888

这很好,但很难阅读。如果结果可以安排在一个较低的对角线表中,组因子为行和列,那会更好。

这样的东西
  1 2 3 4 5 6
1 
2 p
3 p p
4 p p p
5 p p p p 
6 p p p p p

其中p是适当的p值。这可能吗?

1 个答案:

答案 0 :(得分:1)

以下是我使用tidyverse进行手动转换的建议。 我把它作为一个函数包装起来,你可以通过传递p_adj以外的东西来改变指标。请注意,我假设输入(tbl)是一个数据框。

transformTable <- function(tbl, metric) {
  # Takes table of TurkeyHSD output metrics
  # and transforms them into a pairwise comparison matrix.
  # tbl is assumed to be a data.frame or tibble,
  # var is assumed to be a character string
  # giving the variable name of the metric in question
  # (here: "diff", "lwr", "upr", or "p_adj")
  tbl <- tbl %>%
    # Split comparison into individual variables
    mutate(
      Var1 = as.numeric(substr(X, 1, 1)),
      Var2 = as.numeric(substr(X, 3, 3))) %>%
    # Only keep relevant fields
    select(Var1, Var2, matches(metric)) %>%
    # Filter out NA's
    filter(!is.na(metric)) %>%
    # Make into "wide" format using Var2
    spread_(key = 'Var2', value = metric, fill = '')

  # Let's change the row names to Var1
  row.names(tbl) <- tbl$Var1
  # And drop the Var1 column
  tbl <- select(tbl, -Var1)

  return(tbl)
}


transformTable(df, 'p_adj')

输出:

          1         2         3         4         5
2 0.4873403                                        
3 0.4219408 0.9999974                              
4 0.0206341 0.6147144 0.6866539                    
5     2e-06 0.0006595 0.0009888  0.084416          
6 0.4219408 0.9999974         1 0.6866539 0.0009888

可重复的数据集:

df <- structure(list(X = structure(c(1L, 2L, 4L, 7L, 11L, 3L, 5L, 8L, 
12L, 6L, 9L, 13L, 10L, 14L, 15L), .Label = c("2-1", "3-1", "3-2", 
"4-1", "4-2", "4-3", "5-1", "5-2", "5-3", "5-4", "6-1", "6-2", 
"6-3", "6-4", "6-5"), class = "factor"), diff = c(0.213, 0.225, 
0.388, 0.688, 0.225, 0.0125, 0.175, 0.475, 0.0125, 0.163, 0.463, 
1.78e-15, 0.3, -0.163, -0.463), lwr = c(-0.13653578, -0.12403578, 
0.03846422, 0.33846422, -0.12403578, -0.31064434, -0.14814434, 
0.15185566, -0.31064434, -0.16064434, 0.13935566, -0.32314434, 
-0.02314434, -0.48564434, -0.78564434), upr = c(0.5615358, 0.5740358, 
0.7365358, 1.0365358, 0.5740358, 0.3356443, 0.4981443, 0.7981443, 
0.3356443, 0.4856443, 0.7856443, 0.3231443, 0.6231443, 0.1606443, 
-0.1393557), p_adj = c(0.4873403, 0.4219408, 0.0206341, 2e-06, 
0.4219408, 0.9999974, 0.6147144, 0.0006595, 0.9999974, 0.6866539, 
0.0009888, 1, 0.084416, 0.6866539, 0.0009888)), .Names = c("X", 
"diff", "lwr", "upr", "p_adj"), class = "data.frame", row.names = c(NA, 
-15L))