Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform a list of t-tests results into a data frame [duplicate]

I have some results from a perm.t.test that I would like to transform into a data frame.

This is a reduced version of my dataset:

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 run multiple MKinfer::perm.t.test between combinations of times (t) independently for each treatment (treat), using this function (Thanks to Ronak Shah):

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

But now I have two problems:

1- My result is a list of class ‘c("perm.htest", "these") objects, but I need to transform it into a data frame where each row is a test and, each column is an output of the test (i.e. statistic, parameter, p.value). So I can easily check my results, correct the p-values for multiple comparisons and export them.

2- I don't know which test correspond to which combination of levels of factor t. In the output of the function there is no mention to that. But I guess I could retrieve these using the function combn(levels(data$t), 2) and then create a vector (combt) with all the combinations of level of t:

combt = combn(levels(data$t), 2)
combt = t(combt)
combt = data.frame(combt)
combt = paste(combt$X1, combt$X2, sep=" vs ")

I've tried to search but I could find the solution. Is there any who can help me?

Thank you in advance.

like image 790
Valen78 Avatar asked Dec 21 '25 20:12

Valen78


1 Answers

You can apply broom::tidy on list of result -

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
like image 69
Ronak Shah Avatar answered Dec 23 '25 11:12

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!