Valen78
Valen78

Reputation: 125

Transform a class "perm.htest" objects, from (multiple permutation t test) into a data frame

I have some results from a perm.t.test that I would like to transform into a data frame. I've tried a lot and searched a lot but I don't manage to fix this, I'm still new to R.

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), R=999, paired = T)
    }, simplify = FALSE) -> result

But now I have two problems:

1- My result is a list of class "perm.htest" 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.

I've been advised to use:

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

The problem is that MKinfer::perm.t.test perform both a permutation-t-test and a parametric t-test. So in the results, there is both a p.value and a perm.p.value, a there are both conf.int and perm.conf.int. So if I use broom::tidy, it will recognise it an htest and I will just get the results of the parametric t-tests.

Maybe something similar would work:

dtlist <- combn(levels(data$t), 2, function(x) {
  dt <- MKinfer::perm.t.test(respvar~t,data = subset(data, t %in% x), R=999, paired = T)
  df <- data.frame(
    perm.p = dt$perm.p.value,
    stat = dt$statistic)
  return(df)
})

But I need a data frame as an output that I can then export in csv.

Does anyone have a solution?

Thank you in advance.

Upvotes: 2

Views: 116

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 389135

You can use -

do.call(rbind, combn(levels(data$t), 2, function(x) {
  dt <- MKinfer::perm.t.test(exparat~t,
            data = subset(data, t %in% x), R=999, paired = T)
  data.frame(perm.p = dt$perm.p.value,stat = dt$statistic)
}, simplify = FALSE)) -> result

result$combination <- combn(levels(data$t), 2, paste0, collapse = ' vs ')

Upvotes: 2

Zaw
Zaw

Reputation: 1474

library(tidyverse)

tidy_result <- function(result) {
  tibble(
    estimate = result$estimate,
    statistic = result$statistic,
    conf.low = result$conf.int[1],
    conf.high = result$conf.int[2],
    perm.p.value = result$perm.p.value
  )
}

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

map_dfr(result, tidy_result)


# # A tibble: 6 x 5
#   estimate statistic conf.low conf.high perm.p.value
#      <dbl>     <dbl>    <dbl>     <dbl>        <dbl>
# 1   -0.015    -0.116   -0.307    0.277         0.918
# 2   -0.073    -0.764   -0.289    0.143         0.470
# 3   -0.04     -0.670   -0.175    0.0950        0.528
# 4   -0.058    -0.482   -0.330    0.214         0.593
# 5   -0.025    -0.220   -0.282    0.232         0.824
# 6    0.033     0.292   -0.222    0.288         0.748

Upvotes: 2

Related Questions