Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

8.1 Logistic Regression

Before using logistic regression to model our data, we will attempt to do so through simple linear regression. While linear regression is not suitable for dichotomous outcomes, visualizing it can help illustrate why logistic regression is a better fit for our research question.

Why Not Linear Regression?

<Figure size 640x480 with 1 Axes>

As you can see, linear regression struggles with binary outcomes, as evidenced by predicted values exceeding 1 beyond approximately 80 months, which is invalid for probabilities. Since our dependent variable is dichotomous (e.g., pass/fail), we need a model that restricts predicted values to fall between 0 and 1, such as logistic regression.

Logistic Regression

Logistic regression naturally ensures that predicted probabilities stay between 0 and 1. In this tutorial, we will use the LogisticRegression() class from scikit-learn for modeling.

Intercept: [-2.83306013]
Coefficients: [[0.06612023]]

Interpreting the Model Outputs: Logits

The interpretation of the model coefficients is analogous to linear regression:

The output of a logistic regression model is linear in the log-odds (logits). Each coefficient in the logistic regression tells us how a one-unit change in a predictor affects the log-odds of the outcome. While not as intuitive as probabilities, the transformation to logits is crucial because it allows us to use linear regression techniques for binary outcomes.

But what even are logits?

Logits are the natural logarithm of the odds of an event occurring in logistic regression. The odds of an event are defined as the ratio of the probability of the event occurring (PP) to the probability of the event not occurring (1−P)(1-P):

Odds=P1−P\text{Odds} = \frac{P}{1-P}

In logistic regression, we predict the logit (log-odds) as a linear combination of the independent variables (X1,X2,…,Xk)(X_1, X_2, \dots, X_k) and their corresponding regression coefficients (β1,β2,…βk)(\beta_1, \beta_2, \dots \beta_k):

Logit(P)=ln⁡(P1−P)=β0+β1X1+β2X2+⋯+βkXk\text{Logit}(P) = \ln\left(\frac{P}{1-P}\right) = \beta_0 + \beta_1X_1 + \beta_2X_2 + \dots + \beta_kX_k

If we plot the equation, we can see how the regression line looks like:

<Figure size 640x480 with 1 Axes>

From Logits to Probabilities

We can simply transform the logits back into probabilities (more specifically the conditional probability of an observation y belongig to class 1 given predictor(s) X):

P(Y=1∣X)=11+e−(b0+b1X)P(Y=1 \mid X) = \frac{1}{1 + e^{-(b_0 + b_1 X)}}

To better understand the model’s behavior, let’s plot its outputs. A simple way to do this is by ceating an evenly spaced array of values for our range, and then use model.predict() to predict the outcome for each value. This will generate the regression line:

<Figure size 640x480 with 1 Axes>