[R] how to aggregate T-test result in an elegant way?
arun
smartpink111 at yahoo.com
Mon Jan 7 17:25:33 CET 2013
Hi Yao,
You could also have the results in a wide format:
res<-do.call(rbind,lapply(lapply(split(b,b$variable),function(x) t.test(x$value[x$O2=="13%"],x$value[x$O2=="21%"])),function(x) data.frame(mean13=x$estimate[1],mean21=x$estimate[2],p.value=x$p.value,CILow=x$conf.int[1],CIHigh=x$conf.int[2])))
res
# mean13 mean21 p.value CILow CIHigh
#EMW 14.35000 17.68000 0.09355374 -7.682686 1.022686
#EW.17.5 42.87000 45.97333 0.17464018 -9.265622 3.058955
#EW.INCU 49.61333 47.08333 0.43689727 -7.119234 12.179234
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: Monday, January 7, 2013 10:57 AM
Subject: Re: [R] how to aggregate T-test result in an elegant way?
Hi,arun
Yes , I just want to do the t.test
I think maybe it is not necessary to generate a 3D array from the raw
data.frame by acast() at first
Thanks a lot
2013/1/7 arun <smartpink111 at yahoo.com>:
> Hi Yao,
>
> It's okay.
>
> How did you generate the 3 D array?
> Using ?acast()
>
> I am not sure I understand your question "
>
> if you meet a t-test task as I described , is that generate a
> high-dimension array a good way ?"
>
> Do you want to do the t-test in the melt dataset?
>
> b<- read.table(text="
> ID O2 variable value
> 1 TWF2H5 13% EW.INCU 49.38
> 2 TWF2H6 13% EW.INCU 48.02
> 3 TWF2H19 13% EW.INCU 51.44
> 280 TWF2H101 13% EW.17.5 42.26
> 281 TWF2H105 13% EW.17.5 43.52
> 282 TWF2H106 13% EW.17.5 42.83
> 472 TWF2N102 21% EW.17.5 45.97
> 473 TWF2N104 21% EW.17.5 43.32
> 474 TWF2N106 21% EW.17.5 48.63
> 689 TWF2N2 21% EMW 19.57
> 690 TWF2N6 21% EMW 18.07
> 691 TWF2N10 21% EMW 15.4
> 491 TWF2H5 13% EMW 15.61
> 492 TWF2H6 13% EMW 13.41
> 493 TWF2H19 13% EMW 14.03
> 199 TWF2N2 21% EW.INCU 48.69
> 200 TWF2N6 21% EW.INCU 50.52
> 201 TWF2N10 21% EW.INCU 42.04
> ",sep="",header=TRUE,stringsAsFactors=FALSE)
> res<-lapply(lapply(split(b,b$variable),function(x) t.test(x$value[x$O2=="13%"],x$value[x$O2=="21%"])),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
> #EMW.13% 14.35000 0.09355374
> #EMW.21% 17.68000 0.09355374
> #EW.17.5.13% 42.87000 0.17464018
> #EW.17.5.21% 45.97333 0.17464018
> #EW.INCU.13% 49.61333 0.43689727
> #EW.INCU.21% 47.08333 0.43689727
>
> 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: Monday, January 7, 2013 4:00 AM
> Subject: Re: [R] how to aggregate T-test result in an elegant way?
>
> Hi, arun
> I'm so sorry for that isn't helpful.
> One of question is that I don't know how to subset a small part as it
> is a 3-dimension array so I just show the structure of that.
> I tried dput() to a file , then what should I do for subsetting it?
>
> Another question is :
> My rawdata is a "melt" dataframe like that:
> IID O2 variable value
> 1 TWF2H5 13% EW.INCU 49.38
> 2 TWF2H6 13% EW.INCU 48.02
> 3 TWF2H19 13% EW.INCU 51.44
> 280 TWF2H101 13% EW.17.5 42.26
> 281 TWF2H105 13% EW.17.5 43.52
> 282 TWF2H106 13% EW.17.5 42.83
> 472 TWF2N102 21% EW.17.5 45.97
> 473 TWF2N104 21% EW.17.5 43.32
> 474 TWF2N106 21% EW.17.5 48.63
> 689 TWF2N2 21% EMW 19.57
> 690 TWF2N6 21% EMW 18.07
> 691 TWF2N10 21% EMW 15.4
> 491 TWF2H5 13% EMW 15.61
> 492 TWF2H6 13% EMW 13.41
> 493 TWF2H19 13% EMW 14.03
> 199 TWF2N2 21% EW.INCU 48.69
> 200 TWF2N6 21% EW.INCU 50.52
> 201 TWF2N10 21% EW.INCU 42.04
>
> if you meet a t-test task as I described , is that generate a
> high-dimension array a good way ?
> Thank you!
>
> Yao He
> 2013/1/7 arun <smartpink111 at yahoo.com>:
>> 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
>> ——————————————————————————
>>
>
>
>
> --
> —————————————————————————
> 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
> ——————————————————————————
>
--
—————————————————————————
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