applying cor.test to a (m, n) matrix

View: New views
5 Messages — Rating Filter:   Alert me  

applying cor.test to a (m, n) matrix

by Monica Pisica :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi everybody,

I would like to apply cor.test to a matrix with m rows and n columns and get the results in a list of matrices , one matrix for p.val, one for the statistic, one for the correlation and 2 for upper and lower confidence intervals, something analog with cor() applied to a matrix.

I have done my own function to get a matrix of p.values and i suppose i can build similar functions for all the others. But i have used for loops and i wonder if there is any way to actually use one of the functions from the "apply" family to do this in a quicker way.

Here is my little function:

cor.pval <- function(x, method = c("pearson", "kendal", "spearman"), digit=8) {
n <- dim(x)[2]
pval <- matrix(paste(rep("c", n*n), seq(1,n*n), sep = ""), n, n, byrow = T)
for (i in 1:n) {
        for (j in 1:n){
pval[i, j] <- cor.test(x[,i], x[,j], method = method)$p.value
                                  }
                   }
pval <- matrix(round(as.numeric(pval),digit), n, n, byrow = T)
rownames(pval) <- colnames(x)
colnames(pval) <- colnames(x)
return(pval)
}

Thanks for any input,

Monica







_________________________________________________________________


esh_messenger_052008
______________________________________________
R-help@... 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.

Re: applying cor.test to a (m, n) matrix

by Yasir Kaheil :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

interesting request..I'm looking forward to the replies
All I could come up with is putting it in two lines..
pr<-array(0,c(dim(x)[2],dim(x)[2]));
for (i in 1:dim(x)[2]) for (j in 1:dim(x)[2]) pr[i,j]<-cor.test(x[,i],x[,j])$p.val;

y

Monica Pisica wrote:
Hi everybody,

I would like to apply cor.test to a matrix with m rows and n columns and get the results in a list of matrices , one matrix for p.val, one for the statistic, one for the correlation and 2 for upper and lower confidence intervals, something analog with cor() applied to a matrix.

I have done my own function to get a matrix of p.values and i suppose i can build similar functions for all the others. But i have used for loops and i wonder if there is any way to actually use one of the functions from the "apply" family to do this in a quicker way.

Here is my little function:

cor.pval <- function(x, method = c("pearson", "kendal", "spearman"), digit=8) {
n <- dim(x)[2]
pval <- matrix(paste(rep("c", n*n), seq(1,n*n), sep = ""), n, n, byrow = T)
for (i in 1:n) {
        for (j in 1:n){
pval[i, j] <- cor.test(x[,i], x[,j], method = method)$p.value
                                  }
                   }
pval <- matrix(round(as.numeric(pval),digit), n, n, byrow = T)
rownames(pval) <- colnames(x)
colnames(pval) <- colnames(x)
return(pval)
}

Thanks for any input,

Monica







_________________________________________________________________


esh_messenger_052008
______________________________________________
R-help@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.
Yasir H. Kaheil
Catchment Research Facility
The University of Western Ontario

Re: applying cor.test to a (m, n) matrix

by Dimitris Rizopoulos :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

have a look also at function rcor.test() from package ltm.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message -----
From: "Monica Pisica" <pisicandru@...>
To: <r-help@...>
Sent: Thursday, May 08, 2008 9:05 PM
Subject: [R] applying cor.test to a (m, n) matrix


>
> Hi everybody,
>
> I would like to apply cor.test to a matrix with m rows and n columns
> and get the results in a list of matrices , one matrix for p.val,
> one for the statistic, one for the correlation and 2 for upper and
> lower confidence intervals, something analog with cor() applied to a
> matrix.
>
> I have done my own function to get a matrix of p.values and i
> suppose i can build similar functions for all the others. But i have
> used for loops and i wonder if there is any way to actually use one
> of the functions from the "apply" family to do this in a quicker
> way.
>
> Here is my little function:
>
> cor.pval <- function(x, method = c("pearson", "kendal", "spearman"),
> digit=8) {
> n <- dim(x)[2]
> pval <- matrix(paste(rep("c", n*n), seq(1,n*n), sep = ""), n, n,
> byrow = T)
> for (i in 1:n) {
> for (j in 1:n){
> pval[i, j] <- cor.test(x[,i], x[,j], method = method)$p.value
>                                  }
>                   }
> pval <- matrix(round(as.numeric(pval),digit), n, n, byrow = T)
> rownames(pval) <- colnames(x)
> colnames(pval) <- colnames(x)
> return(pval)
> }
>
> Thanks for any input,
>
> Monica
>
>
>
>
>
>
>
> _________________________________________________________________
>
>
> esh_messenger_052008
> ______________________________________________
> R-help@... 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.
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
R-help@... 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.

