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.
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