[R] correlation help

William Revelle lists at revelle.net
Mon Sep 21 04:28:20 CEST 2009


Adrian,

To find all the correlations between columns of a matrix and to find 
their individual significance levels (questionable given that you are 
doing many correlations) use either the rcorr  function in the Hmisc 
package or the  corr.test function in the psych package.

Bill



\At 6:19 PM -0400 9/20/09, Adrian Johnson wrote:
>thank you john.
>however, I am finding it difficult to automate on a matrix.
>
>Pardon my ignorance in R computing:
>
>I do not know how to automate on a matrix.
>
>If I do the following it works:
>>  x = cor.test(d6[1,],d6[2,])
>>  x
>
>        Pearson's product-moment correlation
>
>data:  d6[1, ] and d6[2, ]
>t = 10.5196, df = 10, p-value = 9.973e-07
>alternative hypothesis: true correlation is not equal to 0
>95 percent confidence interval:
>  0.8520623 0.9883592
>sample estimates:
>      cor
>0.9576655
>
>
>If I want to run it on all rows, I do not know how to do it.
>
>I tried following,
>
>>  lapply(d6,cor.test)
>Error in cor.test.default(X[[1L]], ...) :
>  element 1 is empty;
>   the part of the args list of 'length' being evaluated was:
>   (y)
>
>
>>  sapply(d6,cor.test)
>Error in cor.test.default(X[[1L]], ...) :
>  element 1 is empty;
>   the part of the args list of 'length' being evaluated was:
>   (y)
>
>>  for(i in 1:14659){
>+ k = i+1
>+ cor.test(d6[i,],d6[k,])
>+ x = cor.test(d6[i,],d6[k,])
>+ return(x)}
>Error: no function to return from, jumping to top level
>
>
>I appreciate your help.
>
>thank you.
>Adrian
>
>On Sun, Sep 20, 2009 at 6:17 PM, Adrian Johnson
><oriolebaltimore at gmail.com> wrote:
>>  thank you john.
>>  however, I am finding it difficult to automate on a matrix.
>>
>>  Pardon my ignorance in R computing:
>>
>>  I do not know how to automate on a matrix.
>>
>>  If I do the following it works:
>>>  x = cor.test(d6[1,],d6[2,])
>>>  x
>>
>>         Pearson's product-moment correlation
>>
>>  data:  d6[1, ] and d6[2, ]
>>  t = 10.5196, df = 10, p-value = 9.973e-07
>>  alternative hypothesis: true correlation is not equal to 0
>>  95 percent confidence interval:
>>   0.8520623 0.9883592
>>  sample estimates:
>>       cor
>>  0.9576655
>>
>>
>>  If I want to run it on all rows, I do not know how to do it.
>>
>>  I tried following,
>>
>>>  lapply(d6,cor.test)
>>  Error in cor.test.default(X[[1L]], ...) :
>>   element 1 is empty;
>>    the part of the args list of 'length' being evaluated was:
>>    (y)
>>
>>
>>>  sapply(d6,cor.test)
>>  Error in cor.test.default(X[[1L]], ...) :
>>   element 1 is empty;
>>    the part of the args list of 'length' being evaluated was:
>>    (y)
>>
>>>  for(i in 1:14659){
>>  + k = i+1
>>  + cor.test(d6[i,],d6[k,])
>>  + x = cor.test(d6[i,],d6[k,])
>>  + return(x)}
>>  Error: no function to return from, jumping to top level
>>
>>
>>  I appreciate your help.
>>
>>  thank you.
>>  Adrian
>>
>>
>>
>>
>>
>>
>>  On Sun, Sep 20, 2009 at 5:13 PM, John Kane <jrkrideau at yahoo.ca> wrote:
>>>  ?cor
>>>  ?cor.test
>>>
>>>  --- On Sun, 9/20/09, Adrian Johnson <oriolebaltimore at gmail.com> wrote:
>>>
>>>>  From: Adrian Johnson <oriolebaltimore at gmail.com>
>>>>  Subject: [R] correlation help
>>>>  To: r-help at r-project.org
>>>>  Received: Sunday, September 20, 2009, 5:00 PM
>>>>  Dear group,
>>>>
>>>>  I have a matrix like the following:
>>>>
>>>>  Name     Sample1
>>>>  sample2    sample3   sample4 .....
>>>>  sample(n)
>>>>  nm1        10.5
>>>>      13.5
>>>>  30             31
>>>>  nm2         8
>>>>            11
>>>>        34
>>>>     29
>>>>  nm3         9
>>>>            10.3
>>>>      27.8         35
>>>>  nm(j)
>>>>
>>>>
>>>>  I want to be able to calculate correlation between
>>>>  all pairs of names.
>>>>  For example (nm1,nm2), (nm1,nm3), (nm1,nmj), (nm2,nm3),
>>>>  (nm2,nmj)....
>>>>
>>>>  Then I want to calculate the significance of correlation
>>>>  using t-score
>>>>  or p-value.
>>>>
>>>>  I can calculate correlation coeffecient in excel but not
>>>>  significance
>>>>  in both excel and R.
>>>>
>>>>  I want to be able to do it in R, I appreciate your help.
>>>>  thank you.
>>>>  Ad.
>>>>
>>>>  ______________________________________________
>>>>  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.
>>>>
>>>
>>>
>>>       __________________________________________________________________
>>>  Be smarter than spam. See how smart SpamGuard is at giving junk 
>>>email the boot with the All-new Yahoo! Mail.  Click on Options in 
>>>Mail and switch to New Mail today or register for free at 
>>>http://mail.yahoo.ca
>>>
>>
>
>______________________________________________
>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.


-- 
William Revelle		http://personality-project.org/revelle.html
Professor			http://personality-project.org/personality.html
Department of Psychology             http://www.wcas.northwestern.edu/psych/
Northwestern University	http://www.northwestern.edu/
Use R for psychology                       http://personality-project.org/r
It is 5 minutes to midnight	http://www.thebulletin.org




More information about the R-help mailing list