Multivariabele logistische regressie

Generalized Linear Models in Python

Ita Cirovic Donev

Data Science Consultant

Multivariabele setting

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

Multivariabele setting

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

Multivariabele setting

  • Modelformule $$ \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

Multivariabele setting

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

  • In Python

    model = glm('y ~ x1 + x2 + x3 + x4', 
              data = my_data, 
              family = sm.families.Binomial()).fit()
    
Generalized Linear Models in Python

Voorbeeld - wisselen van put

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

Voorbeeld - wisselen van put

                  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
  • Beide coëfficiënten zijn statistisch significant
  • Tekens van de coëfficiënten zijn logisch
  • Een eenheidsverandering in distance100 geeft een negatieve verandering van 0,89 in de logit
  • Een eenheidsverandering in arsenic geeft een positieve verandering van 0,46 in de logit
Generalized Linear Models in Python

Effect van een variabele toevoegen

  • Impact van arsenic
  • distance100 verandert van -0,62 naar -0,89
  • Verder van de veilige put
    • Grotere kans op hogere arseenniveaus
                  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

Multicollineariteit

  • Variabelen die gecorreleerd zijn met andere modelvariabelen

Structuur van twee variabelen met correlaties 0,8 0,4 0, -0,4 en -0,8.

  • Grotere standaardfouten van coëfficiënten
    • Coëfficiënten mogelijk niet statistisch significant
1 https://en.wikipedia.org/wiki/Correlation_and_dependence
Generalized Linear Models in Python

Aanwezigheid van multicollineariteit?

Waar let je op?

  • Coëfficiënt niet significant, maar variabele sterk gecorreleerd met $y$
  • Toevoegen/verwijderen van een variabele verandert coëfficiënten sterk
  • Niet-logisch teken van de coëfficiënt
  • Variabelen hebben hoge paargewijze correlatie
Generalized Linear Models in Python

Variance inflation factor (VIF)

  • Meest gebruikte diagnose voor multicollineariteit
    • Berekend voor elke verklarende variabele
    • Hoeveel de variantie van de coëfficiënt is opgeblazen
  • Aanbevolen drempel VIF > 2,5
  • In Python
from statsmodels.stats.outliers_influence import variance_inflation_factor
Generalized Linear Models in Python

Laten we oefenen!

Generalized Linear Models in Python

Preparing Video For Download...