March 15, 2015

# Logistic Regression – Meaningful Coefficients

**Why Logistic Regression**

Today I will talk about logistic regression, a method that allows us to use some of the useful features of linear regression to fit models that predict binary outcomes: vs. , “success” vs. “failure”, “result present” vs. “result absent”.

Just by itself linear regression is not adequate for this sort of thing as it doesn’t place any restrictions on the range of the response variable and we could end up with values far greater than or far below . We could discard anything we know about linear regression and use an altogether different approach to build a model that would, on the basis of a group of explanatory variables, predict our response variable. Indeed there are a number of such approaches: trees, forests, SVMs, etc. However, linear regression is such a simple but powerful technique with so many great things about it – it would be really good if we could somehow harness its power to build binary predictive models. And logistic regression is the method that allows us to do just that.

**What Logistic Regression Is and How It Works**

We are trying to build a model that would predict a binary outcome. In other words the model would accept as inputs a number of explanatory variables and would output the response variable that takes on values or . How can we combine our explanatory variables into an expression that would be simple (ideally a linear combination), produce a binary outcome, and allow for an intuitive interpretation of various components and coefficients within that expression?

First of all let’s note that we can be flexible and instead of requiring the outcome variable to strictly take on values or , allow it to take any value between and and then use some kind of a threshold such that whenever , we interpret it as and whenever we interpret it as . A convenient way to think of it is that becomes a *probability*, so that if it is close to we predict , and if close to we predict . In this case, a natural candidate for the threshold is .

Secondly let’s see how we can convert a linear combination of explanatory variables into a outcome.

To start, take a linear model based on our explanatory variables:

At this stage, depending on the values of and , this could produce any number arbitrarily large or small, positive of negative, i.e. in the range.

Now “squeeze” the range into the range by means of a logistic function:

This logistic function is nice because it is a very neat way to take our linear combination of explanatory variables and to transform it to essentially a probability value between and . Have a look at the formula and the graph:

- when , i.e. when goes into far negatives.
- when ,, i.e. when goes into large positives.
- exactly when , i.e. when , i.e crosses the -plane.

As a result we have a model that takes all our explanatory variables and by means of first a linear combination and then a logistic transformation maps these to a response variable that lies between and . We can them agree on a threshold, for example (although it doesn’t have to be and there are cases where the model is better off having a different threshold), and predict whenever the model gives an outcome , or predict when the model gives an outcome :

Ok so now we have some kind of a model that takes a bunch of explanatory variables in all shapes and forms and from that produces a probability, which can in turn be interpreted as a binary outcome. At this stage I am not saying anything about whether this model is good or bad, whether it predicts well, whether it is easy to fit, whether it has any nice statistical properties. All I can say for now is that it gets us from A to Z and it does that via a path that is fairly straightforward, intuitive and easy to interpret (well at least as straightforward as easy as this sort of thing can be).

**Fitting the Model**

Logistic regression models are fitted through the *maximum likelihood estimation (MLE)* method. This is a very popular way to fit models in general, not just logistic regression, for a very long list of reasons.

Often in statistics courses logistic regression is covered somewhere towards the end of the course, after the larger part of the course has been spent on OLS linear regression. As a result, a lot of students coming through the traditional path of statistical education may be inclined to have OLS fresh in their minds and be tempted to just use OLS instead of MLE to fit a logistic regression model. This, however, would not work. With logistic regression the concept of r*esidual sum of squares (RSS)* doesn’t really make sense – we can still compute something that would technically be , but because our responses have been “squeezed” into the range, the residuals are not meaningful and it doesn’t make sense to try to minimize them or their sum of squares. Thus, we use MLE instead.

The good news is that linear regression models too can be fit by means of MLE instead of OLS. Furthermore, as long some minor regularity conditions are satisfied, it turns out that fitting a linear regression model by MLE would yield exactly the same coefficient estimates as fitting the model via the conventional OLS method would. In fact, while it is correct to say that using MLE to fit a linear regression model gives results consistent with results of OLS, the statement would be a bit unfair. It would be more fair to say that it is OLS, the niche method for fitting linear regression models, that is consistent with MLE, the ubiquitous and versatile method for fitting all sorts of models including linear regression, logistic regression and others.

**Coefficient Interpretation**

One of the reasons logistic regression is popular is the fact that regression coefficients have a very intuitive interpretation.

*Logit Equals Log Odds*

The linear component is commonly referred to as *logit*. It can be interpreted as the *log of odds (log-odds)*. Recall that the *odds* are defined as the ratio of the probability of success to the probability of failure:

Continuing with our previous idea that the sigmoid function corresponds to probability that the outcome is or “success”, the odds can be expressed as:

And subsequently, substituting the full expression for , the log-odds are equal to the logit:

.

So for any set of explanatory variables , we can just plug them into the logit formula and quickly be able to say something like “the odds of the passenger surviving are ”.

*Individual Coefficients Equal to Log of Odds Ratio*

*Odds ratio (OR)* is just that – a ratio of the odds. We use it to test one explanatory variable’s effect on increasing or decreasing the odds (all other explanatory variables are adjusted for):

A good summary and a crash course in odds ratios can be found here.

