# Re: [R] Obtaining correlation parameters for multiple rows

From: jpnitya <joao_at_genetics.med.harvard.edu>
Date: Fri, 25 Apr 2008 13:03:46 -0700 (PDT)

Hi Jorge,

That was great! Thank you very much for the suggestion.

I just had to make a couple of minor changes (e.g., pearson vs. spearman) and it worked perfectly.

Thanks,
Joao Pedro

Hi,

I'm sure it could be better but try this:

# F statistics based on lm
FSTAT=function(y,x) summary(lm(y~x))\$f

# Correlation and p-value

CORR=function(y,x){
tc=cor.test(x,y,method="spearman",alternative="two.sided") temp=matrix(c(tc\$estimate,tc\$p.value),ncol=2) colnames(temp)=c('rho','pvalue')
temp
}

# 1000 variables and 100 samples
set.seed(124)
X=matrix(rnorm(1000*100),ncol=100)

# Correlation coefficient, p-value and F statistics
corr=t(apply(X[-1,],1,CORR,x=X[1,])) # Your reference is X[1,] fs=apply(X[-1,],1,FSTAT,x=X[1,]) # Your reference is X[1,]

# Report

temp=data.frame(fstats=fs,rho=corr[,1],pvalue=corr[,2]) rownames(temp)=paste("X",2:nrow(X),sep="") dim(temp)
 999 3

temp[1:10,]
fstats rho pvalue

```X2  1.421307790 -0.05038104 0.61807912
X3  0.051423768 -0.04614461 0.64795111
X4  0.128000634  0.01795380 0.85902211
X5  0.990235820 -0.06540654 0.51730942
X6  5.569006085  0.24232823 0.01532172
X7  0.001862766 -0.01436544 0.88703532
X8  1.025363077 -0.10628263 0.29206908
X9  0.679794149  0.06509451 0.51927479
X10 1.296034903  0.09492949 0.34686211
X11 0.126636867  0.05137714 0.61110106

```

HTH, Jorge

```--
View this message in context: http://www.nabble.com/Obtaining-correlation-parameters-for-multiple-rows-tp16851980p16904683.html
Sent from the R help mailing list archive at Nabble.com.

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