Numpy Python中cor.test的R的等价物
有没有办法在Python中找到r置信区间 在R中,我可以做如下操作:Numpy Python中cor.test的R的等价物,numpy,statistics,scipy,statsmodels,Numpy,Statistics,Scipy,Statsmodels,有没有办法在Python中找到r置信区间 在R中,我可以做如下操作: cor.test(m, h) Pearson's product-moment correlation data: m and h t = 0.8974, df = 4, p-value = 0.4202 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: -0.6022868 0
cor.test(m, h)
Pearson's product-moment correlation
data: m and h
t = 0.8974, df = 4, p-value = 0.4202
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.6022868 0.9164582
sample estimates:
cor
0.4093729
在Python中,我可以使用以下公式计算r cor:
r,p = scipy.stats.pearsonr(df.age, df.pets)
但这不会返回r置信区间。这里有一种计算内部置信区间的方法 首先得到皮尔逊的相关值
In [85]: from scipy import stats
In [86]: corr = stats.pearsonr(df['col1'], df['col2'])
In [87]: corr
Out[87]: (0.551178607008175, 0.0)
使用Fisher变换得到z
In [88]: z = np.arctanh(corr[0])
In [89]: z
Out[89]: 0.62007264620685021
西格玛值,即标准误差
In [90]: sigma = (1/((len(df.index)-3)**0.5))
In [91]: sigma
Out[91]: 0.013840913308956662
应用双边条件公式得到正态连续随机变量的正态95%区间概率密度函数
In [92]: cint = z + np.array([-1, 1]) * sigma * stats.norm.ppf((1+0.95)/2)
最后取双曲正切,得到95%的区间值
In [93]: np.tanh(cint)
Out[93]: array([ 0.53201034, 0.56978224])
谢谢,这就解决了。我想知道为什么以及statsmodels和/或scipy是否还没有提供此功能。我也很惊讶,或者我对堆栈的搜索不够好。有任何更新吗?理想情况下,应该有一个单线性scipy函数来计算,而不是您上面提供的八线性方法。