It turns out that each individual coefficient in the logistic regression model can be interpreted as the log of odds ratio (log odds ratio). This is easiest to demonstrate with a *dummy explanatory variable.* Recall that a dummy explanatory variable is an explanatory variable that is binary, taking only values and , so for example something that represents gender (males are coded as , females as ), exposure to treatment (“received treatment” is coded as , “hasn’t received the treatment” is coded as ), and so on.

Suppose we have our usual logit:

.

where is a dummy variable taking only values and . Then whenever the odds are:

whereas whenever the odds are:

Thus, the odds ratio is:

and the logarithm of the odds ratio is .

From the argument above we see that the statement would hold for any dummy explanatory variable, not just and for any number of explanatory variables. We could also extend this line of thought to demonstrate that for non-dummy variables (ie. continuous numerical variables) the coefficient represents the log-odds ratio for a unit change in the explanatory variable.

If you are like me and come from pure mathematics and statistics background, you may find the concept of odds and odds ratios somewhat archaic – something they use when making bets in horse races. However, even though these concepts might be a exotic to you, they are very well ensconced in some of the applied disciplines like epidemiology or genetics that make very heavy use of binary classification. So for these fields it is nice to have something like logistic regression – on one hand it carries all the power of a proper statistical model, on the other hand it has nice intuitive interpretation of the logit and the coefficients that is consistent with the concepts of odds and odds ratio that anyone can just derive with a pen and paper and a simple 2×2 table.

**Evaluating the Model**

* – Not Right For Logistic Regression*

There is a “pseudo-” metric that often gets included in the standard output by statistical software when fitting a logistic regression model. There are actually several variants of these, you can see a comprehensive list here.

It would be nice to have something as simple to compute and easy to interpret as was for linear regression, but unfortunately none of these pseudo- metrics cut it. In the case of OLS linear regression, was based on the meaningful concept of and had all sorts of nice facts associated with it, such as for example the fact that (provided there is an intercept term) , or the fact that = (where is the Pearson correlation coefficient). In the case of logistic regression we have none of these niceties. The pseudo- is something that has been computed through an artificial formula that has been made to look like the real , but unfortunately it lacks most of the properties that make the real useful.

*Metrics Specific to Logistic Regression – Pearson’s -Test and Hosmer-Lemeshow Test*

Secondly, continuing on the topic of odds and odds ratios and 2×2 tables from the previous section, there is the Pearson’s -Test for categorical data that can be applied to our binary classification problem. There is also the Hosmer-Lemeshow Test.

The problem with these metrics, is similar to one of the problems that we had for in the case of linear regression. You will recall that in linear regression, was a great measure of goodness of fit by many standards but it had a few drawbacks, one being that it only allowed to compare linear regression models with linear regression models and not with other non-linear-regression models. Well, similarly here, both the Pearson -Test and the Hosmer-Lemeshow Test let us compare various logistic regression models to each other and to determine the better candidate, but may be not applicable to some of the other models that we may want to compare as alternatives to the logistic regression models.

*General Model Evaluation and Goodness Of Fit Measures*

And it is for this reason that it is best to use a very general goodness of fit measure, not just a metric that measures logistic regression models but is not applicable to other models. We have talked about model evaluation in another post. And we have concluded that the best ways to evaluate and compare models is to either use or statistics or to use cross-validation. We have noted that the preference between and is largely philosophical and that cross validation (with leave-one-out) and are asymptotically equivalent. So it would be these three that would be the best methods for evaluating our linear regression model – they would enable us to compare the model to all sorts of alternative models, not all of which have to be logistic regression models.

**Other Alternatives Compared**

As mentioned earlier, logistic regression is used for fitting models where the outcome is a binary response variable. There are however other methods one could use to predict binary models. Below are some examples of “competing” binary response models along with their strengths and weaknesses relative to logistic regression.

*Probit Regression*

We could use a method similar to logistic regression but where the “squeezing” function is not the logistic function but instead some other function with domain and range. For this purpose, any probability cumulative distribution function (CDF) will do. For example a natural candidate would be the CDF for normal distribution. This way we still have the linear relationship

but this time it is squeezed into range by means of the CDF for normal distribution. The new sigmoid curve would like like this when compared to the old logistic sigmoid curve:

Thus we have a model similar to logistic regression that we can just as well fit by means of MLE and grade by means of or cross validation. We call this model *probit regression*.

On one hand probit regression appears to be more intuitive – instead of using this exotic looking logistic function we use the very popular normal distribution CDF. The former usually gets taught at the very end of the first year university Calculus course only to never be seen again while the latter is the well-known and ubiquitous normal distribution that everyone is comfortable with. So it makes sense to use probit rather than logit right?

Not necessarily. Firstly, the fact the “squeezing” function is probit does not actually add any nice properties to our model – it does not make residuals normally distributed or introduce any normal distribution anywhere. We simply use normal distribution as a means of squeeing the range into but without making any assumptions about normal distribution anywhere and without obtaining normal distribution anywhere. On the other hand with logistic regression we actually do have a very nice and straightforward interpretation of logit (log odds) and the individual coefficients (log odds ratio) – and that is a major advantage over probit regression.

**Further Resources and Links: **

Logistic Regression using R: http://cran.r-project.org/web/packages/HSAUR/vignettes/Ch_logistic_regression_glm.pdf