多變量羅吉斯迴歸

Generalized Linear Models in Python

Ita Cirovic Donev

Data Science Consultant

多變量情境

  • 模型式 $$ \text{logit}(y) = \beta_0+\beta_1\color{red}{x_1} $$
Generalized Linear Models in Python

多變量情境

  • 模型式 $$ \text{logit}(y) = \color{blue}{\beta_0}+\color{blue}{\beta_1}\color{red}{x_1} $$
Generalized Linear Models in Python

多變量情境

  • 模型式 $$ \text{logit}(y) = \beta_0+\beta_1x_1 + \beta_2\color{red}{x_2} + ... + \beta_p \color{red}{x_p} $$
Generalized Linear Models in 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()
    
Generalized Linear Models in 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
===============================================================================
Generalized Linear Models in 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 單位,logit 減少 0.89
  • arsenic 增加 1 單位,logit 增加 0.46
Generalized Linear Models in 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  
Generalized Linear Models in Python

多重共線性

  • 與其他自變數「高度相關」的變數

相關係數為 0.8、0.4、0、-0.4、-0.8 的兩變數結構示意圖。

  • 係數的標準誤會增加
    • 係數可能不具統計顯著性
1 https://en.wikipedia.org/wiki/Correlation_and_dependence
Generalized Linear Models in Python

多重共線性是否存在?

要觀察什麼?

  • 係數不顯著,但變數與 $y$ 高度相關
  • 新增/移除變數會大幅改變係數
  • 係數的正負號不合理
  • 變數之間成對相關很高
Generalized Linear Models in Python

方差膨脹因子(VIF)

  • 最常用的多重共線性診斷
    • 對每個解釋變數計算
    • 衡量係數變異被放大的程度
  • 建議門檻 VIF > 2.5
  • 在 Python
from statsmodels.stats.outliers_influence import variance_inflation_factor
Generalized Linear Models in Python

一起來練習吧!

Generalized Linear Models in Python

Preparing Video For Download...