NewUsr_stat
NewUsr_stat

Reputation: 2573

Uncentered Pearson correlation

I would like to compute the uncentered Pearson correlation using R.
The "Pearson" correlation that is one of the methods in cor function refers to centered Pearson correlation. Am I wrong?
Is there any way to compute the uncentered Pearson correlation?

Upvotes: 1

Views: 3797

Answers (4)

Ben Bolker
Ben Bolker

Reputation: 226162

It shouldn't be that hard to compute it yourself ... based on http://www.stanford.edu/~maureenh/quals/html/ml/node53.html (link now dead):

The uncentered version of Pearson correlation assumes that the mean of the population is zero. This is equivalent to calculating the cosine of the angle.

r_{xy} = 
\frac {\sum_{i=1}^n (x_{i}) (y_{i})} 
{(n-1) s_{x}^{(0)} s_{y}^{(0)} }

Where

s_{x}^{(0)}  =  \sqrt{ \frac{1}{n-1} \sum_{i=1}^n x_{i} ^2 }
set.seed(101)
x <- runif(100)
y <- runif(100)

n <- length(x)
stopifnot(length(y)==n)
sx0 <- sqrt(sum(x^2)/(n-1))
sy0 <- sqrt(sum(y^2)/(n-1))
(c1 <- sum(x*y)/((n-1)*sx0*sy0))  ## 0.7859549

Actually, I was following the formula listed there too closely -- the factors of n-1 cancel out, and it's even easier:

all.equal(c1,sum(x*y)/(sqrt(sum(x^2)*sum(y^2))))   ## TRUE

You could also try library("sos"); findFn("uncentered Pearson correlation") (but I didn't get any hits ...)

Upvotes: 3

sczinner
sczinner

Reputation: 53

This was asked a long time ago but just in case anyone comes across this, I would use the fact that

COV(X,Y)=E(XY)-E(X)E(Y). Hence, you can do,

Cov1<-cov(xx,yy)#the centered covariance
UCov1<-Cov1+mean(xx)*mean(yy)#the uncentered covariance
UCor1<-cov2cor(UCov1)#the uncentered correlation

For a data frame X you can do,

Covs<-cov(X)#the centered covariances
Means<-mean(X)#the means
UCovs<-Covs+Means%*%t(Means)#the uncentered covariances
UCors<-cov2cor(UCovs)#the uncentered correlations

Upvotes: 0

MixedModeler
MixedModeler

Reputation: 125

as I am currently facing the same issue, I was looking for a similar package recently and found the following one. It is called philentropy . In that package there is a function called lin.cor. When setting method = "pearson2" one should get Pearson's uncentred correlation coefficient. See following link for further explanation: https://www.rdocumentation.org/packages/philentropy/versions/0.5.0/topics/lin.cor

Upvotes: 1

user5377948
user5377948

Reputation: 1

def uncentered_corr_coeff(x, y):

import numpy as np

# find the lengths of the x and y vectors
x_length = len(x)
y_length = len(y)

# check to see if the vectors have the same length
if x_length is not y_length:
    print 'The vectors that you entered are not the same length'
    return False

# calculate the numerator and denominator
xy = 0
xx = 0
yy = 0
for i in range(x_length):
    xy = xy + x[i]*y[i]
    xx = xx + x[i]**2.0
    yy = yy + y[i]**2.0

# calculate the uncentered pearsons correlation coefficient
uxy = xy/np.sqrt(xx*yy)

return uxy

Upvotes: 0

Related Questions