[R] how to aggregate T-test result in an elegant way?

arun smartpink111 at yahoo.com
Mon Jan 7 06:32:21 CET 2013


HI,
I tried to create an example dataset (as you didn't provide the data).
set.seed(25)
a<-array(sample(1:50,60,replace=TRUE),dim=c(2,10,3))
dimnames(a)[[1]]<-c("13%","21%")
dimnames(a)[[2]]<-paste("TWF2H",101:110,sep="")
dimnames(a)[[3]]<-c("EW.INCU","EW.17.5","EMW")


str(a)
# int [1:2, 1:10, 1:3] 21 35 8 45 7 50 32 17 4 15 ...
 #- attr(*, "dimnames")=List of 3
  #..$ : chr [1:2] "13%" "21%"
  .#.$ : chr [1:10] "TWF2H101" "TWF2H102" "TWF2H103" "TWF2H104" ...
  #..$ : chr [1:3] "EW.INCU" "EW.17.5" "EMW"

res<-lapply(lapply(seq_len(dim(a)[3]),function(i) t.test(a[dimnames(a)[[1]][1],,i],a[dimnames(a)[[1]][2],,i])),function(x) data.frame(mean=x$estimate,p.value=x$p.value))
res1<-do.call(rbind,res)
  row.names(res1)[grep("mean of x",row.names(res1))]<-gsub("(.*\\.).*$","\\113%",row.names(res1)[grep("mean of x",row.names(res1))])
 row.names(res1)[grep("mean of y",row.names(res1))]<-gsub("(.*\\.).*$","\\121%",row.names(res1)[grep("mean of y",row.names(res1))])
res1
#            mean   p.value
#EW.INCU.13% 22.3 0.2754842
#EW.INCU.21% 29.3 0.2754842
#EW.17.5.13% 20.5 0.4705772
#EW.17.5.21% 16.0 0.4705772
#EMW.13%     23.9 0.9638679
#EMW.21%     24.2 0.9638679
A.K.




----- Original Message -----
From: Yao He <yao.h.1988 at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Sunday, January 6, 2013 11:21 PM
Subject: Re: [R] how to aggregate T-test result in an elegant way?

Thank you,it is really helpful everytime.

I didn't provide any example data because I thought it is just a
question of how to report t.test() result in R.
However,as you say,it is better to show more details for finding an elegant way

In fact  I generate a 3-dimension array like that:
str(a)
num [1:2, 1:245, 1:3] 47.5 NA 48.9 NA 47.5 ...
- attr(*, "dimnames")=List of 3
  ..$ : chr [1:2] "13%" "21%"
  ..$ : chr [1:245] "TWF2H101" "TWF2H105" "TWF2H106" "TWF2H110" ...
  ..$ : chr [1:3] "EW.INCU" "EW.17.5" "EMW"

I want to do two sample mean t-test between 13% and 21% for each
variable "EW.INCU" "EW.17.5" "EMW".

So I try these codes:
variable<-dimnames(a)[[3]]
  O2<-dimnames(a)[[1]]
  for (i in variable) {
    print(i)
    print(O2[1])
    print(O2[2])
    print(t.test(a[O2[1],,i],a[O2[2],,i],na.rm=T))
    }

I don't think it is an elegant way and I am inexperience to report raw result.
Could you give me more help?

Yao He

2013/1/7 arun <smartpink111 at yahoo.com>:
> Hi,
> You didn't provide any example data.  So, I am not sure whether this helps.
>
> set.seed(15)
> dat1<-data.frame(A=sample(10:20,5,replace=TRUE),B=sample(18:28,5,replace=TRUE),C=sample(25:35,5,replace=TRUE),D=sample(20:30,5,replace=TRUE))
>  res<-lapply(lapply(seq_len(ncol(dat2)),function(i) t.test(dat2[,i],dat1[,1],paired=TRUE)),function(x) data.frame(meanDiff=x$estimate,p.value=x$p.value))# paired
> names(res)<-paste("A",LETTERS[2:4],sep="")
> res<- do.call(rbind,res)
> res
>   # meanDiff     p.value
> #AB      9.4 0.021389577
> #AC     15.0 0.002570261
> #AD     10.6 0.003971604
>
>
> #or
> res1<-lapply(lapply(seq_len(ncol(dat2)),function(i) t.test(dat2[,i],dat1[,1],paired=FALSE)),function(x) data.frame(mean=x$estimate,p.value=x$p.value))
> names(res1)<-paste("A",LETTERS[2:4],sep="")
> res1<-do.call(rbind,res1)
> row.names(res1)[grep("mean of y",row.names(res1))]<-gsub("(.*\\.).*","\\1A",row.names(res1)[grep("mean of y",row.names(res1))])
> row.names(res1)[grep("mean of x",row.names(res1))]<-gsub("(\\w)(\\w)(\\.).*","\\1\\2\\3\\2",row.names(res1)[grep("mean of x",row.names(res1))])
> res1
> #     mean      p.value
> #AB.B 25.2 1.299192e-03
> #AB.A 15.8 1.299192e-03
> #AC.C 30.8 5.145519e-05
> #AC.A 15.8 5.145519e-05
> #AD.D 26.4 1.381339e-03
> #AD.A 15.8 1.381339e-03
>
>
> A.K.
>
>
>
> ----- Original Message -----
> From: Yao He <yao.h.1988 at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Sunday, January 6, 2013 10:20 PM
> Subject: [R] how to aggregate T-test result in an elegant way?
>
> Dear all:
>
> Plan 1:
> I want to do serval t-test means for different variables in a loop ,
> so I want to add all results to an object then  dump() them to an
> text. But I don't know how to append T-test result to the object?
>
> I have already plot the barplot and I want to know an elegant way to
> report raw result.
> Can anybody give me some pieces of advice?
>
> Yao He
> —————————————————————————
> Master candidate in 2rd year
> Department of Animal genetics & breeding
> Room 436,College of Animial Science&Technology,
> China Agriculture University,Beijing,100193
> E-mail: yao.h.1988 at gmail.com
> ——————————————————————————
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
—————————————————————————
Master candidate in 2rd year
Department of Animal genetics & breeding
Room 436,College of Animial Science&Technology,
China Agriculture University,Beijing,100193
E-mail: yao.h.1988 at gmail.com
——————————————————————————





More information about the R-help mailing list