Generalized Linear Models and GLMs

Generalized Linear Models

Components of the Model

A generalized linear model describes the relationship between a response variable and a set of predictors. It has three components:

  • Random Component: Specifies the response variable YY and its distribution, conditional on predictors X<em>1,X</em>2,,Xp1X<em>1, X</em>2, …, X_{p-1}.
  • Systematic Component: Specifies the predictors in the linear predictor η=β<em>0+β</em>1X<em>1+β</em>2X<em>2++β</em>p1Xp1\eta = \beta<em>0 + \beta</em>1X<em>1 + \beta</em>2X<em>2 + \cdots + \beta</em>{p-1}X_{p-1}.
  • Link Function: \g(\cdot) specifies the relationship between the systematic component and the expected value of the random component.
    • Link functions are chosen based on the probability distribution of the random component.
    • The 'natural' link functions are called canonical link functions.

Canonical Link Functions

DistributionResponse RangeLink (η=g(μ))(\eta = g(\mu))μ=g1(η)\mu = g^{-1}(\eta)
Normal(,)(-\infty, \infty)Identity (μ)(\mu)η\eta
Poisson0,1,2,3,0, 1, 2, 3, \dotsLog (ln(μ))(\ln(\mu))eηe^{\eta}
Gamma(0,)(0, \infty)Inverse (μ1)(\mu^{-1})η1\eta^{-1}
Binomial1,2,3,,N1, 2, 3, \dots, NLogit (ln(μ1μ))(\ln(\frac{\mu}{1-\mu}))11+eη\frac{1}{1+e^{-\eta}}
ProbitΦ(μ)\Phi(\mu)Φ1(η)\Phi^{-1}(\eta)

Normal error regression model uses the identity link function: g(x)=xg(x) = x.

Y=β<em>0+β</em>1X<em>1++β</em>p1Xp1+ϵY = \beta<em>0 + \beta</em>1X<em>1 + \cdots + \beta</em>{p-1}X_{p-1} + \epsilon

E[Y]=β<em>0+β</em>1X<em>1++β</em>p1Xp1E[Y] = \beta<em>0 + \beta</em>1X<em>1 + \cdots + \beta</em>{p-1}X_{p-1}

Examples of Generalized Linear Models
  • Binomial Distribution: For a response YY with a binomial distribution with parameters nn and π=P(Y=1)\pi = P(Y = 1), the generalized linear model is given by:

    ln(π(X<em>1,,X</em>p1)1π(X<em>1,,X</em>p1))=β<em>0+β</em>1X<em>1++β</em>p1Xp1\ln\left(\frac{\pi(X<em>1, \dots, X</em>{p-1})}{1 - \pi(X<em>1, \dots, X</em>{p-1})}\right) = \beta<em>0 + \beta</em>1X<em>1 + \cdots + \beta</em>{p-1}X_{p-1}

    where π(X<em>1,,X</em>p1)=E[YX<em>1,,X</em>p1]\pi(X<em>1, \dots, X</em>{p-1}) = E[Y \mid X<em>1, \dots, X</em>{p-1}].

  • Poisson Distribution: For a response YY with a Poisson distribution with parameter λ\lambda, the generalized linear model is given by:

    ln(λ(X<em>1,,X</em>p1))=β<em>0+β</em>1X<em>1++β</em>p1Xp1\ln(\lambda(X<em>1, \dots, X</em>{p-1})) = \beta<em>0 + \beta</em>1X<em>1 + \cdots + \beta</em>{p-1}X_{p-1}

    where λ(X<em>1,,X</em>p1)=E[YX<em>1,,X</em>p1]\lambda(X<em>1, \dots, X</em>{p-1}) = E[Y \mid X<em>1, \dots, X</em>{p-1}].

