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
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 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. > >----- Yasir H. Kaheil Catchment Research Facility The University of Western Ontario -- View this message in context: http://www.nabble.com/applying-cor.test-to-a-%28m%2C-n%29-matrix-tp17134290p17142033.html Sent from the R help mailing list archive at Nabble.com.
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 at hotmail.com>
To: <r-help at r-project.org>
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 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm