[R] multiple paired t-tests

David Winsemius dwinsemius at comcast.net
Wed Mar 25 00:00:02 CET 2009


?try
?tryCatch

On Mar 24, 2009, at 6:04 PM, Dan Kortschak wrote:

> Hi Jorge,
>
> That is exactly what I wanted -  I should have given a reasonable
> number of observations (my set has *almost* all paired observations,
> so it will still break with that approach unless I manicure the data
> set). Is there a way to fail nicely on a single one of the tests
> without the whole thing failing?
>
> again, thanks for your help
> Dan
>
>
> On 25/03/2009, at 7:46 AM, Jorge Ivan Velez wrote:
>
>> # Data
>> set.seed(1)
>> x<-sample(1:3,100,replace=TRUE)
>> y<-sample(1:20,100,replace=TRUE)
>> z<-rnorm(100)
>> Data<-data.frame(x,y,z)
>>
>> # Observations for Type and Class
>> with(Data, table(x,y))
>>
>>
>> # Splitting the data by Class
>> SD<-with(Data,split(Data,y))
>>
>> res<-lapply(SD, function(.data){
>>          # Type combinations by Class
>>          combs<-t(combn(sort(unique(.data[,1])),2))
>>
>>          # Applying the t-test for them
>>            apply(combs,1, function(.r){
>>                x1<-.data[.data[,1]==.r[1],3]  # select third column
>>                x2<-.data[.data[,1]==.r[2],3]  # select third column
>>                tvalue<-t.test(x1,x2)
>>                res<-c(tvalue$statistic,tvalue$parameter,tvalue
>> $p.value)
>>                names(res)<-c('stat','df','pvalue')
>>                res
>>                        }
>>                       )
>>            }
>>     )
>>
>> res
>>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list