Statistical modeling : Generalised Linear model

Logistic models

Bongani Ncube(3002164)

2025-07-07

NoteGeneralized Linear Modeling

GLM theory suggests that the canonical link can be modeled as a linear combination of the explanatory variable(s). This approach unifies a number of modeling results used in statistics. By so doing we have now generalized our modeling to handle non-normal responses. In addition to normally distributed responses, we are able to handle Poisson responses, binomial responses, and more. Writing a pmf or pdf for a response in one-parameter exponential family form reveals the canonical link which can be modeled as a linear function of the predictors.

Exponential Family

The theory of GLMs is developed for data with distribution given y belongs to the exponential family.
The form of the data distribution that is useful for GLMs is

\[ f(y;\theta, \phi) = \exp(\frac{\theta y - b(\theta)}{a(\phi)} + c(y, \phi)) \]

where

  • \(\theta\) is called the natural parameter
  • \(\phi\) is called the dispersion parameter

Example

if we have \(Y \sim N(\mu, \sigma^2)\)

\[ \begin{aligned} f(y; \mu, \sigma^2) &= \frac{1}{(2\pi \sigma^2)^{1/2}}\exp(-\frac{1}{2\sigma^2}(y- \mu)^2) \\ &= \exp(-\frac{1}{2\sigma^2}(y^2 - 2y \mu +\mu^2)- \frac{1}{2}\log(2\pi \sigma^2)) \\ &= \exp(\frac{y \mu - \mu^2/2}{\sigma^2} - \frac{y^2}{2\sigma^2} - \frac{1}{2}\log(2\pi \sigma^2)) \\ &= \exp(\frac{\theta y - b(\theta)}{a(\phi)} + c(y , \phi)) \end{aligned} \]

where

  • \(\theta = \mu\)
  • \(b(\theta) = \frac{\mu^2}{2}\)
  • \(a(\phi) = \sigma^2 = \phi\)
  • \(c(y , \phi) = - \frac{1}{2}(\frac{y^2}{\phi}+\log(2\pi \sigma^2))\)

Properties of GLM exponential families

  1. \(E(Y) = b' (\theta)\) where \(b'(\theta) = \frac{\partial b(\theta)}{\partial \theta}\)

  2. \(var(Y) = a(\phi)b''(\theta)= a(\phi)V(\mu)\).

    • \(V(\mu)\) is the variance function; however, it is only the variance in the case that \(a(\phi) =1\)
  3. If \(a(), b(), c()\) are identifiable, we will derive expected value and variance of Y.

Examples

Normal distribution

