打印皮尔逊相关残差值

时间:2015-11-30 17:30:06

标签: python correlation chi-squared pearson

我能够计算两个列表之间的皮尔森相关性:

import scipy
from scipy import stats
from scipy.stats import pearsonr 
List1 = [1,2,3,4,5]
List2 = [2,3,4,5,6]
pearson = scipy.stats.pearsonr(List1,List2)
print "pearson correlation: " + str(pearson)

我想列出list1的观察值 - 期望值。有人知道如何扩展此代码以打印观察到的预期值吗?

鉴于此测试的说明: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.pearsonr.html 我不确定我是否可以使用这种方法获得这些值,或者其他更合适的东西?

编辑,我现在可以计算线性回归模型:

import sys
import scipy
from scipy import stats

List1 = [1,2,3,4,5]
List2 = [1,3,5,6,7]

slope, intercept, r_value, p_value, std_err = stats.linregress(List1,List2)

补充说明:

我假设一旦我按照亲切的建议拟合模型,我就会想出如何使用linregress包来拟合残差。

然而,当我打电话时:

>>> dir(stats.linregress)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']

我可以看到那里没有类似.resid / .residuals等的东西。有人能指出我正确的方向进行下一步吗?目的是计算list1的观察值 - 期望值列表(见上文?)

校正:

import scipy
from scipy import stats
x = [28,26,44,40,10,7,27,25,26,10]
y = [0.055,0.074,0.049,0.067,0.037,0.036,0.044,0.041,0.071,0.03]
print scipy.stats.linregress(x,y)

给我这个:

(0.00075454346398073121, 0.032064593825268217, 0.59378410770471368, 0.07031502216706445, 0.00036149633561360087)

我假设是残差。非常感谢。

******* ****** CORRECTION

0.00075454346398073121是斜率(m),0.032064593825268217是常数(c)。

To obtain the raw residuals for the first two items in lists x and y (see above) respectively, use y=mx+c:

list1 = [28,26]
X1 observed = 28; X1 predicted = 0.000754*28 + 0.03206 = 0.0531
X1 residual = 28 - 0.0531 = 27.94

X2 observed = 26; X2 predicted = 0.000764*26+0.03206 = 0.051664
X2 residual = 26 - 0.051664 = 25.94



list2 [0.055,0.074]
Y1 observed = 0.055; Y1 predicted = 0.000764*0.055+0.03206 = 0.032
Y1 residual = 0.055 - 0.032 = 0.022

Y2 observed = 0.074; Y2 predicted = 0.000764*0.074+0.03206 = 0.032
Y2 residual = 0.074 - 0.032 = 0.041.

非常感谢。

1 个答案:

答案 0 :(得分:0)

它返回一个元组,您可以解压缩:

corcoef, pval = scipy.stats.pearsonr(List1,List2)

您所追求的是线性回归:http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html#scipy.stats.linregress

然后,您可以使用该模型计算残差。