Binaire data en logistische regressie

Generalized Linear Models in Python

Ita Cirovic Donev

Data Science Consultant

Binaire responsdata

  • Twee-klassenrespons $\rightarrow \large{\texttt{\color{#079EA1}{0},\color{#ED715F}{1}}}$

Voorbeelden:

  • Kredietscore $\rightarrow \texttt{\color{#ED715F}{"Default"}/\color{#079EA1}{"Non-Default"}}$
  • Slagen voor een toets $\rightarrow \texttt{\color{#079EA1}{"Pass"}/\color{#ED715F}{"Fail"}}$
  • Fraudedetectie $\rightarrow \texttt{\color{#ED715F}{"Fraud"}/\color{#079EA1}{"No-Fraud"}}$
  • Productkeuze $\rightarrow \texttt{\color{#2485F2}{"Product ABC"}/\color{#F2AC30}{"Product XYZ"}}$
Generalized Linear Models in Python

Binaire data

ONGEGROEPEERD

  • Enkele gebeurtenis
  • Eén munt gooien
  • Twee mogelijke uitkomsten: 0/1
  • $Bernoulli(p)$ of
  • $Binomial(n=1,p)$

GROEPEERD

  • Meerdere gebeurtenissen
  • Meerdere munten gooien
  • Aantal successen in $n$ pogingen
  • $Binomial(n,p)$
Generalized Linear Models in Python

Logistische functie

Spreidingsdiagram van studeeruren en de uitkomst slagen/zakken (0/1)

Generalized Linear Models in Python

Logistische functie

Spreidingsdiagram van studeeruren en de uitkomst slagen/zakken (0/1)

  • Toetsuitkomst: $PASS=1$ of $FAIL=0$

  • We willen modelleren

$P(y=1)=\beta_0 + \beta_1x_1$

$P(\text{Pass})=\beta_0 + \beta_1 \times \text{Uren studeren}$

Generalized Linear Models in Python

Logistische functie

Logistische fit voor studeeruren en de uitkomst slagen/zakken (0/1)

  • Toetsuitkomst: $PASS=1$ of $FAIL=0$

  • We willen modelleren

$P(y=1)=\beta_0 + \beta_1x_1$

$P(\text{Pass})=\beta_0 + \beta_1 \times \text{Uren studeren}$

  • Gebruik de logistische functie

$f(z) = \frac{1}{(1+\exp(-z))}$

Generalized Linear Models in Python

Odds en oddsratio

       

$$ ODDS = \frac{\text{gebeurtenis treedt op}}{\text{gebeurtenis treedt NIET op}} $$

       

$$ \text{ODDS RATIO} = \frac{odds 1}{odds 2} $$

Generalized Linear Models in Python

Voorbeeld: odds

  • 4 wedstrijden 3 overwinningen en 1 verlies als reeks

  • Odds zijn 3 tegen 1 Visuele berekening van odds met 3 winstvakken in de teller en 1 verliesvak in de noemer.

Generalized Linear Models in Python

Odds en kansen

  $$ \text{odds} \neq \text{kans} $$

  $$ \text{odds} = \frac{\text{kans}}{1-\text{kans}} $$

  $$ \text{kans} = \frac{\text{odds}}{1+\text{odds}} $$

Generalized Linear Models in Python

Van kansmodel naar logistische regressie

 

Stap 1. Kansmodel

$E(y)=\mu=P(y=1)=\beta_0 + \beta_1x_1$

 

Stap 2. Logistische functie

$f(z) = \large{\frac{1}{(1+\exp(-z))}}$

 

Stap 3. Pas logistische functie toe $\rightarrow$ INVERSE-LOGIT

$\mu = \large{\frac{1}{1+\exp(-(\beta_0+\beta_1x_1))}} = \large{\frac{\exp(\beta_0+\beta_1x_1)}{1+\exp(\beta_0+\beta_1x_1)}}$

$1-\mu = \large{\frac{1}{1+\exp(\beta_0+\beta_1x_1)}}$

Generalized Linear Models in Python

Van kansmodel naar logistische regressie

 

  • Kans $\rightarrow$ odds $$ ODDS=\frac{\mu}{1-\mu} = exp{(\beta_0+\beta_1x_1)} $$  
  • Logtransformatie $\rightarrow \color{#CF5383}{\text{LOGISTISCHE REGRESSIE}}$

  $$ LOGIT(\mu)=log(\frac{\mu}{1-\mu}) = \beta_0+\beta_1x_1 $$

Generalized Linear Models in Python

Logistische regressie in Python

Functie - glm()

model_GLM = glm(formula = 'y ~ x',                        
                data = my_data, 
                family = sm.families.Binomial()).fit

Input

y = [0,1,1,0,...]
y = ['No','Yes','Yes',...]
y = ['Fail','Pass','Pass',...]
Generalized Linear Models in Python

Laten we oefenen!

Generalized Linear Models in Python

Preparing Video For Download...