Re: applying cor.test to a (m, n) matrix

by Kenn Konstabel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've used the following function (I wrote it some time ago so I don't
remember any more why I needed it, but I checked and it still works).  I
don't think you can get rid of for loops altogether: if you look at the code
of apply, you'll see some there too.

The argument STATS specifies those components of a function's output you
want to extract (if the component you're asking for has length >1 then
you'll get the first item). To extract ci's you will need an intermediate
function:

cor.test2 <- function(...) {
    foo<-cor.test(...)
    ci<-foo$conf.int
    foo$ci1<-ci[1]
    foo$ci2<-ci[2]
    foo
}

Then rrapply(my.data, FUN=cor.test2, STATS=c("estimate", "p.value", "ci1",
"ci2")) should do what you want.


rrapply <-
function(x, FUN=t.test, STATS=c("statistic", "p.value"), ...){
   # use FUN=cor.test, STATS=c("estimate", "p.value") for matrix of
correlations and p-values
   K <- ncol(x)
   RESU <- list()
   my.fun <- FUN
       fuf<-my.fun(x[,2],x[,1], ...)
       if(!is.list(fuf)) {
             STATS <- "A"
             my.fun <- function(x,y,...) list(A=FUN(x,y,...))
             }
   neimz <- colnames(x)
   for(i in 1:length(STATS)) {
         fof <- STATS[i]
         RESU[[fof]] <- matrix(NA, ncol=K, nrow=K)
         colnames(RESU[[fof]]) <- neimz
         rownames(RESU[[fof]]) <- neimz
         names(RESU)[i] <- fof
         }

   for(i in 2:K) for(j in 1:(i-1)) {
        foo<-my.fun(x[,i],x[,j], ...)
          for(h in 1:length(STATS)) {
               fof <- STATS[h]
               RESU[[fof]][i,j]<- foo[[fof]]
               }
   }
  if(is.list(fuf)) RESU else RESU$A
}


On Fri, May 9, 2008 at 10:12 AM, Dimitris Rizopoulos <
dimitris.rizopoulos@...> wrote:

> have a look also at function rcor.test() from package ltm.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
>    http://www.student.kuleuven.be/~m0390867/dimitris.htm<http://www.student.kuleuven.be/%7Em0390867/dimitris.htm>
>
>
> ----- Original Message ----- From: "Monica Pisica" <pisicandru@...
> >
> To: <r-help@...>
> Sent: Thursday, May 08, 2008 9:05 PM
> Subject: [R] applying cor.test to a (m, n) matrix
>
>
>
>
>> Hi everybody,
>>
>> I would like to apply cor.test to a matrix with m rows and n columns and
>> get the results in a list of matrices , one matrix for p.val, one for the
>> statistic, one for the correlation and 2 for upper and lower confidence
>> intervals, something analog with cor() applied to a matrix.
>>
>> I have done my own function to get a matrix of p.values and i suppose i
>> can build similar functions for all the others. But i have used for loops
>> and i wonder if there is any way to actually use one of the functions from
>> the "apply" family to do this in a quicker way.
>>
>> Here is my little function:
>>
>> cor.pval <- function(x, method = c("pearson", "kendal", "spearman"),
>> digit=8) {
>> n <- dim(x)[2]
>> pval <- matrix(paste(rep("c", n*n), seq(1,n*n), sep = ""), n, n, byrow =
>> T)
>> for (i in 1:n) {
>> for (j in 1:n){
>> pval[i, j] <- cor.test(x[,i], x[,j], method = method)$p.value
>>                                 }
>>                  }
>> pval <- matrix(round(as.numeric(pval),digit), n, n, byrow = T)
>> rownames(pval) <- colnames(x)
>> colnames(pval) <- colnames(x)
>> return(pval)
>> }
>>
>> Thanks for any input,
>>
>> Monica
>>
>>
>>
>>
>>
>>
>>
>> _________________________________________________________________
>>
>>
>> esh_messenger_052008
>> ______________________________________________
>> R-help@... 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.
>>
>>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
>
> ______________________________________________
> R-help@... 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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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.

Re: applying cor.test to a (m, n) matrix

by Monica Pisica :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks Kenn. I will add your solution to my ever growing colection of functions ;-))
 
