【发布时间】:2014-05-27 13:53:56
【问题描述】:
我想使用 R 计算未居中的 Pearson 相关性。
作为cor函数方法之一的“Pearson”相关是指居中的Pearson相关。我错了吗?
是否有任何软件包能够计算未居中的 Pearson 相关性?
最好的
E.
【问题讨论】:
标签: r statistics correlation
我想使用 R 计算未居中的 Pearson 相关性。
作为cor函数方法之一的“Pearson”相关是指居中的Pearson相关。我错了吗?
是否有任何软件包能够计算未居中的 Pearson 相关性?
最好的
E.
【问题讨论】:
标签: r statistics correlation
根据http://www.stanford.edu/~maureenh/quals/html/ml/node53.html 自己计算应该不会那么难,
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")(但我没有得到任何点击...)
【讨论】:
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
【讨论】:
由于我目前面临同样的问题,我最近正在寻找一个类似的包,并找到了以下一个。它被称为 philentropy 。在那个包中有一个名为lin.cor 的函数。当设置method = "pearson2" 时,应该得到Pearson 的非中心相关系数。更多解释见以下链接:https://www.rdocumentation.org/packages/philentropy/versions/0.5.0/topics/lin.cor
【讨论】: