如何从Statsmodels的线性回归中提取P值(附代码示例)

856 阅读2分钟

你可以使用以下方法来提取使用Python中statsmodels模块拟合的线性回归模型中的系数的p值:

#extract p-values for all predictor variables
for x in range (0, 3):
    print(model.pvalues[x])

#extract p-value for specific predictor variable name
model.pvalues.loc['predictor1']

#extract p-value for specific predictor variable position
model.pvalues[0]

下面的例子展示了如何在实践中使用每种方法。

例子:从Statsmodels的线性回归中提取P值

假设我们有如下的pandas DataFrame,它包含了某个班级学生的学习时间、参加的预习考试和最后得到的分数等信息:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'hours': [1, 2, 2, 4, 2, 1, 5, 4, 2, 4, 4, 3, 6],
                   'exams': [1, 3, 3, 5, 2, 2, 1, 1, 0, 3, 4, 3, 2],
                   'score': [76, 78, 85, 88, 72, 69, 94, 94, 88, 92, 90, 75, 96]})

#view head of DataFrame
df.head()

	hours	exams	score
0	1	1	76
1	2	3	78
2	2	3	85
3	4	5	88
4	2	2	72

我们可以使用statsmodels模块中的OLS()函数来拟合一个多元线性回归模型,使用 "学时 "和 "考试 "作为预测变量,"分数 "作为响应变量

import statsmodels.api as sm

#define predictor and response variables
y = df['score']
x = df[['hours', 'exams']]

#add constant to predictor variables
x = sm.add_constant(x)

#fit linear regression model
model = sm.OLS(y, x).fit()

#view model summary
print(model.summary())

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                  score   R-squared:                       0.718
Model:                            OLS   Adj. R-squared:                  0.661
Method:                 Least Squares   F-statistic:                     12.70
Date:                Fri, 05 Aug 2022   Prob (F-statistic):            0.00180
Time:                        09:24:38   Log-Likelihood:                -38.618
No. Observations:                  13   AIC:                             83.24
Df Residuals:                      10   BIC:                             84.93
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         71.4048      4.001     17.847      0.000      62.490      80.319
hours          5.1275      1.018      5.038      0.001       2.860       7.395
exams         -1.2121      1.147     -1.057      0.315      -3.768       1.344
==============================================================================
Omnibus:                        1.103   Durbin-Watson:                   1.248
Prob(Omnibus):                  0.576   Jarque-Bera (JB):                0.803
Skew:                          -0.289   Prob(JB):                        0.669
Kurtosis:                       1.928   Cond. No.                         11.7
==============================================================================

默认情况下,summary()函数会显示每个预测变量的P值,最高为小数点后三位:

  • 截距的P值:0.000
  • 小时的P值。0.001
  • 考试的P值。0.315

然而,我们可以通过使用以下语法来提取模型中每个预测变量的完整p值:

#extract p-values for all predictor variables
for x in range (0, 3):
    print(model.pvalues[x])

6.514115622692573e-09
0.0005077783375870773
0.3154807854805659

这样我们就可以看到小数点后几位的p值:

  • 截距的P值:0.00000000651411562269257
  • 小时的P值:0.0005077783375870773
  • 考试的P值:0.3154807854805659

注意:我们在range()函数中使用了3,因为我们的回归模型中总共有三个系数。

我们还可以使用下面的语法来具体提取 "小时 "变量的p值:

#extract p-value for 'hours' only
model.pvalues.loc['hours']

0.0005077783375870773

或者我们可以使用下面的语法来提取回归模型中某一特定位置的变量的系数的p值:

#extract p-value for coefficient in index position 0
model.pvalues[0]

6.514115622692573e-09

其他资源

下面的教程解释了如何在Python中执行其他常见的任务:

如何在Python中进行Logistic回归
如何在Python中计算回归模型的AIC
如何在Python中计算调整后的R-Squared