Com*_*ent 7 statistics numpy 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.9164582
sample estimates:
cor
0.4093729
Run Code Online (Sandbox Code Playgroud)
在Python中我可以使用以下公式计算r(cor):
r,p = scipy.stats.pearsonr(df.age, df.pets)
Run Code Online (Sandbox Code Playgroud)
但这不会返回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)
Run Code Online (Sandbox Code Playgroud)
使用Fisher变换得到z
In [88]: z = np.arctanh(corr[0])
In [89]: z
Out[89]: 0.62007264620685021
Run Code Online (Sandbox Code Playgroud)
并且,sigma值即标准误差
In [90]: sigma = (1/((len(df.index)-3)**0.5))
In [91]: sigma
Out[91]: 0.013840913308956662
Run Code Online (Sandbox Code Playgroud)
获得正常连续随机变量应用two-sided条件公式的正常95%区间概率密度函数
In [92]: cint = z + np.array([-1, 1]) * sigma * stats.norm.ppf((1+0.95)/2)
Run Code Online (Sandbox Code Playgroud)
最后取双曲正切以获得95%的区间值
In [93]: np.tanh(cint)
Out[93]: array([ 0.53201034, 0.56978224])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3349 次 |
| 最近记录: |