[R] how to compute uncentered (pearson correlation) correlation efficiently

David Winsemius dwinsemius at comcast.net
Sat Mar 8 21:00:27 CET 2008


"Ng Stanley" <stanleyngkl at gmail.com> wrote:: 

> Seeking suggestions to compute uncentered (pearson correlation)
> correlation efficiently.
> 
> corr from stats library works on x and y columns. dist from amap
> library works on x and y rows.
> 
>  My data layout is slightly different such that row(i) of matrix x
>  is compared to row(i) of matrix y.

Do you mean cor()?

> ?corr
No documentation for 'corr' in specified packages and libraries:
you could try 'help.search("corr")'

I do not think that cor() will complain when you send it rows rather than 
columns.

r25 <- matrix(rnorm(25),ncol=5)
q25 <- matrix(rnorm(25),ncol=5)

> r25
           [,1]       [,2]       [,3]       [,4]       [,5]
[1,]  0.9075305  0.1768761  0.9946014 -2.1863247 -1.4031437
[2,] -0.6675117  0.5282182 -0.2522370 -0.3905784 -1.7219424
[3,]  0.7975418 -0.1992466  0.8884690  2.4123639 -0.9834216
[4,]  0.4027469  1.7962510 -0.1084113  0.7382887  0.2165000
[5,] -1.3969290  1.3095061  0.8976753  0.5161417 -2.0408553

> cor(q25[,1],r25[,1])
[1] 0.4221951
> cor(q25[1,],r25[1,])
[1] -0.4903181

-- 
David Winsemius



More information about the R-help mailing list