Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么statmotsmodels.Logit P>|z |与scipy.stats pearsonr不同?它们都是p_值,但它们给了我不同的值_Python_Scipy_Statsmodels_P Value_Pearson - Fatal编程技术网

Python 为什么statmotsmodels.Logit P>|z |与scipy.stats pearsonr不同?它们都是p_值,但它们给了我不同的值

Python 为什么statmotsmodels.Logit P>|z |与scipy.stats pearsonr不同?它们都是p_值,但它们给了我不同的值,python,scipy,statsmodels,p-value,pearson,Python,Scipy,Statsmodels,P Value,Pearson,这是我的熊猫数据框和我的数据: 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.

这是我的熊猫数据框和我的数据:

        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_值:

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

几点相关的统计数据,你也可以查出来。皮尔森和线性回归只有当你与截距配合并考虑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,1,50),
                   'c2':np.random.uniform(0,1,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

你需要在personr函数上使用拦截器吗?我该怎么做?在我的例子中,我的Y变量是二进制的(两类),我该如何把你的思路应用到我的问题上?我是否仍应将其视为一个数字(OLS)而不是一个两类问题?(Logit)?嘿,你的问题是为什么p值不同,答案是因为你使用不同的回归。如果你想找出哪个预测因子与你的Y变量相关,那么你应该使用逻辑推理。希望这是清楚的