Maximum Likelihood Estimators

  • Given a random sample Y<em>1,Y</em>2,,Y<em>nY<em>1, Y</em>2, \dots, Y<em>n with density functions f(y</em>iθ,X)f(y</em>i | \theta, X), dependent on a parameter θ\theta (possibly vector-valued) and predictor XX, the likelihood function is:

    L(θy<em>1,y</em>2,,y<em>n,X)=f(y</em>1θ,X)f(y<em>2θ,X)f(y</em>nθ,X)L(\theta | y<em>1, y</em>2, \dots, y<em>n, X) = f(y</em>1 | \theta, X)f(y<em>2 | \theta, X) \cdots f(y</em>n | \theta, X).

  • The log-likelihood function is:

    l(θy<em>1,y</em>2,,y<em>n,X)=ln(L(θy</em>1,y<em>2,,y</em>n,X))=lnf(y<em>1θ,X)+lnf(y</em>2θ,X)++lnf(y<em>nθ,X)=</em>i=1nlnf(yiθ,X)l(\theta | y<em>1, y</em>2, \dots, y<em>n, X) = \ln(L(\theta | y</em>1, y<em>2, \dots, y</em>n, X)) = \ln f(y<em>1 | \theta, X) + \ln f(y</em>2 | \theta, X) + \cdots + \ln f(y<em>n | \theta, X) = \sum</em>{i=1}^{n} \ln f(y_i | \theta, X).

  • The likelihood function of a random sample is a product of density functions.

  • The log-likelihood function of a random sample is the sum of the logs of density functions.

  • Response and predictor values are fixed (observed), and parameters are unknowns to be estimated.

Maximum Likelihood Estimate (MLE)
  • The MLE of a parameter θ\theta is the value that maximizes the likelihood of the observed data.

    L(mle θy<em>1,y</em>2,,y<em>n,X)=max</em>all possible θL(θy<em>1,y</em>2,,yn,X)L(\text{mle } \theta | y<em>1, y</em>2, \dots, y<em>n, X) = \max</em>{\text{all possible } \theta} L(\theta | y<em>1, y</em>2, \dots, y_n, X).

  • θ\theta can represent multiple parameters (e.g., mean and variance).

  • The MLE is usually computed using the log-likelihood function because the log function is increasing, so both maximize at the same place.

  • Theoretically, you try all possible values for θ\theta until you find the one that maximizes the (log)-likelihood function, subject to any constraints on the parameter. This is the MLE.

