使用 perm.t.test 的多重配对排列 t 检验
Multiple paired permutation t-tests using perm.t.test
我有一些实验数据要用 R 分析,但我遇到了问题,经过几天的搜索,我找不到解决方案。
我需要对我的数据进行 运行 多重配对排列 t 检验。这是我的数据集的简化版本:
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
例如,我必须说明每次治疗 (treat) 的时间 (t) 组合之间的响应变量 (respvar) 是否存在差异。如果我必须使用参数 t 检验,我会使用 dplyr 管道和函数 group_by:
stat.test <- data %>%
group_by(treat) %>%
t_test(exparat ~ t, paired = TRUE)
但我不能对排列 t 检验(perm.t.test,包:RVAideMemoire)做同样的事情,因为它只允许对具有两个水平的因子进行检验。而我的因子时间 (t) 有 4 个级别。一种解决方案是为每一对时间 (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)
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
但这似乎是一种非常低效且耗时的方法。在我的真实数据集中,我确实有更多的因子 t 水平,所以设置所有这些需要很长时间。
任何人都可以帮我设置一个循环吗?
提前致谢。
您可以使用 combn
来获得 data$t
值的所有组合。
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
我有一些实验数据要用 R 分析,但我遇到了问题,经过几天的搜索,我找不到解决方案。
我需要对我的数据进行 运行 多重配对排列 t 检验。这是我的数据集的简化版本:
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
例如,我必须说明每次治疗 (treat) 的时间 (t) 组合之间的响应变量 (respvar) 是否存在差异。如果我必须使用参数 t 检验,我会使用 dplyr 管道和函数 group_by:
stat.test <- data %>%
group_by(treat) %>%
t_test(exparat ~ t, paired = TRUE)
但我不能对排列 t 检验(perm.t.test,包:RVAideMemoire)做同样的事情,因为它只允许对具有两个水平的因子进行检验。而我的因子时间 (t) 有 4 个级别。一种解决方案是为每一对时间 (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)
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
但这似乎是一种非常低效且耗时的方法。在我的真实数据集中,我确实有更多的因子 t 水平,所以设置所有这些需要很长时间。
任何人都可以帮我设置一个循环吗?
提前致谢。
您可以使用 combn
来获得 data$t
值的所有组合。
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