Binary Logistic Regression

Introduction

These notes will primarily focus on binary logistic regression. It is the most common type of logistic regression, and sets up the foundation for more complicated versions - ordinal and nominal logistic regression.

Regression is modeling the expected (mean/average) of the target variable conditional on the predictors:

\[ E(y_i | x_{1,i}, x_{2,i}, \ldots) \]

For a binary (0/1) target variable, \(y_i\), the expected value is just the probability of the event:

\[ E(y_i) = P(y_1 = 1) = p_i \]

This could lead us to thinking that we should model the following:

\[ p_i = \beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i} \]

That is called the linear probability model. This is not the best model to use to model probabilities as logistic regression models are more designed for modeling probabilities. However, for completeness sake however, let’s discuss the linear probability model.

Linear Probability Model

The linear probability model is not as widely used since probabilities do not tend to follow the properties of linearity in relation to their predictors. Also, the linear probability model possibly produces predictions outside of the bounds of 0 and 1 (where probabilities should be!).

Let’s first view what a linear probability model would look like plotted on our data and then we can build the model.

Even though it doesn’t appear to really look like our data, let’s fit this linear probability model anyway for completeness sake. Only the results are shown below.

OLS Regression Results
Dep. Variable: bonus R-squared: 0.312
Model: OLS Adj. R-squared: 0.312
Method: Least Squares F-statistic: 496.4
Date: Sat, 15 Nov 2025 Prob (F-statistic): 5.88e-91
Time: 10:54:40 Log-Likelihood: -579.24
No. Observations: 1095 AIC: 1162.
Df Residuals: 1093 BIC: 1172.
Df Model: 1
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
Intercept -0.3581 0.038 -9.540 0.000 -0.432 -0.284
GrLivArea 0.0005 2.32e-05 22.280 0.000 0.000 0.001
Omnibus: 1.803 Durbin-Watson: 2.173
Prob(Omnibus): 0.406 Jarque-Bera (JB): 1.759
Skew: -0.032 Prob(JB): 0.415
Kurtosis: 3.186 Cond. No. 4.88e+03


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 4.88e+03. This might indicate that there are
strong multicollinearity or other numerical problems.

As we can see from the charts above, the assumptions of ordinary least squares don’t really hold in this situation. Therefore, we should be careful interpreting the results of the model. Maybe a better model won’t have these problems?

Binary Logistic Regression Model

Due to the limitations of the linear probability model, people typically just use the binary logistic regression model. The logistic regression model does not have the limitations of the linear probability model. The outcome of the logistic regression model is the probability of getting a 1 in a binary variable, \(E(y_i) = P(y_i = 1) = p_i\). That probability is calculated as follows:

\[ p_i = \frac{1}{1+e^{-(\beta_0 + \beta_1x_{1,i} + \cdots + \beta_k x_{k,i})}} \]

This function has the desired properties for predicting probabilities. The predicted probability from the above equation will always be between 0 and 1. The parameter estimates do not enter the function linearly (this is a non-linear regression model), and the rate of change of the probability varies as the predictor variables vary as seen below.

(-0.05, 1.05)

To create a linear model, a link function is applied to the probabilities. The specific link function for logistic regression is called the logit function.

\[ logit(p_i) = \log(\frac{p_i}{1-p_i}) = \beta_0 + \beta_1x_{1,i} + \cdots + \beta_k x_{k,i} \]

The relationship between the predictor variables and the logits are linear in nature as the logits themselves are unbounded. This structure looks much more like our linear regression model structure. However, logistic regression does not use ordinary least squares (OLS) to estimate the coefficients in our model. OLS requires residuals which the logistic regression model does not provide. The target variable is binary in nature, but the predictions are probabilities. Therefore, we cannot calculate a traditional residual. The estimation method for logistic regression is discussed after we build our first model.

Let’s see how to run binary logistic regression in each software!

Coefficient Interpretations

One of the benefits of logistic regression is the interpretability of the model. The coefficients from the logistic regression model are interpretable, just not in the exact sense of linear regression. The coefficients in the logistic regression are the foundation of calculating odds ratios.

