[R] T-test by groups

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Thu Feb 26 11:58:07 CET 2009


Uwe Ligges wrote:
> 
> 
> Ingrid Tohver wrote:
>> I would like to run a t-test within a "by" group function. My 
>> dataset, "error",  is organized as the following (I have 133 Sites):
>>
>> Site week Dataset Region  lat_map  long_map mean_tsim diff20 diff40 
>> diff80
>> ALFI   15   USACE     UC 48.15625 -117.0938      8.87   1.34   1.90   
>> 2.98
>> ALFI   16   USACE     UC 48.15625 -117.0938     10.28   0.57   1.08   
>> 2.27
>> ALFI   17   USACE     UC 48.15625 -117.0938     11.08   0.74   1.30   
>> 2.52
>> ALFI   18   USACE     UC 48.15625 -117.0938     12.23   0.42   1.11   
>> 2.42
>> ALFI   19   USACE     UC 48.15625 -117.0938     13.19   1.00   1.73   
>> 3.14
>> ALFI   20   USACE     UC 48.15625 -117.0938     14.31   1.77   2.62   
>> 3.78
>>
>> I am interested in running the t-test by the "Site" index. My code 
>> looks like this:
>>
>> t_test<-by(error, error['Site'], function(dat) t.test(subset(error
>> $diff20),subset(error$diff80), data=dat))
>>
>> This code runs the t-test, but over the whole dataset without 
>> discriminating by Site, so each Site's result is the same. Could 
>> someone help determine a better approach or why mine is not working.
> 
> I guess you want
> 
> by(error, error['Site'], function(dat) t.test(dat$diff20, dat$diff40))


...and most likely also, paired=TRUE. Notice that the data= argument
only works for the formula interface, which is not (yet) defined for
paired data, but only for the y~group type two-sample case.


> Uwe Ligges
> 
> 
>> Thank you,
>> Ingrid
>>
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
> 
> ______________________________________________
> 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.


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list