What does a generalized linear model do?

What does a generalized linear model do? R supplies a modeling function called glm() that fits generalized linear models (abbreviated as GLMs). A natural question is what does it do and what problem is it solving for you? We work some examples and place generalized linear models in context with other techniques.For predicting a categorical outcome (such as y = true/false) it is often advised to use a form of GLM called a logistic regression instead of a standard linear regression. The obvious question is: what is does the logistic regression do? We will explain what problem the logistic regression is trying to solve.

Behind the scenes R encodes y=true as 1 and y=false as 0 (this is called encoding with an indicator). Then a standard linear regression is performed to try and predict these values. A linear model (in this case) is a formula of the form a + b*x1 + c*x2 = y. Standard least squares linear regression picks a,b and c to minimize the sum of (a + b*x1 + c*x2 – y)^2 over all of the training data. In general a standard result that this “minimum square loss model” linear regression is also (under fairly general conditions) a “maximum likelihood model” (the model that considers the training data least surprising).

For general data this has the slight disadvantage that the model can be penalized for being “too right”. Predicting “2” when the answer is “1” is considered (in terms of training error) to be just as bad as predicting “0” when the answer is “1.” Also it is well know that truely binomially distributed data is heteroscedastic because in a situation where our binomial outcome has probability p of coding to 1 the indicator has a variance of p*(1-p). Thus if p is varying over our data (which is one of the assumptions driving the attempt to build any sort of model) then we have the expected observations errors are a function of the observation values (which causes problems when fitting and when attempting to interpret coefficient significances). A first pass fix is to apply a stabilizing transform like sqrt() or arcsinh() ( see for example: “The transformation of Poisson, binomial and negative-binomial data”, FJ Anscombe, Biometrika, 1948 vol. 35 (3) pp. 246-254 ) which have the amazing property that they can make the observed variance nearly constant over a wide range of p (and therefore nearly independent of the expectation, as we want). However, for all of these corrections when fitting a linear model to a categorical outcome you are still overly dependent on the details of how you encoded that outcome as an indicator.

The next thing to try is a generalized linear model. A generalized linear model (in this case) fits s(a + b*x1 + c*x2) = y. For logistic regression we have s(z) = 1/(1+e^{-z}). This s(), called the sigmoid, has the following desirable properties:

s(-Infinity) = 0

s(Infinity) = 1

s(0) = 1/2

s(-x) = 1 – s(x)

x > y implies s(x) > s(y)

s'(x) = s(x) (1 – s(x))

So we certainly can no longer over-run our target y: s(x) ≤ 1 for all x, so we can’t ever be penalized for returning a prediction of 2. Thus all of the degrees of freedom or error budget can be used during fitting to try and address other (more important) fitting problems. To perform a logistic regression in R we just do the following:

m <- glm(y~x1+x2,data=d,family=binomial(link='logit'))
summary(m)

What this does is fit a maximum likelihood model to our data. That is a model that supplies probabilities for each datum and the product of all the predicted probabilities is least surprising (so the model tends to predict high values on y=true examples and low values on y=false examples). As discussed in the simpler derivation of logistic regression this is equivalent to finding the a,b and c such that maximize the product given by multiplying in a term of the form s(a + b*x1 + c*x2) for each positive example and multiplying in a term of the form (1 – s(a + b*x1 + c*x2)) for each negative example. Or in equations: the maximum likelihood solution picks a,b and c to maximize the following product over all training data:

That is: it picks the coefficients a,b and c so the training data is most consistent with the model (or least shocking given the model).

Notice this is not the same as minimizing the square loss (as we did for linear regression):

One great feature of linear regression that under fairly general conditions: the maximum likelihood solution is also the minimum square-loss solution. This breaks down for models of other forms (like logistic regression). We will continue our numeric example just to show the two solutions are not the same for logistic regression. The reason we work an example is statistics in general is hard to write about (as when you write about it you are managing many different concerns: data, model form and error). And statistics write-ups are even harder to read (as a lot of statistical writing claims to solve all problems at once which means authors tend not to admit to choices and trade-offs). So instead of trying to think through all possible intents we work a toy example (and given we run into complications in the toy example, you can be assured things are even more complicated once we expose techniques to real data).

Because many people (sometimes including this author) mis-remember GLMs as minimizing loss or transformed loss we continue with our worked example. Note that the balance equations (or the gradient of the logarithm of the likelihood equation, discussed in the simpler derivation of logistic regression) are obeyed in R’s solution:

However, the square-loss has its own gradient which implies its own balance equations. These new square loss balance equations are not obeyed. The new checks (which are just the derivative of the loss function with respect to each of the parameters a,b,c) are:

We can further confirm the optimal solution the square loss conditions is different than the maximum likelihood solution by going ahead and finding the square loss solution (so we can compare it to the maximum likelihood solution we already have). Define a function f() that computes the square-loss of given estimate:

which is a lower total square loss than our original 4.42. This confirms the two solutions (the one that maximizes likelihood and the one that minimizes square loss) are not the same for logistic regression.

The overall summary is: You can first try linear regression. If this is not appropriate for your problem you can then try pre-transforming your y-data (a log-like or logit transform) and seeing if that fits better. However, if you transform your y-data you are using a new error model (in the transformed space such as log(y)-units instead of y-units, this can be better or can be worse depending on your situation). If this error model is not appropriate you can move on to a generalized linear model. However, the generalized linear model does not minimize square error in y-units but maximizes data likelihood under the chosen model. The distinction is mostly technical and maximum likelihood is often a good objective (so you should be willing to give up your original square-loss objective). If you wan’t to go further still you can try a generalized additive model which in addition to re-shaping the y distribution uses splines to learn re-shapings of the x-data.

Comment Policy

All comments are held for moderation. Only comments that will be interesting to other readers will be considered for posting. Comments that are irrelevant, offensive or link-spam will be deleted. Also we do use a mechanical comment spam filter, and would like to apologize in advance for any comments that get lost to the filter.