Problem: using cor.test with by( )

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem: using cor.test with by( )

Che-hsu (Joe) Chang
Hello everyone,

 

I'm a new R user switching from SAS and JMP. In the first few days, I have
been trying to do a fairly simple task but yet found no success. I've
checked the help archive as well as few R textbooks but didn't seem to find
the answer. So, please help me if you can.

 

Basically, I want to calculate the correlation between variable A and B for
every subject in my study. (yep, that simple)

What I did is this:

 

by(data, id, function (x) cor.test(A,B, data=x))

 

The results gave me numbers of correlation for each subject. But, the
problem is that, all these correlations are the same numbers and the sample
size was always the entire database (including all subjects). I've also
tried the lm function instead of the cor.test, and the by() function works
fine. Can any of you tell me what I did wrong? Or could you tell me what is
the best way to apply a function by subjects? Thank you!

 

 

Best,

Che-hsu (Joe) Chang, Sc.D., P.T.

 


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem: using cor.test with by( )

Peter Dalgaard
Che-hsu (Joe) Chang wrote:

> Hello everyone,
>
>  
>
> I'm a new R user switching from SAS and JMP. In the first few days, I have
> been trying to do a fairly simple task but yet found no success. I've
> checked the help archive as well as few R textbooks but didn't seem to find
> the answer. So, please help me if you can.
>
>  
>
> Basically, I want to calculate the correlation between variable A and B for
> every subject in my study. (yep, that simple)
>
> What I did is this:
>
>  
>
> by(data, id, function (x) cor.test(A,B, data=x))
>
>  
>
> The results gave me numbers of correlation for each subject. But, the
> problem is that, all these correlations are the same numbers and the sample
> size was always the entire database (including all subjects). I've also
> tried the lm function instead of the cor.test, and the by() function works
> fine. Can any of you tell me what I did wrong? Or could you tell me what is
> the best way to apply a function by subjects? Thank you!
>
>  
>
>  
Only the model formula interface to cor.test uses the data argument, so
you need either

cor.test(x$A,x$B)

or

cor.test(~A+B, data=x)

>  
>
> Best,
>
> Che-hsu (Joe) Chang, Sc.D., P.T.
>
>  
>
>
>  


--
   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
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem: using cor.test with by( )

Liviu Andronic
In reply to this post by Che-hsu (Joe) Chang
On Sun, Mar 30, 2008 at 7:11 PM, Che-hsu (Joe) Chang <[hidden email]> wrote:
>  Basically, I want to calculate the correlation between variable A and B for
>  every subject in my study. (yep, that simple)
>
>  What I did is this:
>  by(data, id, function (x) cor.test(A,B, data=x))

This recent thread [1] might prove of interest.
Liviu

[1] http://www.nabble.com/p-value-in-Spearman-rank-order-tt15738907.html

______________________________________________
[hidden email] 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.