多变量逻辑回归

Python 中的广义线性模型

Ita Cirovic Donev

Data Science Consultant

多变量情形

  • 模型公式 $$ \text{logit}(y) = \beta_0+\beta_1\color{red}{x_1} $$
Python 中的广义线性模型

多变量情形

  • 模型公式 $$ \text{logit}(y) = \color{blue}{\beta_0}+\color{blue}{\beta_1}\color{red}{x_1} $$
Python 中的广义线性模型

多变量情形

  • 模型公式 $$ \text{logit}(y) = \beta_0+\beta_1x_1 + \beta_2\color{red}{x_2} + ... + \beta_p \color{red}{x_p} $$
Python 中的广义线性模型

多变量情形

  • 模型公式 $$ \text{logit}(y) = \beta_0+\beta_1x_1 + \color{blue}{\beta_2}\color{red}{x_2} + ... + \color{blue}{\beta_p}\color{red}{x_p} $$

  • 在 Python 中

    model = glm('y ~ x1 + x2 + x3 + x4', 
              data = my_data, 
              family = sm.families.Binomial()).fit()
    
Python 中的广义线性模型

示例——换井

formula = 'switch ~ distance100 + arsenic'
wells_fit = glm(formula = formula, data = wells, 
                family = sm.families.Binomial()).fit()
===============================================================================
                  coef    std err          z      P>|z|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept       0.0027      0.079      0.035      0.972      -0.153       0.158
distance100    -0.8966      0.104     -8.593      0.000      -1.101      -0.692
arsenic         0.4608      0.041     11.134      0.000       0.380       0.542
===============================================================================
Python 中的广义线性模型

示例——换井

                  coef    std err          z      P>|z|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept       0.0027      0.079      0.035      0.972      -0.153       0.158
distance100    -0.8966      0.104     -8.593      0.000      -1.101      -0.692
arsenic         0.4608      0.041     11.134      0.000       0.380       0.542
  • 两个系数均具有统计显著性
  • 系数符号合乎逻辑
  • distance100 增加 1,对数几率减少 0.89
  • arsenic 增加 1,对数几率增加 0.46
Python 中的广义线性模型

加入变量的影响

  • arsenic 的影响
  • distance100 从 -0.62 变为 -0.89
  • 离安全井越远
    • 砷含量越可能更高
                  coef    std err 
---------------------------------
Intercept       0.0027      0.079
distance100    -0.8966      0.104
arsenic         0.4608      0.041  
                  coef    std err
---------------------------------
Intercept       0.6060      0.060
distance100    -0.6291      0.097  
Python 中的广义线性模型

多重共线性

  • 与模型中其他变量存在相关性的变量

相关系数为 0.8、0.4、0、-0.4、-0.8 的两变量结构示意图。

  • 系数的标准误增大
    • 系数可能不再显著
1 https://en.wikipedia.org/wiki/Correlation_and_dependence
Python 中的广义线性模型

存在多重共线性吗?

应关注什么?

  • 系数不显著,但变量与 y 高度相关
  • 加/删一个变量使系数显著变化
  • 系数符号不合逻辑
  • 变量两两相关性高
Python 中的广义线性模型

方差膨胀因子(VIF)

  • 最常用的多重共线性诊断
    • 对每个自变量计算
    • 衡量系数方差被放大的程度
  • 建议阈值 VIF > 2.5
  • 在 Python 中
from statsmodels.stats.outliers_influence import variance_inflation_factor
Python 中的广义线性模型

Passons à la pratique !

Python 中的广义线性模型

Preparing Video For Download...