将 t 检验结果列表转换为数据框

Transform a list of t-tests results into a data frame

我有一些来自 perm.t.test 的结果,我想将其转换为数据框。

这是我的数据集的简化版本:

treat = c("C","C","C","C","C","C","C","C","C","C","C","C","C",
         "C","C","C","C","C","C","C","T","T","T","T","T","T",
         "T","T","T","T","T","T","T","T","T","T","T","T","T","T")
subj = c("B16","B17","B18","B19","B20","B16","B17","B18","B19",
        "B20","B16","B17","B18","B19","B20","B16","B17","B18",
        "B19","B20","B1","B2","B3","B4","B5","B1","B2","B3","B4"
        ,"B5","B1","B2","B3","B4","B5","B1","B2","B3","B4","B5")
t = c("T0","T0","T0","T0","T0","T1","T1","T1","T1","T1","T2",
      "T2","T2","T2","T2","T3","T3","T3","T3","T3","T0","T0",
      "T0","T0","T0","T1","T1","T1","T1","T1","T2","T2","T2",
      "T2","T2","T3","T3","T3","T3","T3")
exparat = c(0.11,0.27,0.04,0.47,-0.11,-0.05,-0.05,0.33,-0.11,
            0.47,-0.01,0.43,0.47,0.33,-0.11,-0.09,0.20,-0.11,
                0.47,0.33,0.19,0.02,0.33,0.47,-0.11,0.42,0.13,0.47,
                -0.11,0.33,0.42,0.19,-0.11,0.33,0.47,0.42,0.17,
                0.33,0.47,-0.11)

data = data.frame(treat, subj, t, exparat)

data$treat <- factor(data$treat)
data$t <- factor(data$t,levels=unique(data$t))

head(data)

  treat subj  t exparat
1     C  B16 T0    0.11
2     C  B17 T0    0.27
3     C  B18 T0    0.04
4     C  B19 T0    0.47
5     C  B20 T0   -0.11
6     C  B16 T1   -0.05

I 运行 多个 MKinfer::perm.t.test 时间组合之间 (t) 独立于每个治疗 (treat),使用此函数 ():

library(MKinfer)

    combn(levels(data$t), 2, function(x) {
      perm.t.test(exparat~t,data = subset(data, t %in% x), nperm=999, paired = T)
    }, simplify = FALSE) -> result

但是现在我有两个问题:

1- 我的结果是 class 'c("perm.htest", "these") 对象的列表,但我需要将它转换成一个数据框,其中每一行都是一个测试,每一列都是测试的输出(即统计、参数、p.value)。所以我可以轻松地检查我的结果,更正多重比较的 p 值并导出它们。

2- 我不知道哪个检验对应于哪个因子 t 水平组合。在函数的输出中没有提到这一点。但我想我可以使用函数 combn(levels(data$t), 2) 检索这些,然后使用 t:

的所有级别组合创建一个向量(combt)
combt = combn(levels(data$t), 2)
combt = t(combt)
combt = data.frame(combt)
combt = paste(combt$X1, combt$X2, sep=" vs ")

我已经尝试搜索,但我可以找到解决方案。有谁可以帮助我吗?

提前致谢。

您可以在 result -

的列表中应用 broom::tidy
library(dplyr)
library(purrr)

map_df(result, broom::tidy) %>%
  mutate(combination = combn(levels(data$t), 2, paste0, collapse = ' vs '), .before = 1)

# combination estimate statistic p.value parameter conf.low conf.high method                    alternative
#  <chr>          <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl> <chr>                     <chr>      
#1 T0 vs T1      -0.015    -0.116   0.910         9   -0.307    0.277  Permutation Paired t-test two.sided  
#2 T0 vs T2      -0.073    -0.764   0.465         9   -0.289    0.143  Permutation Paired t-test two.sided  
#3 T0 vs T3      -0.04     -0.670   0.519         9   -0.175    0.0950 Permutation Paired t-test two.sided  
#4 T1 vs T2      -0.058    -0.482   0.641         9   -0.330    0.214  Permutation Paired t-test two.sided  
#5 T1 vs T3      -0.025    -0.220   0.831         9   -0.282    0.232  Permutation Paired t-test two.sided  
#6 T2 vs T3       0.033     0.292   0.777         9   -0.222    0.288  Permutation Paired t-test two.sided