Generalized Linear Models in Python
Ita Cirovic Donev
Data Science Consultant
LINEAR MODEL
glm('y ~ weight',
data = crab,
family = sm.families.Gaussian())
$\mu = -0.14 + \color{#B21AB4}{0.32}*weight$
For every one-unit increase in weight
LOGIT MODEL
glm('y ~ weight',
data = crab,
family = sm.families.Binomial())
$log(odds) = -3.69 + \color{#228FF5}{1.8}*weight$
For every one-unit increase in weight
Logistic model $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1x_1 $$
Increase $x$ by one-unit $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1\color{blue}{(x_1+1)} $$
Logistic model $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1x_1 $$
Increase $x$ by one-unit $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1\color{blue}{(x_1+1)} = \beta_0 + \color{blue}{\beta_1x_1+\beta_1} $$
Take the exponential $$ (\frac{\mu}{1-\mu}) = \color{red}{\exp(\beta_0 + \beta_1x_1)}\color{blue}{\exp(\beta_1)} $$
Conclusion $\rightarrow$ the $\color{red}{\text{odds}}$ are multiplied by $\color{blue}{\exp(\beta_1)}$
Crab model y ~ weight
$$
log(\frac{\mu}{1-\mu}) = -3.6947 + \color{blue}{1.815}*weight
$$
The odds of satellite crab multiply by $\color{blue}{\exp(1.815) = 6.14}$ for a unit increase in weight
Crab model y ~ weight
$$
log(\frac{\mu}{1-\mu}) = \color{blue}{-3.6947} + 1.8151*weight
$$
The odds of satellite crab multiply by $\exp(1.8151) = 6.14$ for a unit increase in weight
# Choose x (weight) and extract model coefficients
x = 1.5
intercept, slope = model_GLM.params
# Compute estimated probability
est_prob = np.exp(intercept + slope * x)/(1 + np.exp(intercept + slope * x))
0.2744
# Compute incremental change in estimated probability given x
ic_prob = slope * est_prob * (1 - est_prob)
0.3614
$logit = -3.6947 + 1.8151*weight$
Generalized Linear Models in Python