\[ \begin{aligned} b'(\theta) &= \frac{\partial b(\mu^2/2)}{\partial \mu} = \mu \\ V(\mu) &= \frac{\partial^2 (\mu^2/2)}{\partial \mu^2} = 1 \\ &\to var(Y) = a(\phi) = \sigma^2 \end{aligned} \]

Poisson distribution

\[ \begin{aligned} f(y, \theta, \phi) &= \frac{\mu^y \exp(-\mu)}{y!} \\ &= \exp(y\log(\mu) - \mu - \log(y!)) \\ &= \exp(y\theta - \exp(\theta) - \log(y!)) \end{aligned} \]

where

  • \(\theta = \log(\mu)\)
  • \(a(\phi) = 1\)
  • \(b(\theta) = \exp(\theta)\)
  • \(c(y, \phi) = \log(y!)\)

Hence,

\[ \begin{aligned} E(Y) &= \frac{\partial b(\theta)}{\partial \theta} = \exp(\theta) = \mu \\ var(Y) &= \frac{\partial^2 b(\theta)}{\partial \theta^2} = \mu \end{aligned} \]

Since \(\mu = E(Y) = b'(\theta)\)

In GLM, we take some monotone function (typically nonlinear) of \(\mu\) to be linear in the set of covariates

\[ g(\mu) = g(b'(\theta)) = \mathbf{x'\beta} \]

Equivalently,

\[ \mu = g^{-1}(\mathbf{x'\beta}) \]

where \(g(.)\) is the link function since it links mean response (\(\mu = E(Y)\)) and a linear expression of the covariates

Important Logistic Regression

If \(X\) follows a binomial distribution with \(n\) trials and probability of success \(p\), we can write:

\[\begin{aligned} P(X=x)&= \binom{n}{x}p^x(1-p)^{(n-x)} \\ &=e^{x\log(p) + (n-x)\log(1-p) + \log\binom{n}{x}} \end{aligned}\] However, this probability mass function is not quite in one-parameter exponential family form. Note that there are two terms in the exponent which consist of a product of functions of \(x\) and \(p\). So more simplification is in order:

\[P(X=x) = e^{x\log\left(\frac{p}{1-p}\right) + n\log(1-p)+ \log\binom{n}{x}}\].

The one-parameter exponential family form for binomial responses shows that the canonical link is \(\log\left(\frac{p}{1-p}\right)\). Thus, GLM theory suggests that constructing a model using the logit, the log odds of a score, as a linear function of covariates is a reasonable approach.

\[ p_i = f(\mathbf{x}_i ; \beta) = \frac{exp(\mathbf{x_i'\beta})}{1 + exp(\mathbf{x_i'\beta})} \]

Equivalently,

\[ logit(p_i) = log(\frac{p_i}{1-p_i}) = \mathbf{x_i'\beta} \]

where \(\frac{p_i}{1-p_i}\)is the odds.

In this form, the model is specified such that a function of the mean response is linear. Hence, Generalized Linear Models

The likelihood function

\[L(p_i) = \prod_{i=1}^{n} p_i^{Y_i}(1-p_i)^{1-Y_i}\]

where \[p_i = \frac{\mathbf{exp(x'_i \beta})}{1+\mathbf{exp(x'_i \beta)}}\] and \(1-p_i = \frac{1}{1+ exp(\mathbf{x'_i \beta})}\)

Logistic Regression: Interpretation of \(\beta\)

For single regressor, the model is

\[ logit\{\hat{p}_{x_i}\} \equiv logit (\hat{p}_i) = \log(\frac{\hat{p}_i}{1 - \hat{p}_i}) = \hat{\beta}_0 + \hat{\beta}_1 x_i \]

When \(x= x_i + 1\)

\[ logit\{\hat{p}_{x_i +1}\} = \hat{\beta}_0 + \hat{\beta}(x_i + 1) = logit\{\hat{p}_{x_i}\} + \hat{\beta}_1 \]

Then,

\[ logit\{\hat{p}_{x_i +1}\} - logit\{\hat{p}_{x_i}\} = log\{odds[\hat{p}_{x_i +1}]\} - log\{odds[\hat{p}_{x_i}]\} \\ = log(\frac{odds[\hat{p}_{x_i + 1}]}{odds[\hat{p}_{x_i}]}) = \hat{\beta}_1 \]

and

\[ exp(\hat{\beta}_1) = \frac{odds[\hat{p}_{x_i + 1}]}{odds[\hat{p}_{x_i}]} \]

the estimated odds ratio

the estimated odds ratio, when there is a difference of c units in the regressor x, is \(exp(c\hat{\beta}_1)\). When there are multiple covariates, \(exp(\hat{\beta}_k)\) is the estimated odds ratio for the variable \(x_k\), assuming that all of the other variables are held constant.

Application in R

Data Dictionery

ImportantAbout the dataset
NoteWhy not linear Regression

Comments

  • The figure above illustrates a data set with a binary (0 or 1) response target (Y) and a single continuous predictor cholestrol (X). The solid line is a linear regression fit with least squares to model the probability of a success (Y=1 , presence of disease) for a given value of cholestrol X. With a binary response, the line doesn’t fit the data well, and it produces predicted probabilities below 0 and above 1. On the other hand, the logistic regression fit (dashed curve) with its typical “S” shape follows the data closely and always produces predicted probabilities between 0 and 1.

Fit a simple logistic regression model using age as the only risk factor of heart disease status

TipRecap

A logistic regression model is a generalized linear model and is made of a linear predictor:

\[\underbrace{g(\mu_i)}_{Link~~function} = \underbrace{\beta_0 + \beta_1X_1~+~...~+~\beta_pX_p}_{Linear~component}\]

Modeling in R

To fit a logistic model in R we use the function:

model <- glm(y~x, data, family = "binomial", ...)
summary(model)

Simple Logistic in R

Note
  • the equation below shows a simple logistic regression model fit on age as the predictor.

\[ \begin{aligned} \log(\frac{\mu_i}{1-\mu_i})= 2.943003-.0530036Age \end{aligned}\]

Report the estimated coefficient for age, and interpret its sign and meaning in the context of heart disease risk

NoteComments
  • the model for the output above can be written as follows

\[ \begin{aligned} \text{logit}(\mu_i) &= \log(\frac{\mu_i}{1-\mu_i})\\ &= 2.943003-.0530036Age \end{aligned}\]

  • The estimated coefficient for age is -0.0530036 and it is negative
  • Here we see that for a 1 year increase in age , the log odds of being diagnosed with heart disease decrease significantly by 0.0530036
  • Thus the chances of developing heart disease decrease with age

Is Age significantly associated with heart disease?

  • Age is indeed significantly associated with probability of developing heart disease at 5% level of significance since the corresponding p-value is 0.000 and is less than 0.05
  • the coefficient’s confidence is given by \([-0.0674686 ,-0.0385386]\) and does not contain zero hence the variable is significant.
NoteDetermining the odds

\[ \begin{aligned} \log(\frac{\mu_i}{1-\mu_i})&= 2.943003-.0530036Age\\ \frac{\mu_i}{1-\mu_i}&=e^{2.943003-.0530036Age}\\ \frac{\mu_i}{1-\mu_i}&=e^{2.943003}.e^{-.0530036Age} \end{aligned}\]

  • the odds associated with age is therefore \(e^{-.0530036}=0.9483766\) meaning that for a 1 year increase in age the odds of being diagnosed with heart disease decrease by \((1-0.9483766)*100\% \approx 5.16\%\)
Tip
  • also note that we can find the probability of being diagnosed at a given age by the following probability formula.

\[ \begin{aligned} \hat{p}(x) &= \frac{exp^{\hat{\beta}_{0} + \hat{\beta}_{1}{age}}}{1 + exp^{\hat{\beta}_{0} + \hat{\beta}_{1}{age}}}\\ &= \frac{exp^{2.943003-.0530036Age}}{1 + exp^{2.943003-.0530036Age}} \end{aligned}\]

likelihood ratio test in R

NoteComments
  • The likelihood ratio test above compares the null model with a model with age alone.
  • the \(p-value<0.001\) indicates that the model with age is better fit compared to a model with no covariates at all.
  • thus our current model fits the data better than a model with no predictors.

Add sex variable

Model with sex in R

NoteComments
  • the model for the output above can be written as follows

\[ \begin{aligned} \text{logit}(\mu_i) &= \log(\frac{\mu_i}{1-\mu_i})\\ &= 4.819817-.0674349Age-1.533812Male \end{aligned}\]

  • The estimated coefficient for age is -.0674349 and it is negative
  • Here we see that for a 1 year increase in age , the log odds of being diagnosed with heart disease decrease significantly by 0.0674349 while adjusting for gender of the patient.
  • whilst Adjusting for Age, males have 1.533812 less log odds of being diagnosed with heart disease as compared to females.
  • the two variables are significant at 5% level of significance since they have p-values less than 5%
Note Confidence Intervals for Model Coefficients

Since the Wald statistic follows a normal distribution with \(n\) large, we could generate a Wald-type (normal-based) confidence interval for \(\beta_2\) using:

\(\hat\beta_2 \pm 1.96\cdot\textrm{SE}(\hat\beta_2)\) and then exponentiating endpoints if we prefer a confidence interval for the odds ratio \(e^{\beta_2}\). In this case,

\[ \begin{align*} 95\% \textrm{ CI for } \beta_2 &= \hat{\beta}_2 \pm 1.96 \cdot \textrm{SE}(\hat{\beta}_2) \\ &= -1.533812 \pm 1.96 \cdot 0.158526 \\ &= -0.0132 \pm 0.00764 \\ &= (-1.223101, -1.844523) \\ 95\% \textrm{ CI for } e^{\beta_2} &= (e^{-1.223101}, e^{-1.844523}) \\ &= (0.2943161, 0.1581007) \\ \end{align*} \]

Thus, we can be 95% confident that being male is associated with a 84% to 70.7% decrease in the odds of dying after controlling age.

Back to top