Monica

Date: Fri, 9 May 2008 18:35:30 +0300From: lebatsnok@...: pisicandru@...: Re: [R] applying cor.test to a (m, n) matrixCC: r-help@...'ve used the following function (I wrote it some time ago so I don't remember any more why I needed it, but I checked and it still works).  I don't think you can get rid of for loops altogether: if you look at the code of apply, you'll see some there too.The argument STATS specifies those components of a function's output you want to extract (if the component you're asking for has length >1 then you'll get the first item). To extract ci's you will need an intermediate function:cor.test2 <- function(...) {    foo<-cor.test(...)    ci<-foo$conf.int    foo$ci1<-ci[1]    foo$ci2<-ci[2]    foo}Then rrapply(my.data, FUN=cor.test2, STATS=c("estimate", "p.value", "ci1", "ci2")) should do what you want.rrapply <-function(x, FUN=t.test, STATS=c("statistic", "p.value"), ...){   # use FUN=cor.test, STATS=c("estimate", "p.val!
 ue") for matrix of correlations and p-values   K <- ncol(x)   RESU <- list()   my.fun <- FUN       fuf<-my.fun(x[,2],x[,1], ...)       if(!is.list(fuf)) {             STATS <- "A"             my.fun <- function(x,y,...) list(A=FUN(x,y,...))             }   neimz <- colnames(x)   for(i in 1:length(STATS)) {         fof <- STATS[i]         RESU[[fof]] <- matrix(NA, ncol=K, nrow=K)         colnames(RESU[[fof]]) <- neimz         rownames(RESU[[fof]]) <- neimz         names(RESU)[i] <- fof         }   for(i in 2:K) for(j in 1:(i-1)) {        foo<-my.fun(x[,i],x[,j], ...)          for(h in 1:length(STATS)) {               fof <- STATS[h]               RESU[[fof]][i,j]<- foo[[fof]]               }   }  if(is.list(fuf)) RESU else RESU$A}

On Fri, May 9, 2008 at 10:12 AM, Dimitris Rizopoulos <dimitris.rizopoulos@...> wrote:
have a look also at function rcor.test() from package ltm.Best,Dimitris----Dimitris RizopoulosBiostatistical CentreSchool of Public HealthCatholic University of LeuvenAddress: Kapucijnenvoer 35, Leuven, BelgiumTel: +32/(0)16/336899Fax: +32/(0)16/337015Web: http://med.kuleuven.be/biostat/   http://www.student.kuleuven.be/~m0390867/dimitris.htm----- Original Message ----- From: "Monica Pisica" <pisicandru@...>To: <r-help@...>Sent: Thursday, May 08, 2008 9:05 PMSubject: [R] applying cor.test to a (m, n) matrix



Hi everybody,I would like to apply cor.test to a matrix with m rows and n columns and get the results in a list of matrices , one matrix for p.val, one for the statistic, one for the correlation and 2 for upper and lower confidence intervals, something analog with cor() applied to a matrix.I have done my own function to get a matrix of p.values and i suppose i can build similar functions for all the others. But i have used for loops and i wonder if there is any way to actually use one of the functions from the "apply" family to do this in a quicker way.Here is my little function:cor.pval <- function(x, method = c("pearson", "kendal", "spearman"), digit=8) {n <- dim(x)[2]pval <- matrix(paste(rep("c", n*n), seq(1,n*n), sep = ""), n, n, byrow = T)for (i in 1:n) {for (j in 1:n){pval[i, j] <- cor.test(x[,i], x[,j], method = method)$p.value                                }                 }pval <- matrix(round(as.numeric(pval),digit), n, n, byrow = T)rownames(pval) <- colnames(x)c!
 olnames(pval) <- colnames(x)return(pval)}Thanks for any input,Monica_________________________________________________________________esh_messenger_052008______________________________________________R-help@... mailing listhttps://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm 


______________________________________________R-help@... mailing listhttps://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
_________________________________________________________________
With Windows Live for mobile, your contacts travel with you.

_mobile_052008
        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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.