借助应用函数计算T检验

时间:2013-10-01 19:22:58

标签: r statistics

我有一个矩阵:

>data

      A  A  A  B  B  C
gene1 1  6 11 16 21 26
gene2 2  7 12 17 22 27
gene3 3  8 13 18 23 28
gene4 4  9 14 19 24 29
gene5 5 10 15 20 25 30

我想测试每个基因的不同组之间每个基因(行)值的平均值是否不同?我想用它进行T检验。该函数应该将所有列都归属于组A,将所有列归属于组B,将所有列归属于组C,...并计算每个列之间的T检验每个基因的组(每组包含几列) 我从回答我的预览帖子得到的实施是:

 Results <- combn(colnames(data), 2, function(x) t.test(data[,x]), simplify = FALSE)
 sapply(Results, "[", c("statistic", "p.value"))

但它确实在所有列之间进行计算,而不是在每行的组之间进行计算。有人可以帮我修改这段代码来计算组间的T检验,比如我的数据吗?

2 个答案:

答案 0 :(得分:5)

也许这可能有用

> Mat <- matrix(1:20, nrow=4, dimnames=list(NULL, letters[1:5]))
> # t.test
> Results <- combn(colnames(Mat), 2, function(x) t.test(Mat[,x]), simplify = FALSE)
> names(Results) <- apply(Pairs, 2, paste0, collapse="~")
> Results  # Only the first element of the `Results` is shown
$`a~b`  # t.test applied to a and b

    One Sample t-test

data:  Mat[, x] 
t = 5.1962, df = 7, p-value = 0.001258
alternative hypothesis: true mean is not equal to 0 
95 percent confidence interval:
 2.452175 6.547825 
sample estimates:
mean of x 
      4.5 
     ...

更好的输出:

> sapply(Results, "[", c("statistic", "p.value"))
          a~b        a~c         a~d         a~e        b~c         b~d          b~e          c~d         
statistic 5.196152   4.140643    3.684723    3.439126   9.814955    6.688732     5.41871      14.43376    
p.value   0.00125832 0.004345666 0.007810816 0.01085005 2.41943e-05 0.0002803283 0.0009884764 1.825796e-06
          c~e          d~e         
statistic 9.23682      19.05256    
p.value   3.601564e-05 2.730801e-07

答案 1 :(得分:0)

几乎在那里,使用apply,你不在函数内部给出参数,但在

之外
data<-matrix(1:20,4,5)
Tscore<- apply(data, 2, t.test, alternative = c("two.sided", "less", "greater"),mu = 0, paired = FALSE, var.equal = FALSE,conf.level = 0.95)

并测试这是否是你想要的,检查t stats

t.test(data[,1], alternative = c("two.sided", "less", "greater"),mu = 0, paired = FALSE, var.equal = FALSE,conf.level = 0.95)

我可能误解了这个问题,但我刚刚实现了你的y = NULL,单列测试