Coefficients in terms of odds
Previously you have fitted a logistic regression model for the probability of switching the well given the arsenic
levels. In this exercise, you will see how another variable distance100
relates to the probability of switching and interpreting the coefficient values in terms of odds.
Recall that the logistic regression model is in terms of log odds, so to obtain by how much would the odds multiply given a unit increase in x
you would exponentiate the coefficient estimates. This is also called odds ratio.
Recall that odds are a ratio of event occurring to the event not occurring. For example, if the odds of winning a game are 1/2 or 1 to 2 (1:2), it means that for every one win there are 2 losses.
The dataset wells
is loaded in the workspace.
This exercise is part of the course
Generalized Linear Models in Python
Exercise instructions
- Import
statsmodels
library andglm
function fromstatsmodels.formula.api
. Also importnumpy
asnp
. - Using
glm()
fit a logistic regression model whereswitch
is predicted bydistance100
. - Extract model coefficients using
.params
. - Compute the multiplicative effect on the odds using
numpy
exp()
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load libraries and functions
import ____.api as sm
from ____.____.api import glm
import ____ as ____
# Fit logistic regression model
model_GLM = ____(formula = ____,
data = ____,
family = ____.____.____).____
# Extract model coefficients
print('Model coefficients: \n', ____.____)
# Compute the multiplicative effect on the odds
print('Odds: \n', np.____(____.____))