Example: Bernoulli Distribution
  • A random sample of size three is taken from a Bernoulli distribution with unknown proportion of successes π\pi. The observed values are one success and two failures.

    f(yπ)={πy(1π)1yamp;y=0,1 0amp;0 otherwise f(y \mid \pi) = \begin{cases} \pi^y(1 - \pi)^{1-y} &amp; y = 0, 1 \ 0 &amp; 0 \text{ otherwise } \end{cases}

    L(πy<em>1=1,y</em>2=0,y<em>3=0)=f(y</em>1=1π)f(y<em>2=0π)f(y</em>3=0π)=[π1(1π)11][π0(1π)10][π0(1π)10]=π(1π)2,0π1L(\pi \mid y<em>1 = 1, y</em>2 = 0, y<em>3 = 0) = f(y</em>1 = 1 \mid \pi)f(y<em>2 = 0 \mid \pi)f(y</em>3 = 0 \mid \pi) = [\pi^1(1 - \pi)^{1-1}][\pi^0(1 - \pi)^{1-0}][\pi^0(1 - \pi)^{1-0}] = \pi(1 - \pi)^2, \quad 0 \le \pi \le 1

    l(πy<em>1=1,y</em>2=0,y3=0)=ln(π)+2ln(1π)l(\pi \mid y<em>1 = 1, y</em>2 = 0, y_3 = 0) = \ln(\pi) + 2\ln(1 - \pi)

Likelihood Ratio Test (LRT)

  • Suppose M<em>0M<em>0 and M</em>1M</em>1 are two models where M<em>0M<em>0 is a restricted version of M</em>1M</em>1.

    M<em>0:g(μ)=β</em>0+β<em>1X</em>1+β<em>2X</em>2++β<em>kX</em>kM<em>0: g(\mu) = \beta</em>0 + \beta<em>1X</em>1 + \beta<em>2X</em>2 + \cdots + \beta<em>kX</em>k (restricted)

    M<em>1:g(μ)=β</em>0+β<em>1X</em>1+β<em>2X</em>2++β<em>kX</em>k+β<em>k+1X</em>k+1++β<em>p1X</em>p1M<em>1: g(\mu) = \beta</em>0 + \beta<em>1X</em>1 + \beta<em>2X</em>2 + \cdots + \beta<em>kX</em>k + \beta<em>{k+1}X</em>{k+1} + \cdots + \beta<em>{p-1}X</em>{p-1} (full)

  • Let lMl_M be the maximum log-likelihood of a model MM.

Example: Bernoulli Distribution (Continued)
  • Find the maximum log-likelihood function:

    l(πy<em>1=1,y</em>2=0,y3=0)=ln(π)+2ln(1π)l(\pi \mid y<em>1 = 1, y</em>2 = 0, y_3 = 0) = \ln(\pi) + 2\ln(1 - \pi)

    dldπ=1π21π=0\frac{dl}{d\pi} = \frac{1}{\pi} - \frac{2}{1 - \pi} = 0
    π1π=12\frac{\pi}{1 - \pi} = \frac{1}{2}
    mle π=13\text{mle } \pi = \frac{1}{3}
    l(mle πy<em>1=1,y</em>2=0,y3=0)=ln(13)+2ln(113)l(\text{mle } \pi \mid y<em>1 = 1, y</em>2 = 0, y_3 = 0) = \ln(\frac{1}{3}) + 2\ln(1 - \frac{1}{3})

  • The likelihood ratio test statistic is:

    LRT=2(l<em>M</em>0l<em>M</em>1)LRT = -2(l<em>{M</em>0} - l<em>{M</em>1}).

    • If n >>> 0, the LRT statistic has an approximate χ2\chi^2 distribution with kk degrees of freedom.
    • kk is the difference in the number of parameters between the two models.
  • The likelihood ratio test of:

    H<em>0:β</em>k+1==βp1=0H<em>0: \beta</em>{k+1} = \cdots = \beta_{p-1} = 0 (restricted)

    H1:at least one is not equal to 0H_1: \text{at least one is not equal to 0} (full)

  • Rejects the null hypothesis if:

    1. The test statistic LRTLRT is greater than the appropriate critical value: LRTχα,k2LRT \ge \chi^2_{\alpha, k}.
    2. The p-value is less than α\alpha: P(\chi^2_k \ge LRT) < \alpha.

Binary Data: Logistic Regression

  • Using a linear regression model with a binary response leads to fitting a line with an infinite range to the binary response.
  • Logistic regression fits a curve that estimates the probability of a response at various predictor values.
Challenger Space Shuttle Example
  • On January 28, 1986, the Challenger space shuttle disaster occurred 73 seconds into the flight, killing all seven crew members.
  • The investigation focused on O-rings; damage may be related to ambient temperature during the launch.
Odds of Success
  • Suppose Y<em>iY<em>i is a Bernoulli random variable with success probability π</em>i\pi</em>i. The odds of success are:

    o<em>i=π</em>i1πio<em>i = \frac{\pi</em>i}{1 - \pi_i}.

Binomial Distribution
  • A random variable YY has a binomial distribution with parameters nn (number of independent trials) and π\pi (success probability) if its probability mass function is:

    p(y) = \binom{n}{y} \pi^y (1 - \pi)^{n-y}, \quad y = 0, 1, \dots, n, \quad 0 < \pi < 1
    dbinom(y, size = n, prob = π)

  • Logistic Regression Model:

    ln(π(X<em>1,,X</em>p1)1π(X<em>1,,X</em>p1))=β<em>0+β</em>1X<em>1++β</em>p1Xp1\ln\left(\frac{\pi(X<em>1, \dots, X</em>{p-1})}{1 - \pi(X<em>1, \dots, X</em>{p-1})}\right) = \beta<em>0 + \beta</em>1X<em>1 + \cdots + \beta</em>{p-1}X_{p-1}

    π(X<em>1,,X</em>p1)=11+e(β<em>0+β</em>1X<em>1++β</em>p1Xp1)\pi(X<em>1, \dots, X</em>{p-1}) = \frac{1}{1 + e^{-(\beta<em>0 + \beta</em>1X<em>1 + \cdots + \beta</em>{p-1}X_{p-1})}}

    L(β<em>0,β</em>1,,β<em>p1Y</em>i,X<em>i1,,X</em>i,p1,i=1,2,,n)=<em>i=1n(11+e(β</em>0+β<em>1X</em>i1++β<em>p1X</em>i,p1))Y<em>i(111+e(β</em>0+β<em>1X</em>i1++β<em>p1X</em>i,p1))1YiL(\beta<em>0, \beta</em>1, \dots, \beta<em>{p-1} | Y</em>i, X<em>{i1}, \dots, X</em>{i,p-1}, i = 1, 2, \dots, n) = \prod<em>{i=1}^{n} \left( \frac{1}{1 + e^{-(\beta</em>0 + \beta<em>1X</em>{i1} + \cdots + \beta<em>{p-1}X</em>{i,p-1})}} \right)^{Y<em>i} \left(1 - \frac{1}{1 + e^{-(\beta</em>0 + \beta<em>1X</em>{i1} + \cdots + \beta<em>{p-1}X</em>{i,p-1})}} \right)^{1-Y_i}

Challenger Space Shuttle Example (Continued)
  • Partial Dataframe:

    head(Shuttle, n = 3)

      failed temp
    1      0   66
    2      1   70
    3      0   69
    
  • Fit Generalized Linear Model:

    shuttle.glm <- glm(failed ~ temp, data = Shuttle, family = binomial(link = "logit"))

  • Fit Intercept Only Model:

    shuttle.intercept.only <- glm(failed ~ 1, data = Shuttle, family = binomial(link = "logit"))

  • Likelihood Ratio Test:

    anova(shuttle.intercept.only, shuttle.glm, test = "LRT")

    Analysis of Deviance Table
    
    Model 1: failed ~ 1
    Model 2: failed ~ temp
      Resid. Df Resid. Dev Df Deviance Pr(>Chi)
    1        22     28.267                      
    2        21     20.315  1   7.952  0.004804 **
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
  • Summarize Results:

    sum.shuttle <- summary(shuttle.glm)

    Call:
    glm(formula = failed ~ temp, family = binomial(link = "logit"), 
        data = Shuttle)
    
    Coefficients:
                Estimate Std. Error z value Pr(>|z|)
    (Intercept)   15.0429     7.3786   2.039   0.0415 *
    temp         -0.2322     0.1082  -2.145   0.0320 *
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
    (Dispersion parameter for binomial family taken to be 1)
    Null deviance: 28.267  on 22  degrees of freedom
    
    Residual deviance: 20.315 on 21 degrees of freedom AIC: 24.315 Number of Fisher Scoring iterations: 5
  • Confidence Interval Parameter:

    confint(shuttle.glm)

                    2.5 %       97.5 %
    (Intercept)  3.3305848 34.34215133
    temp        -0.5154718 -0.06082076
    
  • Estimate Odds:

    predict(shuttle.glm, newdata = data.frame(temp = c(36, 60)), type = "link")

        1        2
    6.685043 1.113137
    
  • Estimate Probability:

    predict(shuttle.glm, newdata = data.frame(temp = c(36, 60)), type = "response")

            1         2
    0.9987521 0.7527135
    

Donner–Reed Party Example

  • The Donner–Reed Party was a group moving to California in 1846-47 who got stuck in the Sierra Nevada mountains during the winter.
  • Data on party members 15 years of age and older are in the DonnerShort dataset.
Partial Dataframe
  • head(Donner, n = 2)

      age gender survived
    1  23      1        0
    2  40      0        1
    
Fit Generalized Linear Model
  • donner.full <- glm(survived ~ age + gender, data = Donner, family = binomial(link = "logit"))
Fit Intercept Only Model
  • donner.intercept.only <- glm(survived ~ 1, data = Donner, family = binomial(link = "logit"))
Likelihood Ratio Test
  • anova(donner.intercept.only, donner.full, test = "LRT")

    Analysis of Deviance Table
    
    Model 1: survived ~ 1
    Model 2: survived ~ age + gender
      Resid. Df Resid. Dev Df Deviance Pr(>Chi)
    1        44     61.827                      
    2        42     51.256  2   10.57  0.005066 **
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
Summarize Results
  • sum.donner <- summary(donner.full)

    Call:
    glm(formula = survived ~ age + gender, family = binomial(link = "logit"), 
        data = Donner)
    
    Coefficients:
                Estimate Std. Error z value Pr(>|z|)
    (Intercept)   3.23041    1.38686   2.329   0.0198 *
    age          -0.07820    0.03728  -2.097   0.0359 *
    gender       -1.59729    0.75547  -2.114   0.0345 *
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
    (Dispersion parameter for binomial family taken to be 1)
    Null deviance: 61.827  on 44  degrees of freedom
    
    Residual deviance: 51.256 on 42 degrees of freedom AIC: 57.256 Number of Fisher Scoring iterations: 4
Confidence Interval Parameter
  • confint(donner.full, level = 0.95)

                    2.5 %       97.5 %
    (Intercept)  0.8514190 6.42669512
    age         -0.1624377 -0.01406576
    gender      -3.2286705 -0.19510198
    
Estimate Odds
  • new.data <- data.frame(age = c(50, 60), gender = c(1, 0))

  • predict(donner.full, newdata = new.data, type = "link")

            1          2
    -2.277083 -1.461831
    
Estimate Probability
  • predict(donner.full, newdata = new.data, type = "response")

            1          2
    0.09303878 0.18818751
    
Fit Generalized Linear Model - Age Only
  • donner.age.only <- glm(survived ~ age, data = Donner, family = binomial(link = "logit"))
Summarize Results
  • sum.age.only <- summary(donner.age.only)

    Call:
    glm(formula = survived ~ age, family = binomial(link = "logit"), 
        data = Donner)
    
    Coefficients:
                Estimate Std. Error z value Pr(>|z|)
    (Intercept)   1.81852    0.99937   1.820   0.0688 .
    age          -0.06647    0.03222  -2.063   0.0391 *
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
    (Dispersion parameter for binomial family taken to be 1)
    Null deviance: 61.827  on 44  degrees of freedom
    
    Residual deviance: 56.291 on 43 degrees of freedom AIC: 60.291 Number of Fisher Scoring iterations: 4
Likelihood Ratio Test - Age vs Full
  • anova(donner.age.only, donner.full, test = "LRT")

    Analysis of Deviance Table
    
    Model 1: survived ~ age
    Model 2: survived ~ age + gender
      Resid. Df Resid. Dev Df Deviance Pr(>Chi)
    1        43     56.291                      
    2        42     51.256  1   5.0344  0.02485 *
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
Likelihood Ratio Test - Age vs Intercept
  • anova(donner.intercept.only, donner.age.only, test = "LRT")

    Analysis of Deviance Table
    
    Model 1: survived ~ 1
    Model 2: survived ~ age
      Resid. Df Resid. Dev Df Deviance Pr(>Chi)
    1        44     61.827                      
    2        43     56.291  1   5.5358  0.01863 *
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    

Count Data: Poisson Regression

  • A random variable YY has a Poisson distribution with parameter λ\lambda if its probability mass function is:

    p(y)={eλλyy!amp;y=0,1,2,3, 0amp; otherwise p(y) = \begin{cases} \frac{e^{-\lambda}\lambda^y}{y!} &amp; y = 0, 1, 2, 3, \dots \ 0 &amp; \text{ otherwise } \end{cases}

    dpois(y, λ)

  • A Poisson distribution can be used when considering the number of occurrences of a specified event over some type of interval.

Horseshoe Crabs Example
  • A study of horseshoe crabs examined factors affecting whether a female crab has male crabs (satellites) nesting near her.

  • Variables measured: color, spine condition, carapace width, number of satellites, weight.

  • A Poisson random variable counts the number of events occurring in a fixed interval of time, where events occur at rate λ\lambda per interval and independently of the time since the previous event.

  • Poisson Regression Model:

    ln(λ(X<em>1,,X</em>p1))=β<em>0+β</em>1X<em>1+β</em>2X<em>2++β</em>p1Xp1\ln(\lambda(X<em>1, \dots, X</em>{p-1})) = \beta<em>0 + \beta</em>1X<em>1 + \beta</em>2X<em>2 + \cdots + \beta</em>{p-1}X_{p-1}

    λ(X<em>1,,X</em>p1)=eβ<em>0+β</em>1X<em>1++β</em>p1Xp1\lambda(X<em>1, \dots, X</em>{p-1}) = e^{\beta<em>0 + \beta</em>1X<em>1 + \cdots + \beta</em>{p-1}X_{p-1}}

    L(β<em>0,β</em>1,,β<em>p1Y</em>i,X<em>i1,,X</em>i,p1,i=1,2,,n)=<em>i=1neλ(X</em>i1,,X<em>i,p1)λ(X</em>i1,,X<em>i,p1)Y</em>iYi!L(\beta<em>0, \beta</em>1, \dots, \beta<em>{p-1} | Y</em>i, X<em>{i1}, \dots, X</em>{i,p-1}, i = 1, 2, \dots, n) = \prod<em>{i=1}^{n} \frac{e^{-\lambda(X</em>{i1}, \dots, X<em>{i,p-1})} \lambda(X</em>{i1}, \dots, X<em>{i,p-1})^{Y</em>i}}{Y_i!}

Horseshoe Crabs Example (Continued)
  • head(Crabs, n = 3)

      color spine width satellite weight y
    1     3     3  28.3         8   3050 1
    2     4     3  22.5         0   1550 0
    3     2     1  26.0         9   2300 1
    
Fit Generalized Linear Model
  • crab.glm <- glm(satellite ~ width, data = Crabs, family = poisson(link = "log"))
Fit Intercept Only Model
  • crab.intercept <- glm(satellite ~ 1, data = Crabs, family = poisson(link = "log"))
Likelihood Ratio Test
  • anova(crab.intercept, crab.glm, test = "LRT")

    Analysis of Deviance Table
    
    Model 1: satellite ~ 1
    Model 2: satellite ~ width
      Resid. Df Resid. Dev Df Deviance Pr(>Chi)
    1       172     632.79                      
    2       171     567.88  1   64.913  7.828e-16 ***
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
Summarize Results
  • summary(crab.glm)

    Call:
    glm(formula = satellite ~ width, family = poisson(link = "log"), 
        data = Crabs)
    
    Coefficients:
                Estimate Std. Error z value Pr(>|z|)
    (Intercept) -3.30476    0.54224  -6.095 1.1e-09 ***
    width        0.16405    0.01997   8.216  < 2e-16 ***
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    
    (Dispersion parameter for poisson family taken to be 1)
    Null deviance: 632.79  on 172  degrees of freedom
    
    Residual deviance: 567.88 on 171 degrees of freedom AIC: 927.18 Number of Fisher Scoring iterations: 6
Estimate log lambda
  • predict(crab.glm, newdata = data.frame(width = c(20, 30)), type = "link")

             1         2
    -0.0238555 1.6165954
    
Estimate lambda
  • predict(crab.glm, newdata = data.frame(width = c(20, 30)), type = "response")

            1         2
    0.9764268 5.0359157