An odds ratio indicates how much more likely, with respect to odds, a certain event occurs in one group relative to its occurrence in another group. The odds of an event occurring is not the same as the probabilities that an event occurs, but related to the probability an event occurs:

\[ Odds = \frac{p}{1-p} \]

Notice how those odds form the foundation of the logit equation from before:

\[ logit(p_i) = \log(\frac{p_i}{1-p_i}) = \beta_0 + \beta_1x_{1,i} + \cdots + \beta_k x_{k,i} \]

The logit is the natural log of the odds of an event occurring. That means we could take the exponential of the logit function output and get the odds. However, if we were to take the ratio of two of these we would get the interpretable odds ratio. For example, let’s look at comparing the odds of a home with central air conditioning and without. We use the output from the logistic regression above to get the following equations:

\[ Odds_Y = e^{-11.36 + 4.87*CentralAir_Y + 0.0042*GrLivArea} = e^{-11.36 + 4.87(1) + 0.0042*GrLivArea} \]

\[ Odds_N = e^{-11.36 + 4.87*CentralAir_Y + 0.0042*GrLivArea} = e^{-11.36 + 4.87(0) + 0.0042*GrLivArea} \]

Let’s now take the ratio of those equations:

\[ OR = \frac{e^{-11.36 + 4.87(1) + 0.0042*GrLivArea}}{e^{-11.36 + 4.87(0) + 0.0042*GrLivArea}} \]

The intercept term as well as the GrLivArea variable all cancel themselves out leaving only the exponential function of the original coefficient on the CentralAir_Y variable:

\[ OR = \frac{e^{-11.36 + 4.87(1) + 0.0042*GrLivArea}}{e^{-11.36 + 4.87(0) + 0.0042*GrLivArea}} = \frac{e^{-11.36} e^{4.87(1)} e^{0.0042*GrLivArea}}{e^{-11.36} e^{0} e^{0.0042*GrLivArea}} = e^{4.87} = 130.37 \]

This gives the odds ratio. The interpretation on this ratio is that homes with central air are 130.37 times as likely to be bonus eligible than homes without central air, on average.

The same math can be done if we wanted to interpret the increase of a continuous variable by 1 unit. Let’s interpret the impact on odds of bonus eligibility when we increase the square footage by 1:

\[ OR = \frac{e^{-11.36 + 4.87*CentralAir_Y + 0.0042*(GrLivArea + 1)}}{e^{-11.36 + 4.87*CentralAir_Y + 0.0042*GrLivArea}} \]

\[ OR = \frac{e^{-11.36} e^{4.87*CentralAir_Y} e^{0.0042*GrLivArea} e^{0.0042}}{e^{-11.36} e^{4.87*CentralAir_Y} e^{0.0042*GrLivArea}} = e^{0.0042} = 1.0042 \]

The interpretation on this ratio is that every extra square footage on the home makes the odds of bonus eligibility 1.0042 times as likely to occur, on average. Due to the small scale of this increase, it makes it harder to interpret. Instead we could perform the following transformation:

\[ 100\times (OR -1)\% \]

Now our interpretation would be that every additional square foot of space expects to have 0.42% (= \(100 \times (e^{0.0042} - 1)\) ) higher odds to be bonus eligible. If this resulted in a negative number, it would be lower odds of bonus eligibility.

Just for fun, we can work through the math backwards to see what increase in square footage is needed for an expected doubling of the odds of a home being bonus eligible. The estimated amount to double the odds is the following:

\[ Double Odds = \frac{\log(2)}{\beta} = \frac{\log(2)}{0.0042} = 165.02 \]

Therefore, every additional increase of 165 square feet to the home doubles the odds of bonus eligibility, on average.

Let’s see how to calculate this in each software!

Estimation Method

As mentioned above, ordinary least squares is not a valid estimation method for logistic regression. Instead, logistic regression uses maximum likelihood estimation. Maximum likelihood estimation (MLE) is a very popular technique for estimating statistical models. It uses the assumed distribution (here logistic) to find the “most likely” values of the parameters to produce the data we see. In fact, it can be shown mathematically that the OLS solution in linear regression is the same as the MLE for linear regression. The likelihood function measures how probable a specific grid of \(\beta\) values is to have produced your data, so we want to maximize that function

