微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么使用statmotsmodels.Logit P> | z |有别于scipy.stats pearsonr?它们都是p_values,但是它们给我不同的值

如何解决为什么使用statmotsmodels.Logit P> | z |有别于scipy.stats pearsonr?它们都是p_values,但是它们给我不同的值

这是我的熊猫数据框,其中包含我的数据:

        c0  c1  c10 c11 c12 c13 c14 c15 c16 c3  c4  c5  c6  c7  c8  c9
index                                                               
0   1   49  2.0 0   2   2   0   1   6797.761892 130 269.0   0   1   163 0   0.0
1   0   61  0.0 1   2   2   1   3   4307.686943 138 166.0   0   0   125 1   3.6
2   0   46  0.0 2   3   2   0   1   4118.077502 140 311.0   0   1   120 1   1.8
3   0   69  1.0 3   3   2   1   0   7170.849469 140 254.0   0   0   146 0   2.0
4   0   51  1.0 0   2   2   1   0   5579.040145 100 222.0   0   1   143 1   1.2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
283 0   54  0.0 1   2   2   2   0   6293.123474 125 273.0   0   0   152 0   0.5
284 0   42  0.0 0   3   2   0   1   3303.841931 120 240.0   1   1   194 0   0.8
285 1   67  0.0 2   2   2   1   0   3383.029119 106 223.0   0   1   142 0   0.3
286 0   67  1.0 2   3   2   0   2   768.900795  125 254.0   1   1   163 0   0.2
287 0   60  0.0 1   3   2   0   0   1508.832825 130 253.0   0   1   144 1   1.4
288 rows × 16 columns

我已经使用statsmodels获得p_value:

log = sm.Logit(df['c0'],df.loc[:,df.columns != 'c0']).fit()
d1 = pd.DataFrame(index=log.pvalues.index,data=log.pvalues,columns=['statsmodels_pvalue'])

然后我也使用了scipy模块。 personr函数返回相关性和pvalue,您将看到返回值[1]。

index = []
output = []
for i in df.columns[1:]:
    index.append(i)
    output.append(pearsonr(df['c0'],df[i]) [1])    
d2 = pd.DataFrame(index=index,data=output,columns=['pearson_pvalue'])

pd.concat([d1,d2],axis=1)

结果:

    statsmodels_pvalue  pearson_pvalue
c1  0.155704            0.105977
c10 0.449688            0.697069
c11 0.041694            0.038457
c12 0.000269            0.000510
c13 0.012123            0.046765
c14 0.000114            0.000087
c15 0.587200            0.843444
c16 0.301656            0.025142
c3  0.434319            0.330075
c4  0.000163            0.000014
c5  0.792058            0.613432
c6  0.340877            0.454607
c7  0.843758            0.562002
c8  0.365109            0.030531
c9  0.238975            0.070500

解决方法

与统计信息相关的几点,您也可以查看post like this。仅当您使用截距并考虑1个因变量时,皮尔逊和线性回归才等效。您正在执行多元回归,但不起作用。最后,您需要执行普通的最小二乘而不是逻辑回归。

下面将为这两个代码重现p值:

from scipy.stats import pearsonr
import pandas as pd
import numpy as np
import statsmodels.api as sm

np.random.seed(111)

df = pd.DataFrame({'c0':np.random.uniform(0,1,50),'c1':np.random.uniform(0,'c2':np.random.uniform(0,50)})

variables = df.columns[1:]
output = []
for i in variables:
    lm = sm.OLS(df['c0'],sm.add_constant(df.loc[:,i])).fit()
    lm_p = lm.pvalues[1]
    pearson_p = pearsonr(df['c0'],df[i]) [1]
    output.append([lm_p,pearson_p])    

pd.DataFrame(output,index=variables,columns=['lm_p','pearson_p'])

    lm_p    pearson_p
c1  0.062513    0.062513
c2  0.781529    0.781529

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。