[R] apply, t-test and p-values PREVIOUS SUGGESTION DID NOT WORK

John Sorkin jsorkin at grecc.umaryland.edu
Thu Oct 16 18:07:45 CEST 2008

```Although I am grateful to Jorge and David for their suggestions, their solution will not solve my problem as sample2 is not fixed. Let me demonstrate what I want to do:

# Define two matricies
sample1<-matrix(data=c(1:20),byrow=TRUE,ncol=5,nrow=4)
sample2<-sample1+rnorm(20)

These are the computations that I would like to perform using apply (or some similar method):

# Compute t-test comparing first row of sample 1 to first row of sample 2
t.test(sample1[1,],sample2[1,])

# Compute t-test comparing second row of sample 1 to second row of sample 2
t.test(sample1[2,],sample2[2,])

# Compute t-test comparing third row of sample 1 to third row of sample 2
t.test(sample1[3,],sample2[3,])

# Compute t-test comparing fourth row of sample 1 to fourth row of sample 2
t.test(sample1[4,],sample2[4,])

As you can see, the code below does not return the same values! It does not work.
apply(sample1,1,function(x) t.test(x,sample1)\$p.value)

Sugestions would be appreciated!
John

John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

>>> "Jorge Ivan Velez" <jorgeivanvelez at gmail.com> 10/16/2008 10:04 AM >>>
Thanks for the correction David!  You're absolutely right!  A typo  :- )
...
Cheers,

Jorge

On Thu, Oct 16, 2008 at 10:00 AM, David Winsemius <dwinsemius at comcast.net>wrote:

>
> On Oct 16, 2008, at 8:11 AM, Jorge Ivan Velez wrote:
>
>  Dear John,
>> Yes. Assuming that 'sample2' is fixed, something like this should do what
>> you want:
>>
>> # Data sets
>> set.seed(123)
>> sample1=matrix(rnorm(100),ncol=10)
>> sample2=rnorm(10,5,4)
>>
>> # t-test p-values
>> apply(X,1,function(x) t.test(x,sample2)\$p.value)
>>
>
> It seems clear that you meant to type:
> apply(sample1,1,function(x) t.test(x,sample2)\$p.value)
>
>
>
>> [1] 0.002348970 0.004733230 0.004810952 0.004907549 0.002342979
>> 0.018748229
>> 0.003546655 0.006084410
>> [9] 0.003100983 0.007007980
>>
>>
>> HTH,
>>
>> Jorge
>>
>>
>> On Thu, Oct 16, 2008 at 8:01 AM, John Sorkin <jsorkin at grecc.umaryland.edu
>> >wrote:
>>
>>  R 2.7.2
>>> Windows XP
>>>
>>> I am using apply to compute a series of Student's t-test from two
>>> matrices,
>>> sample1 and sample2.
>>> boo<-apply(sample1,1,t.test,sample2)
>>>
>>> I want to pick of the p-values from the tests, but can't seem to get it
>>> to
>>> work. I have tried several methods to get the values including:
>>> boo<-apply(sample1,1,t.test\$t.test,sample2)
>>> boo<-apply(sample1,1,t.test,sample2)\$t.test
>>>
>>> any suggestions?
>>>
>>> Thanks
>>> John
>>>
>>> John Sorkin M.D., Ph.D.
>>> Chief, Biostatistics and Informatics
>>> Baltimore VA Medical Center GRECC,
>>> University of Maryland School of Medicine Claude D. Pepper OAIC,
>>> University of Maryland Clinical Nutrition Research Unit, and
>>> Baltimore VA Center Stroke of Excellence
>>>
>>> University of Maryland School of Medicine
>>> Division of Gerontology
>>> Baltimore VA Medical Center
>>> 10 North Greene Street
>>> GRECC (BT/18/GR)
>>> Baltimore, MD 21201-1524
>>>
>>> (Phone) 410-605-7119
>>> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>>> jsorkin at grecc.umaryland.edu
>>> Confidentiality Statement:
>>> This email message, including any attachments, is for ...{{dropped:13}}
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help