The maximum likelihood function is based on the probability density function. For a binomial target variable, here is the likelihood function:

\[ L(\beta 's | y, x_1, x_2, \ldots ) = \prod_{i=1}^n p_i^{y_i} (1 - p_i)^{1-y_i} \]

The first piece of the likelihood function, \(p_i^{y_i}\), is the 1’s in our target variable and their respective probabilities. The second half of the function, \((1 - p_i)^{1 - y_i}\), is the 0’s in our target variable and their respective probabilities. If we use logistic regression in this likelihood function, the probabilities, \(p_i\) in the above equation, are represented with the logistic regression equation:

\[ p_i = \frac{1}{1+e^{-(\beta_0 + \beta_1x_{1,i} + \cdots + \beta_k x_{k,i})}} \]

Instead of the likelihood function, a lot of times people will estimate the natural log of the likelihood function to help make the computation easier:

\[ \log(L) = \sum_{i=1}^n{[y_i \log(p_i) + (1-y_i)\log(1-p_i)]} \]

A visual representation of maximizing this function is shown in the contour plot below. We try to find the maximum value of the likelihood function across a grid of possible \(\beta\) values.

<matplotlib.colorbar.Colorbar object at 0x32e545160>

The \(\beta\) values that maximize the likelihood function are the ones that most likely produced our data and will form our estimated logistic regression model.

Likelihood Ratio Tests

Likelihood estimation provides a basis for hypothesis testing in logistic regression. If extra predictors don’t add much value / information, then a model that includes them shouldn’t be substantially more likely than the model that doesn’t include them. The likelihood ratio test (LRT) compares these full models to these reduced models. A full model is the bigger of the two models you are comparing. A reduced model is the smaller, but nested, model you are comparing. A nested model is a special case of another model. In other words, a nested model has only some of the variables that the bigger model has, but no variables that the bigger model doesn’t have. The full model will always have the same variables of the nested model plus some additional variables - the ones we are testing.

To mathematically compare these two models we compare the differences in their maximized log likelihood values. The following test statistic is based off that difference:

\[ LRT = 2 \times (\log(L_F) - \log(L_R)) \]

This test statistic follows a \(\chi^2\) distribution.

Let’s see this in each software!

This test is also especially useful when you have categorical variables with more than 2 levels. If a categorical variable has more than 2 levels, then there are more than 2 dummy variables. If we want to test the validity of all of the dummy variables at once, the LRT is the perfect test.

Testing Assumptions

Outside of independence of observations, the biggest assumption of logistic regression is that the continuous predictor variables are linearly related to the logit function (our transformation of the probability of our target). A great way to check this assumption is through Generalized Additive Models (GAMs).

Generalized additive models (GAMs) can be used to help evaluate the linearity of the relationship between the continuous predictor variables and the logit. When GAMs are applied to a logistic regression, the following is the new model:

\[ \log(\frac{p_i}{1-p_i}) = \beta_0 + f_1(x_1) + \cdots + f_k(x_k) \]

These functions applied to the continuous predictor variables need to be estimated. GAMs use spline functions to estimate these. If these splines say a straight line is good, then the assumption is met. There are some options if the assumption is not met:

  1. Use the GAM representation of the logistic regression model instead of the traditional logistic regression model (even though this impacts interpretability).
  2. Strategically bin the continuous predictor variable.

In the first approach, instead of using the logistic regression model as previously defined, the logistic GAM would be used for predictions. The GAM version of the logistic regression is less interpretable in the traditional sense of odds ratios. Instead, plots are used to show potentially complicated relationships.

In the second approach, the continuous variables are categorized and put in the original logistic framework in their categorical form. There are statistical approaches to doing this, but one could also use the GAM plots to decide on possible splits for binning the data.

Let’s see how to produce GAMs in each software!

Predicted Values

Obviously, the predicted logit values really don’t help us too much. Instead we want to gather the predicted probabilities of the target variable categories. Luckily, the software we are looking at make this rather easy to do.

Let’s see how to produce these predicted probabilities in each software!