0

I have some data from an experiment to analyse with R but I have a problem and after days of search, I can't find a solution.

I need to run multiple paired permutation t-tests on my data. 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)

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

For examples, I have to say if there are differences in my response variable (respvar) between combinations of times (t) independently for each treatment (treat). If I had to use a parametric t-test I would have used a dplyr pipe and the function group_by:

stat.test <- data %>%
  group_by(treat) %>%
  t_test(exparat ~ t, paired = TRUE)

But I can't do the same thing for permutation t-tests (perm.t.test, package: RVAideMemoire), because it only allows tests for factors with two levels. While my factor time (t) has 4 levels. One solution would be to subset my data for each pair of time (t) like this:

perm.t.test(exparat~t,data = subset(data, t == "T1" | t == "T2"), nperm=999, paired = T)
perm.t.test(exparat~t,data = subset(data, t == "T1" | t == "T3"), nperm=999, paired = T)
perm.t.test(exparat~t,data = subset(data, t == "T2" | t == "T3"), nperm=999, paired = T)
perm.t.test(exparat~t,data = subset(data, t == "T1" | t == "T2"), nperm=999, paired = T)
perm.t.test(exparat~t,data = subset(data, t == "T1" | t == "T3"), nperm=999, paired = T)
perm.t.test(exparat~t,data = subset(data, t == "T2" | t == "T3"), nperm=999, paired = T)
    
#and so on

But it seems a very inefficient and time-consuming way to do it. And in my real dataset, I do have many more levels of the factor t, so it will take a very long time to set up all this.

Can anyone help me to set a loop for doing this?

Thank you in advance.

Valen78
  • 115
  • 6
  • Can you explain what t-tests exactly you want to perform? A t-test compared only two groups, but you have 14 levels in your predictor variable. So what do you want to compare? – George Savva May 28 '21 at 10:17
  • I would like to perform tests between times, for example T1 vs T2, T2 vs T3, T3 vs T4 and so on. I've modified the post to clarify it. – Valen78 May 29 '21 at 00:44

1 Answers1

1

You can use combn to get all the combinations of data$t value.

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

result
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you Ronak, but it seems it doesn't work. I get this error: "Error in perm.t.test.formula(exparat ~ t, data = subset(data, t %in% x), : t is not a 2-levels factor " – Valen78 May 29 '21 at 06:51
  • Hi Ronak, if it can help, I've modified the post and added the code to reproduce a reduced version of my dataset. – Valen78 May 30 '21 at 02:27
  • 1
    Hi Valerio, If I use the data provided by you with `data = data.frame(treat, subj, t, exparat, stringsAsFactors = TRUE)` and use `MKinfer::perm.t.test` I don't get any error. Have you tried the answer on the same data? – Ronak Shah May 30 '21 at 02:36
  • Thank you Ronak, that's true, it works with MKinfer::perm.t.test, I was using RVAideMemoire::perm.t.test and it was not working. But I still have two problems: one is that I don't know which test is which, because there is no mention in the results to the combinations of t. The second is that I would need a data.frame output, with each row containing a test. Do you think is possible? – Valen78 May 30 '21 at 02:58
  • I have asked another question for this in here: https://stackoverflow.com/questions/67757653/transform-a-list-of-t-tests-results-into-a-data-frame . I would be really glad if you could help me. – Valen78 May 30 '21 at 04:26
  • 1
    Sorry, I did not see your edit in the comment. I have added an answer there. Hope that would be helpful. – Ronak Shah May 30 '21 at 04:35
  • Thank you so much Ronak! Just a last thing, in the function you gave me data are not grouped by treatment (treat). Is there a way to use combn on subsets of my data for the factor treat? Like a using both 'by(data, data$treat, function(sub)' and 'combn(levels(data$t), 2, function(x)'. Sorry I'm new to R, but I find these loops extremely useful and I would love to learn. – Valen78 May 30 '21 at 05:27
  • 1
    Yes, that should be possible. I think you can try something like this - `by(data, data$treat, function(sub) combn(levels(sub$t), 2, function(x) { perm.t.test(exparat~t,data = subset(data, t %in% x), nperm=999, paired = T) }, simplify = FALSE)) ` – Ronak Shah May 30 '21 at 06:26