我能够计算两个列表之间的皮尔逊相关性:
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)
我假设是残差。非常感谢。
*******更正******
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.
非常感谢。