我想用R来计算未被进入的Pearson相关性。
“皮尔逊”相关是cor
函数中的一种方法,它指的是中心皮尔逊相关。我说错了吗?
有任何方法来计算未被进入的Pearson相关性吗?
发布于 2014-05-27 14:03:50
你自己算应该不难.基于http://www.stanford.edu/~maureenh/quals/html/ml/node53.html (链接现在已死):
皮尔逊相关的未被记录的版本假设人口的平均值为零。这相当于计算角度的余弦。
r_{xy} =
\frac {\sum_{i=1}^n (x_{i}) (y_{i})}
{(n-1) s_{x}^{(0)} s_{y}^{(0)} }
哪里
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
实际上,我是非常密切地遵循这里列出的公式-- n-1
的因素抵消了,甚至更容易:
all.equal(c1,sum(x*y)/(sqrt(sum(x^2)*sum(y^2)))) ## TRUE
你也可以试试library("sos"); findFn("uncentered Pearson correlation")
(但我没有得到任何点击.)
发布于 2021-06-25 15:55:15
由于我目前正面临同样的问题,我正在寻找一个类似的软件包,最近发现了以下一个。它被称为philentropy
。在这个包中有一个名为lin.cor
的函数。当设置method = "pearson2"
时,应该得到Pearson的无中心相关系数。有关进一步解释,请参阅以下链接:https://www.rdocumentation.org/packages/philentropy/versions/0.5.0/topics/lin.cor
发布于 2015-09-25 21:59:35
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
https://stackoverflow.com/questions/23891391
复制相似问题