user2806363
user2806363

Reputation: 2593

computing T-test with help of apply function

I have a matrix :

>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

I want to to test whether the mean of each gene (rows) values are different between different groups for each gene or not? I want to use T-test for it. The function should take all columns belong to group A, take all columns belongs to group B, take all columns belongs to group C,... and calculate the T-test between each groups for each genes.(every groups contains several columns) on implementation which I got from answer to my previews post is :

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

but it does compute between all columns rather than between groups for every row. can somebody help me how to modify this code to calculate T test between groups like for my data ?

Upvotes: 1

Views: 9677

Answers (2)

Jilber Urbina
Jilber Urbina

Reputation: 61154

Maybe this can be usuful

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

A nicer output:

> 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

Upvotes: 5

Ananta
Ananta

Reputation: 3711

almost there, with apply, you don't give arguments inside functions, but outside

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)

and to test if this is what you wanted, check t stats

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

I may have misunderstood the question though, I just implemented your y=NULL, t test of single column

Upvotes: 0

Related Questions