多変量ロジスティック回帰

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 の2変数の構造。

  • 係数の標準誤差が増大
    • 係数が統計的に有意でなくなることがある
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...