Generalized Linear Models Lecture Notes
Generalized Linear Models Lecture Notes
Contents
Preface 4
Introduction 5
Assumptions of the normal linear model . . . . . . . . . . . . . . . . . . . . . . . . . 5
Relaxing the assumptions for GLMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Why learn about GLMs? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Binomial models 15
2.1 Bernoulli distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Latent linear model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 Binomial outcomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.4 Scaled binomial distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.5 Implementation in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.6 Logistic regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.7 Tolerance distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2
CONTENTS 3
These notes are an adaptation from the original ST346 slides developed by Prof Martyn Plummer
for the module.
If you find any typos, please inform the module leader.
These materials are solely for your own use and you must not distribute these in any format.
Do not upload these materials to the internet or any filesharing sites nor provide
them to any third party or forum.
All rights are reserved.
4
Introduction
Y ∼ Nn (µ, σ 2 I n )
where for all i = 1, . . . , n:
• E(Yi | xi ) = µi with µi = xTi β;
• Var(Yi | xi ) = σ 2 ;
• Cov(Yi , Yj | xi , xj ) = 0 for i ̸= j;
• Yi | xi ∼ N (µi , σ 2 ) and so Yi is continuous and unbounded.
3. A variance function V (µ) allows the variance of the response to depend on the mean:
Var(Yi ) = ϕV (µi ).
Note:
a. We recover the normal linear model by using the normal distribution as the response
distribution and setting g(µ) = µ, V (µ) = 1 and ϕ = σ 2 .
b. Point 3. is a consequence of Point 1., that is by choosing a specific distribution from the
exponential family we determine the relationship between the mean and the variance of
the response.
5
Chapter 1 Weighted linear regression
Before we discuss GLMs, we introduce weighted linear models. These come into play when we
discuss how to produce maximum likelihood estimates for the parameters of a GLM.
First we consider the Gauss-Markov theorem which shows that the (ordinary) least squares
estimator for the parameters of a linear model is, in some sense, optimal.
We then consider a linear model where the variance is not constant but known up to a constant of
proportionality. We show that in this case, we can still estimate the parameter vector. However
to satisfy the same optimality criterion as in the standard case we need to adapt our estimator
to the generalised least squares estimator.
Yi = xTi β + ϵi for i = 1, . . . , n
where the errors ϵ1 , . . . , ϵn are uncorrelated, have mean zero and variance σ 2 .
The (ordinary) least squares estimator is
−1
β
b = XT X XT Y
and satisfies
E(β)
b = β;
−1
Var(β)
b = σ2 X T X .
1 if i = k,
(
δik =
0 if i =
̸ k,
then we may set ai = δik for given k so that γ = aT β = βk selects a single element of the
parameter vector.
Theorem 1.1 (Gauss-Markov theorem). Let β b be the least squares estimator for β. Then
γb = aT β
b is the unique unbiased linear estimator of γ = aT β with minimum variance.
6
1.1. THE GAUSS-MARKOV THEOREM 7
Because we can write γb as cT Y we have that γb is a linear combination of the response variables
Y1 , . . . , Yn and thus a linear estimator.
Next we show that γb is unbiased. As β
b is unbiased
E (γb ) = E aT β
b = aT E β
b = aT β = γ.
If γ̃ is unbiased, then this puts some constraints on the possible values of b, as we will show
next. For γ̃ to be unbiased we need E (γ̃) = γ = aT β and so bT Xβ = aT β for all values of
β. Hence
bT X = aT (1.1)
Step 3
We now calculate the variances of γb and γ̃.
Var (γ̃) = Var bT Y = bT Var (Y ) b = bT σ 2 I n b = σ 2 bT b.
W = diag(w1 , . . . , wn ).
Y = Xβ + ϵ, where ϵ ∼ Nn (0, ϕW −1 ).
Then
Then
h i−1
β
b = (X ∗ )T X ∗ (X ∗ )T Y ∗
Note
1. For a weighted linear model the weighted (or generalised) least squares estimator is the
unbiased linear estimator for β that has minimum variance. This generalisation of the
Gauss-Markov theorem is due to Aitken (1935).
2. Observations with a higher weight make a greater contribution to D(β). This is sensible as
an observation with higher weight has a smaller variance and so contains more information
about β. This explains why we divide the variance by the weight.
E(Yi ) = µ,
Var(Yi ) = ϕ/wi .
i=1 wi
i=1 wi
The diameter standard deviation tends to be larger for offspring of parents with large diameter.
We compare an ordinary least squares regression of Progeny on Parent against a weighted least
squares regression using SD2 as weights.
lm1 <- lm(Progeny ~ Parent, data = galtonpeas)
lm2 <- lm(Progeny ~ Parent, weights=1/SDˆ2, data=galtonpeas)
OLS
WLS
Progeny mean diameter
17.0
16.5
16.0
15 16 17 18 19 20 21
As we move from the ordinary least squares regression line to the weighted least squares
regression line we note a small shift due to observations with a small parent diameter being
given a larger weight.
Exercise 1
Consider an experiment in which it is known that the variance of the errors of the first two
observations is four times as large as the variance of the errors for the next three observations.
The errors are assumed to be independent. You are given the following model:
µ + ϵi i = 1 or i = 4,
Yi = µ + λ + ϵi i = 2 or i = 5,
µ + 2λ + ϵ3 i = 3.
Find the design matrix for this model and then calculate the generalised least squares estimator,
given that y T = (125, 62.5, 12.5, 100, 50).
Solution:
4 0 0 0 0 0 0 0 0
1
4
0 4 0 0 0 0 1
0 0 0
4
Var(ϵ) = σ 0
2
0 1 0 0 and so W = 0 0 1 0 0
.
0 0 0 1 0 0 0 0 1 0
0 0 0 0 1 0 0 0 0 1
1 0
1 1
14 13 209.375
" # " #
Furthermore, X = 2 1
and so X W X = 4 13 21 and X W y = 90.625 .
T 1 T
0 1
1 1
12 CHAPTER 1. WEIGHTED LINEAR REGRESSION
21 −13
" #
With (X W X)
T −1
= 4
it follows that
125 −13 14
Y
c = HY .
which is both symmetric and idempotent. This is the hat matrix on the scale of (X ∗ , Y ∗ ), that
is the matrix such that
c∗
Y = H ∗Y ∗.
The hat-value or leverage hi is the ith diagonal element of the hat matrix
−1
hi = wi xTi X T W X xi .
As we have seen in ST231, the sum of the hat values is equal to the number of parameters, that
is n
=
X
hi p.
i=1
1.4. WEIGHTED RESIDUALS 13
E(Ri | X) = 0,
E(Ri∗ | X) = 0,
Var(Ri∗ | X) = (1 − hi )ϕ.
The weighted residuals are an example of what we will later call Pearson residuals when we
look at GLMs.
Unlike for weighted residuals, the variance of the response residual depends on the weight.
Therefore, we do not know if a response residual is large or small unless we also know its weight.
In contrast for weighted residuals, if there are no observations with high leverage, then we have
approximately
Var(Ri∗ | X) ≈ ϕ.
This makes the weighted residual more useful for model diagnostics.
The weighted residuals retain all the useful properties we associate with residuals in linear
models.
√
• If the model has an intercept term, then ni=1 wi ri∗ = ni=1 wi ri = 0.
P P
• An unbiased estimate of the dispersion parameter ϕ is the sum of squares of the weighted
residuals, divided by the residual degrees of freedom n − p
1 X n
ϕb = (r∗ )2 .
n − p i=1 i
14 CHAPTER 1. WEIGHTED LINEAR REGRESSION
The standardized residual is the residual divided by an estimate of its standard deviation.
Both residuals lead to the same definition:
√
(s) wi (Yi − xTi β)
ri = q
(1 − hi )ϕb
Exercise 2
Show that the hat matrix on the scale of (X ∗ , Y ∗ ), that is the matrix such that
∗
Y
c = H ∗Y ∗
is given by −1
1 1
H∗ = W 2 X XT W X XT W 2 .
Solution
1 √ √ 1 1 1
Let W 2 = diag( w1 , . . . , wn ) and note that (W 2 )T = W 2 as W 2 is a diagonal matrix. Also
1
recall that we defined X ∗ = W 2 X where X is the design matrix on the original scale. Then,
using the expression for the hat matrix of an ordinary linear model, we obtain
−1
∗ T
T
H ∗
= X ∗
X X ∗
X∗
T −1
1 1 1 1
= W 2X W 2X W 2X W 2 X)T
−1
1 1
= W X X WX
2
T
XT W 2 .
Chapter 2 Binomial models
p(y) = µy (1 − µ)1−y .
The variance of the Bernoulli distribution depends on the mean µ as Var(Y ) = µ(1 − µ). We
write Y ∼ Bernoulli(µ) and Var(Y ) = V (µ). The variance function is depicted in the figure
below.
0.25
0.20
0.15
V(µ)
0.10
0.05
0.00
Note: Heteroscedasticity is built into the Bernoulli distribution as the variance is a quadratic
function in µ.
In linear models, we may try to resolve heteroscedasticity by transforming the outcome variable.
This will not work with the Bernoulli distribution.
• Any transformation of Y to a new variable (say Y ∗ ) will map all Y = 0 values to the same
value (say Y ∗ = a) and all Y = 1 values to the same value (say Y ∗ = b).
• The mean of the transformed variable is
µ∗ = E(Y ∗ ) = µ b + (1 − µ)a.
Var(Y ∗ ) = (µ∗ − a) (b − µ∗ ).
15
16 CHAPTER 2. BINOMIAL MODELS
2.2.1 Definition
We can describe a binary response using a normal linear model by introducing latent variables.
Latent variables are random variables that are not observed. Let Zi ∼ N (xTi β, 1) where
Z1 , . . . , Zn are independent. Then, set
1 if Zi ≥ 0,
(
Yi =
0 if Zi < 0.
2.2.2 Illustration
The figure below shows an iid random sample of Z ∼ N (xT β, 1) of size n = 30.
−2 xTβ 0
Now recall:
To illustrate this derivation we have taken the illustration of our sample and coloured observations
where yi = 1 as black circles, while the observations where yi = 0 are still depicted as white
circles.
2.2. LATENT LINEAR MODEL 17
Index (i)
y=1
y=0
−2 xTβ 0
y=1
y=0
Hence we need to use a probability model for Yi based on the latent value of Zi .
Consider the density of Zi ∼ N (xTi β, 1) shown in the figure below. The shaded tail area shows
0.35
density of Z
0.25
0.15
0.05 xTβ 0
We have
µi = P(Yi = 1) = P(Zi ≥ 0)
= P(N (0, 1) ≥ −xTi β)
= P(N (0, 1) ≤ xTi β)
= Φ(xTi β).
where Φ (“Phi”) is the cumulative distribution function of the standard normal distribution.
To avoid writing xTi β we introduce a new notation
ηi = xTi β
and new terminology. We call this quantity, represented by the Greek letter “eta”, the linear
predictor.
2.3. BINOMIAL OUTCOMES 19
The expectation µi of the outcome variable Yi is not the same as the linear predictor ηi = xTi β.
The two quantities are related by a smooth increasing function, namely Φ, as
µi = Φ(ηi ).
The mean function maps the linear predictor η onto the mean µ. The link function is the
inverse of the mean function. It maps the mean onto the linear predictor.
For the model considered here the link function is the inverse of Φ, that is
ηi = Φ−1 (µi ).
This link function is called the probit function. The name comes from an abbreviation of
“probability unit”.
The figure below on the left shows the mean function which, as discussed above, is given by the
cdf of a standard normal distribution. The figure below on the right shows the corresponding
link function, the so-called probit link function.
Mean function of the probit model Link function of the probit model
1.0
3
2
0.8
1
0.6
η
µ
0
0.4
−1
0.2
−2
0.0
−3
η µ
Exercise 3
Work through Sections 1 - 3 (inclusive) from Computer Practical 1.
Solution: See the solutions for Computer Practical 1 available on moodle.
i=1
Pm Pm
= µ z
i=1 i (1 − µ)m− z
i=1 i
= µy (1 − µ)m−y
where y =
Pm
i=1 zi .
Note: As the likelihood function depends on the data z1 , . . . , zm only through y, we have that
y is sufficient for µ.
The sum of m Bernoulli trials with common mean µ has a binomial distribution:
Y ∼ Binomial(m, µ)
where Y has probability mass function:
!
m
p(y) = µy (1 − µ)m−y for y ∈ {0, 1, . . . , m}.
y
Then
E(Y ) = mµ, and
Var(Y ) = mµ(1 − µ).
Then,
E(Y ∗ ) = µ, and
1 1
Var(Y ∗ ) = µ(1 − µ) = V (µ).
m m
for variance function V (µ) = µ(1 − µ).
2.5. IMPLEMENTATION IN R 21
2.5 Implementation in R
Let’s consider the turbines data from the GLMsData package.
We can fit the probit binomial GLM in two ways.
The first approach uses the proportion y/m as response and weights m:
glm.out1 <- glm(Fissures/Turbines ~ Hours, weights=Turbines,
family=binomial(link="probit"), data=turbines)
The second approach uses the vector (y, m − y) as the response without any weights:
turbines <- transform(turbines, NoFissures = Turbines - Fissures)
glm.out2 <- glm(cbind(Fissures, NoFissures) ~ Hours,
family=binomial(link="probit"), data=turbines)
We use the sumary() function from the faraway package to produce an abbreviated summary
of the two models.
library(faraway)
sumary(glm.out1)
n = 11 p = 2
Deviance = 9.81484 Null Deviance = 112.67005 (Difference = 102.85521)
sumary(glm.out2)
n = 11 p = 2
Deviance = 9.81484 Null Deviance = 112.67005 (Difference = 102.85521)
22 CHAPTER 2. BINOMIAL MODELS
The two commands result in the same fitted model, and thus the summary output reports the
same estimates, standard errors, deviance, etc.
Let’s explore the fitted model graphically. The fitted line is non-linear so we need to use
the predict() function with type = "response" to indicate that we would like to get the
predictions in the scale of the response.
plot(Fissures/Turbines ~ Hours, data=turbines, pch=16)
hvec <- seq(from=min(turbines$Hours), to=max(turbines$Hours), length=101)
ypred <- predict(glm.out1, newdata=list(Hours=hvec), type="response")
lines(hvec, ypred, lwd=2)
If we examine the fitted curve for very small and very large number of hours we observe
horizontal asymptotes at 0 and at 1 (beware though that in the example here this is a substantial
extrapolation!)
1.0
0.0 0.1 0.2 0.3 0.4 0.5 0.6
0.8
Fissures/Turbines
Fissures/Turbines
0.6
0.4
0.2
0.0
Hours Hours
The figure below shows the logistic function on the left and the logit link function on the right.
1.0
µ
logit(µ) = log( )
4
1−µ
0.8
2
0.6
η
µ
0
0.4
exp(η)
logistic(η) =
−2
1 + exp(η)
0.2
−4
0.0
A GLM for binary response data using a logistic mean function and logit link is known as
a logistic regression model. Before we discuss how to interpret the parameters of a logistic
regression model in terms of log odds ratios, we first introduce the concept of odds.
µ × 1 − (1 − µ) × L = 0.
Rearranging gives
µ
L = .
1−µ
These are the odds of E occurring.
Next let us consider how we make use of odds when interpreting the parameters of a logistic
regression model.
24 CHAPTER 2. BINOMIAL MODELS
µ(0)
The odds of disease (Yi = 1) in the unexposed (Xi = 0) is 1−µ(0)
.
µ(1)
The odds of disease (Yi = 1) in the exposed (Xi = 1) is 1−µ(1)
.
The odds ratio is given by
µ(1) 1 − µ(0)
Ω = ×
1 − µ(1) µ(0)
It follows that
µi
= exp α + βxi .
1 − µi
K
µi
log = α+
X
βk 1[Xi =k]
1 − µi k=2
where the original categorical variable has been replaced by K − 1 indicator variables.
• The reference category (k=1) is “absorbed” into the intercept α.
• For k ∈ {2, . . . , K}, the parameter βk is the log odds ratio for level k relative to level 1.
Xi = 1 Xi = 0 Total
Yi = 1 D(1) D(0) D
Yi = 0 H (1) H (0) H
Total N (1) N (0) N
Then
where
µ(1) 1 − µ(0)
Ω = ×
1 − µ(1) µ(0)
Ω = 1 if and only if µ(1) = µ(0) , so the risk is the same in both exposed and unexposed.
The maximum likelihood estimate for Ω is the cross-product ratio
D(1) H (0)
Ω
b = .
H (1) D(0)
We can use Bayes’ theorem to invert the conditional probabilities. For a, b ∈ {0, 1}
P(Yi = a)
P(Yi = a | Xi = b) = P(Xi = b | Yi = a) × .
P(Xi = b)
2.6. LOGISTIC REGRESSION 27
Due to the balance between numerator and denominator in the expression for Ω, the second
factor cancels out and so
P(Xi = 1 | Yi = 1) P(Xi = 0 | Yi = 0)
Ω = .
P(Xi = 1 | Yi = 0) P(Xi = 0 | Yi = 1)
µ(0) ≪ 1,
µ(1) ≪ 1.
Then
µ(1) (1 − µ(0) ) µ(1)
Ω = ≈
µ(0) (1 − µ(1) ) µ(0)
and so the odds ratio is approximately equal to the relative risk, or risk ratio.
Exercise 4
Work through Sections 4 - 5 from Computer Practical 1.
Solution: See the solutions for Computer Practical 1 on moodle.
28 CHAPTER 2. BINOMIAL MODELS
Zi = ηi + ϵi
where ϵi has a continuous, real valued distribution. The derived random variable Yi remains a
deterministic function of Zi given by
1 if Zi ≥ 0,
(
Yi =
0 if Zi < 0.
We have
µi = P(Yi = 1) = P(Zi ≥ 0)
= P(ηi + ϵi ≥ 0)
= P(ϵi ≥ −ηi )
= 1 − F (−ηi ).
µ = 1 − F (−η) = F (η)
normal
logistic
−4 −2 0 2 4
exp(x)
f (x) =
(1 + exp(x))2
exp(x)
F (x) = .
1 + exp(x)
Unlike the normal cdf, the logistic cdf can be written in closed form. This allows us to interpret
the parameters more easily.
A binary regression model using the logistic distribution as the tolerance distribution is called a
logistic regression model. It has mean function h : R → (0, 1) with
exp(η)
µ = h(η) = .
1 + exp(η)
0.3
0.2
0.1
0.0
−4 −2 0 2 4
normal
Cauchy
−4 −2 0 2 4
We note differences between the estimated coefficients, for example between the model using the
probit link and the one using the logit link. However, while the coefficients differ substantially,
the models with different link functions may still produce very similar predictions as can be
seen in the figure below. It shows a scatterplot of the data together with the predictions from
the various models.
probit
0.0 0.1 0.2 0.3 0.4 0.5 0.6
logit
cloglog
Fissures/Turbines
Hours
Exercise 5
1. For each of the tolerance distributions presented verify
a) F ′ (x) = f (x) (the derivative of its cdf is its pdf);
2.7. TOLERANCE DISTRIBUTIONS 33
b) g(h(η)) = η and h(g(µ)) = µ (the link function is the inverse of the mean function).
2. Repeat the experiment from Section [Link] but this time include the cauchit link function.
exp(x)
F ′ (x) = a′ exp(x) exp(x) = 2 = f (x).
1 + exp(x)
b) Recall that h(η) = exp(η)
1+exp(η)
and g(µ) = log µ
1−µ
.
Then, exp(g(µ)) = µ
1−µ
and so
exp g(µ) µ
1−µ
µ
1−µ
h g(µ) = = = = µ.
1 + 1−µ
µ 1
1 + exp g(µ) 1−µ
Also
h(η) exp(η)/ 1 + exp(η)
= = exp(η)
1 − h(η)
1/ 1 + exp(η)
and so
h(η)
g h(η) = log = log exp(η) = η.
1 − h(η)
34 CHAPTER 2. BINOMIAL MODELS
b) Recall that h(η) = 1 − exp − exp(η) and g(µ) = log − log(1 − µ) .
Now note that − exp g(µ) = log(1 − µ) and so
h g(µ) = 1 − exp − exp g(µ) = 1 − exp log(1 − µ) = µ.
Also log 1 − h(η) = − exp(η) and so
g h(η) = log − log 1 − h(η) = log exp(η) = η.
Cauchy distribution:
a) For the Cauchy distribution F (x) = 1
π
arctan(x) + 1
2
and f (x) = 1
π (1+x2 )
.
probit
0.0 0.1 0.2 0.3 0.4 0.5 0.6
logit
cloglog
Fissures/Turbines
cauchit
Hours
As previously seen, the predictions of the fitted models for the probit, logit and cloglog link
are all very similar. The fitted Binomial GLM using the cauchit link, however, gives different
36 CHAPTER 2. BINOMIAL MODELS
predictions. We note that it consistently over-predicts for runtimes of less than 2000 hours. For
example, the group of turbines that were run for 400 hours had no fissures, but the model gives
a predicted probability of fissures of roughly 8%.
Chapter 3 Poisson regression models
m1 = E(Y ),
m2 = E(Y 2 ),
m3 = E(Y 3 ),
and so on.
The moments of a distribution do not always exist. For example, for the t-distribution on d
degrees of freedom, mk does not exist for k ≥ d.
When all moments exist, they uniquely characterize the distribution, if Carleman’s condition is
satisfied: ∞
1
(m2k )− 2k = +∞.
X
k=1
Informally this condition ensures that the absolute value of moment mk does not grow too
quickly with k. For example, the lognormal distribution has moments
mk = exp(k 2 /2)
which grow rapidly with k. Thus the lognormal distribution does not satisfy Carleman’s
condition and is not uniquely determined by its moments.
If the moment generating function M (t) can be defined for t in some neighbourhood of zero,
then it uniquely characterizes the distribution. Recall the definition of the moment generating
function:
∞
tr Y r ∞
tr E (Y r ) ∞
tr mr
!
M (t) = E (exp(tY )) = = =
X X X
E
r=0 r! r=0 r! r=0 r!
By definition
M (0) = E exp(0) = 1.
dr M (0)
mr = .
dtr
37
38 CHAPTER 3. POISSON REGRESSION MODELS
The rth cumulant κr is defined as the rth derivative of the cumulant generating function
evaluated at t = 0:
dr K(0)
κr = .
dtr
The cumulants can be expressed in terms of moments up to the same order (you don’t need to
memorise these!):
κ1 = m1
κ2 = m2 − m21
κ3 = m3 − 2m1 m22 + 2m31
...
Note:
• The first 2 cumulants are well-known quantities: the mean and the variance.
• The 3rd cumulant is a measure of asymmetry, related to skewness:
κ3
Skewness = 3/2
.
κ2
• Higher order cumulants are harder to interpret.
Unlike moments, cumulants are additive for independent random variables.
If S = Yi for independent Y1 , . . . , Yn , then
Pn
i=1
n
KS (t) = KYi (t).
X
i=1
Proof:
h i h n i n
hY i
KS (t) = log E exp(tS) = log E exp(t Yi ) = log E exp(tYi )
X
i=1 i=1
n
Y h i n h i n
= log E exp(tYi ) = log E exp(tYi ) = KYi (t).
X X
It follows that the rth cumulant of S is the sum of the rth cumulants of Y1 , . . . , Yn :
dr KS (0) n
dr KYi (0)
=
X
.
dtr i=1 dtr
dr M (0)
mr = .
dtr
n
KS (t) = KYi (t).
X
i=1
dr KS (0) n
dr KYi (0)
=
X
.
dtr i=1 dtr
Exercise 6
Show that
dr M (0)
mr = ,
dtr
κ1 = m1 ,
κ2 = m2 − m21 .
Solution to Exercise 6:
40 CHAPTER 3. POISSON REGRESSION MODELS
∞
tk mk
M (t) = E (exp(tY )) =
X
.
k=0 k!
Therefore
dr M (t) ∞
tk−r
=
X
mk
dtr k=r (k − r)!
and so
dr M (0)
= mr .
dtr
Furthermore as K(t) = log (M (t)) we have
dK(t) 1 dM (t)
=
dt M (t) dt
and
d2 K(t) 1 d2 M (t) 1 dM (t) 2
= − .
dt2 M (t) dt2 M (t)2 dt
Hence, setting t = 0 and noting that M (0) = 1 we have
1
κ1 = K ′ (0) = M ′ (0) = m1
M (0)
and
1 1
2
κ2 = K ′′ (0) = M ′′ (0) − M ′ (0) = m2 − m21 .
M (0) M (0)2
Exercise 7
Work through Sections 1 - 3 of Computer Practical 2.
3.2. THE POISSON DISTRIBUTION 41
µ=1 µ=5
1500
3000
Frequency
Frequency
1000
2000
1000
500
0
0 2 4 6 0 5 10 15
y y
120
Sample Quantiles
300
Frequency
100
200
100
80
0
y Theoretical Quantiles
42 CHAPTER 3. POISSON REGRESSION MODELS
Proof:
∞
M (t) = E exp(tY ) = exp(tr) P(Y = r)
X
r=0
∞
µr exp(−µ) ∞
exp(−µ)
= exp(tr) = exp(t)r µr
X X
r=0 r! r=0 r!
h ir
∞ exp(t)µ
= exp(−µ) = exp(−µ) exp exp(t)µ
X
r=0 r!
h i
= exp exp(t)µ − µ = exp µ exp(t) − 1 .
K ′ (t) = µ exp(t),
K ′′ (t) = µ exp(t).
Therefore
500
number of hires
300
100
0
0 2 4 6 8 10 12 14 16 18 20 22
There is more variation at times of high demand than at times of low demand.
Yi ∼ Poisson(µi ).
Let S = Yi , then
Pn
i=1
S ∼ Poisson(µS )
where n
=
X
µS µi .
i=1
Proof:
The cumulant generating function of S is given by
n
KS (t) = KYi (t)
X
i=1
n h i
= µi exp(t) − 1
X
i=1
h n
iX
= exp(t) − 1 µi
i=1
h i
= exp(t) − 1 µS
For Poisson outcome data, we normally use a log link to construct a regression model:
log(µi ) = xTi β.
µi = exp xTi β > 0
for all β ∈ Rp .
Other link functions are possible, but they constrain the possible values of β.
T1 T2 T3 T4 T5
0
Time
The inter-event times have independent exponential distributions with common mean λ1 .
T1 T2 T3 C T4 T5
0
Time
Events occurring after the censoring time are not counted. Let Y be the number of events
occurring before time C then
Observations may have different censoring times. With different observation windows, we have
Yi ∼ Poisson(µi ) with µi = λi C i .
The expected event count will vary between observations, even if they have the same rate λi = λ.
3.4. POISSON REGRESSION FOR RATES 47
C1
0 Y1 = 1
C2
0 Y2 = 3
log(µi ) = log(λi Ci )
= log(λi ) + log(Ci )
If we assume
λi = exp(xTi β)
then
log(µi ) = xTi β + log(Ci ).
The term log(Ci ) is called an offset.
An offset is not the same as a predictor variable. If we put log(Ci ) in our model as a predictor,
then we get
log(µi ) = xTi β + γ log(Ci )
with a parameter γ to be estimated from the data. With an offset, we fix γ = 1.
The Poisson model with log link and an offset is used whenever we have a denominator Di such
that
E(Yi ) = Di exp(xTi β)
The denominator does not need to be time. It could be, for example
48 CHAPTER 3. POISSON REGRESSION MODELS
• spatial area,
• population size,
• volume.
m
= (Yi log(λ) − λCi ) + . . .
X
l(λ)
i=1
m
X m
X
= log(λ) Yi − λ Ci + . . .
i=1 i=1
where terms not depending on λ have been discarded. So the total event count Yi and the
Pm
i=1
sum of censoring times mi=1 Ci are sufficient for λ as
P
m m
X
Poisson λ
X
Yi ∼ Ci .
i=1 i=1
(See also the result on sums of independent Poisson random variables in Section 3.2.3!)
If all our predictor variables are categorical, we can aggregate the data. For each combination
of predictor variables, all we need is
• the total number of events Yi and
Pm
i=1
We apply Poisson regression with a log link and use log( Ci ) as an offset. The next section
Pm
i=1
discusses an example.
This is aggregated data. As we have 5 ship types, 4 different year of construction labels and 2
periods of operation, the data was aggregated over 5 × 4 × 2 = 40 groups. The number of ships
for each group was not recorded. However, the total services times (sum of censoring times)
and the total number of damage incidents (sum of events) are provided for each group and are
sufficient to fit a Poisson model for rates.
The variable service gives the aggregate months of service for each group in the data. However
as aggregating the data leads to groups where the period of operation occurs before the year of
construction, the ships in those groups have a service time of zero months. These groups are
excluded from further analysis.
The remaining groups have different total service times and thus different exposure to the risk
of a damage incident and so we are interested in the rate of damage incidents per month of
service time.
ships <- subset(ships, service > 0)
We use log(service) as an offset and fit a Poisson GLM with log-link. Here we are fitting a
null model, that is a model with no predictor variables.
[Link] <- glm(incidents ~ offset(log(service)),
family=poisson(link="log"), data=ships)
round(coef([Link]), 2)
(Intercept)
-6.13
round(exp(coef([Link])), 5)
(Intercept)
0.00218
Suppose we would like to examine the incident risks for different types of ships, then we may fit
the following Poisson GLM:
[Link] <- glm(incidents ~ 0 + type + offset(log(service)),
family=poisson(link="log"), data=ships)
round(exp(coef([Link])), 4)
typeA typeB typeC typeD typeE
0.0044 0.0018 0.0019 0.0038 0.0062
Exercise 8
Work through Sections 3 and 4 of Computer Practical 2.
Chapter 4 Exponential dispersion models
4.1 Motivation
Before we define the class of exponential dispersion models, we take a moment to understand
why they are needed. Consider a simple problem of estimating a common mean.
• Let Y1 , . . . , Yn be i.i.d. random variables with mean µ.
• For non-EDMs, the sample mean may still be a consistent and efficient estimator of µ.
50
4.2. DEFINITION OF AN EDM 51
θy − b(θ)
p(y | θ, ϕ) = a(y, ϕ) exp
ϕ
where
• θ ∈ Θ is the canonical parameter and Θ = {θ ∈ R : |b(θ)| < ∞},
• b(θ) is the cumulant function,
• ϕ ∈ R+ is the dispersion parameter. The dispersion parameter may be free, in which
case it is an additional parameter to be estimated, or it may be fixed to a known value
(usually ϕ = 1).
• a(y, ϕ) is the normalizing function. It ensures that
Z
p(y | θ, ϕ)dy = 1
y∈S
where S is the support (the permitted values of y). The normalizing function does not
depend on θ and plays no role in inference on θ.
Examples of EDMs include the following standard distributions:
• Normal (Gaussian),
• Binomial,
• Poisson,
• Gamma,
• Inverse Gaussian,
• Negative Binomial.
The support S of an EDM is determined by its normalizing function a(y, ϕ). Different EDMs
have different support, see the table below.
Distribution Support S
Normal R
Poisson N0
Scaled Binomial {0, m1 , m2 , . . . , 1}
Negative Binomial N0
Gamma R+
Inverse Gaussian R+
52 CHAPTER 4. EXPONENTIAL DISPERSION MODELS
where terms depending on the normalizing function a(y, ϕ/wi ) have been omitted.
An observation with weight wi ∈ N makes the same contribution to the log likelihood as wi
identical observations with weight 1.
4.4 Examples
4.4.1 Normal distribution
Recall the canonical form of the pdf/pmf of an EDM:
θy − b(θ)
p(y | θ, ϕ) = a(y, ϕ) exp .
ϕ
1 (y − µ)2
p(y | µ, σ 2 ) = √ exp −
2πσ 2 2σ 2
1 −y 2 + 2µy − µ2
= √ exp
2πσ 2 2σ 2
1 −y 2 µy − 12 µ2
= √ exp exp .
2πσ 2 2σ 2 σ2
θy − b(θ)
p(y | θ, ϕ) = a(y, ϕ) exp
ϕ
we deduce that
1 −y 2 θy − 12 θ2
p(y | µ, σ 2 ) = p(y | θ, ϕ) = √ exp exp
2πϕ 2ϕ ϕ
is a pdf in canonical form with
θ = µ
ϕ = σ2,
θ2
b(θ) = , and
2
1 −y 2
a(y, ϕ) = √ exp .
2πϕ 2ϕ
Therefore
exp(θ)
b(θ) = − log(1 − µ) = − log 1 −
1 + exp(θ)
1
= − log = log 1 + exp(θ) .
1 + exp(θ)
The scaled Binomial distribution is an example of an EDM where the dispersion parameter ϕ is
fixed at 1. Another such example is the Poisson distribution as the following exercise shows.
Note that the recommended textbook by Dunn and Smyth uses κ(θ) (kappa) for the cumulant
function, whereas we use b(θ).
4.5. CUMULANTS FOR EDMS 55
We can obtain the mean and variance of an EDM from the derivatives of its cumulant generating
function:
Hence
Therefore, the mean is independent of ϕ and the variance is proportional to ϕ (hence the name
“dispersion parameter”.)
Next we prove that for EDMs the cumulant generating function is given by
θy − b(θ)
p(y | θ, ϕ) = a(y, ϕ) exp .
ϕ
Then,
M (t) = E exp(tY )
Z
= exp(ty) p(y |, θ, ϕ)dy
y∈S
θy − b(θ)
Z
= a(y, ϕ) exp ty + dy
y∈S ϕ
b(θ∗ ) − b(θ) Z
= exp p(y | θ∗ , ϕ)dy
ϕ y∈S
b(θ∗ ) − b(θ)
= exp .
ϕ
b(θ) = exp(θ).
Hence
µ = b′ (θ) = exp(θ).
Solving for θ gives
θ = log(µ).
Therefore, the canonical link g() for the Poisson distribution is the log link, that is g(µ) = log(µ).
Solution:
Recall that θ ∈ Θ such that Θ = {θ ∈ R : |b(θ)| < ∞}. Note that in the examples above θ
has been restricted to lie on the negative real line. This is to ensure that |b(θ)| < ∞ for all
permissible θ.
Gamma distribution b(θ) = − log(−θ) and so b′ (θ) = − 1θ . Setting µ = b′ (θ) = − 1θ and solving
for θ we find θ = − µ1 and so the canonical link function of the gamma distribution is given by
1
g(µ) = − .
µ
Negative binomial distribution b(θ) = −k log 1 − exp(θ) and so b′ (θ) = k exp(θ)
1−exp(θ)
. Setting
µ = b′ (θ) and solving for θ we find
µ
θ = log
k+µ
and so the canonical link for the negative binomial distribution is g(µ) = log k+µ
µ
.
√
Inverse Gaussian distribution b(θ) = − −2θ and so b′ (θ) = √−2θ 1
. Setting µ = b′ (θ) and
solving for θ we find θ = − 2µ1 2 and so the canonical link function for the inverse Gaussian
distribution is g(µ) = − 2µ1 2 .
4.8 Deviance
In ST231 we derived our parameter estimates by minimizing the residual sum of squares function,
or deviance. n
= (yi − µi )2 .
X
D(y, µ)
i=1
(y − µ)2
p(y | µ, σ ) 2
∝ exp − ,
2σ 2
4.8. DEVIANCE 59
the likelihood
i=1 (yi − µi )2
Pn
p(y | µ, σ )
2
∝ exp −
2σ 2
is maximized when the deviance is minimized.
In week 1 of ST346 we extended this to weighted models:
n
= wi (yi − µi )2 .
X
D(y, µ)
i=1
The concept of deviance arises naturally from EDMs, but different EDMs will have different
formulae for the deviance. The one-to-one correspondence between θ and µ implies that we can
re-write the density function of an EDM in terms of µ, ϕ instead of θ, ϕ. Let
t(y, µ) = θy − b(θ),
θy − b(θ)
p(y | θ, ϕ) = a(y, ϕ) exp
ϕ
t(y, µ)
= a(y, ϕ) exp
ϕ
where a∗ (y, ϕ) = a(y, ϕ) exp t(y, y)/ϕ .
Setting
d(y, µ) = 2 t(y, y) − t(y, µ) .
d(y, µ)
p(y | µ, ϕ) = a (y, ϕ) exp
∗
−
2ϕ
Proposition 4.1 (Unit deviance). The unit deviance d(y, µ) is non-negative and exactly zero if
and only if µ = y. It is thus is a measure of the discrepancy between the expected value µ and
the observed value y.
60 CHAPTER 4. EXPONENTIAL DISPERSION MODELS
Proof
If t(y, µ) has a unique maximum at µ = y, then the unit deviance
d(y, µ) = 2 t(y, y) − t(y, µ)
dt
= y − b′ (θ) = y − µ
dθ
and so
dt
= 0 ⇐⇒ µ = y.
dθ
Moreover
d2 t
= −b′′ (θ) = −Var(Y )/ϕ < 0.
dθ2
t(y, µ) = yθ − b(θ)
= yθ − θ2 /2
= yµ − µ2 /2.
This is maximised at
t(y, y) = y 2 − y 2 /2 = y 2 /2.
= y 2 − 2yµ + µ2
= (y − µ)2 .
4.8. DEVIANCE 61
Bounded above:
d(y, µ) = lim 2 t(y, y − ϵ) − t(y, µ) = 2 lim t(y, y − ϵ) − t(y, µ) where ϵ > 0.
ϵ→0+ ϵ→0+
So the unit deviance depends partly on the ratio y/µ and partly on the difference y − µ.
Second case: if y = 0, then
d(0, µ) = lim 2 t(0, ϵ) − t(0, µ)
ϵ→0+
= lim 2 0 × log(ϵ) − ϵ − 0 × log(µ) + µ)
ϵ→0+
= 2µ.
62 CHAPTER 4. EXPONENTIAL DISPERSION MODELS
Therefore
t(y, µ) = yθ − b(θ)
!
µ
= y log + log(1 − µ)
1−µ
= y log(µ) + (1 − y) log(1 − µ).
Case 2: y = 0.
For a scaled binomial distribution we have 0 < µ < 1. Hence, for y = 0, we need to replace
t(y, y) with limϵ→0+ t(y, y + ϵ).
We have
lim t(0, ϵ) = lim 0 × log(ϵ) + 1 × log(1 − ϵ) = log(1) = 0.
ϵ→0+ ϵ→0+
It follows that
d(0, µ) = 2 lim t(0, ϵ) − t(0, µ)
ϵ→0+
= 2 0 − 0 × log(µ) − 1 × log(1 − µ) = −2 log(1 − µ)
Case 3: y = 1.
For y = 1, we need to replace t(y, y) with limϵ→0+ t(y, y − ϵ).
We have
lim t(1, 1 − ϵ) = lim 1 × log(1 − ϵ) + 0 × log(ϵ) = log(1) = 0.
ϵ→0+ ϵ→0
It follows that
d(1, µ) = 2 lim t(1, 1 − ϵ) − t(1, µ)
ϵ→0
= 2 0 − 1 × log(µ) − 0 × log(1 − µ) = −2 log(µ).
In summary
−2 log(1 − µ) when y = 0,
=
d(y, µ)
2 y log y
µ
+ (1 − y) log 1−y
1−µ
when y ∈ { m1 , . . . , m−1
m
}
−2 log(µ) when y = 1.
Chapter 5 Generalized linear models
Furthermore, there is a one-to-one correspondence between the canonical parameter θ and the
mean parameter µ = E(Y ) via the canonical link. Hence we can rewrite the variance in terms
of µ and ϕ as
Var(Y | µ, ϕ) = ϕV (µ)
where V (µ) is the variance function.
Var(Y | µ, ϕ) = ϕV (µ).
Lemma 5.1. Z
1
θ(µ) = dµ.
V (µ)
dµ
= b′′ (θ) = V (µ)
dθ
dθ 1
=⇒ =
dµ V (µ)
Z
1
=⇒ θ = dµ + C
V (µ)
64
5.1. THE VARIANCE FUNCTION 65
Lemma 5.2.
µ
Z
b θ(µ) = dµ.
V (µ)
Proof. (Proposition 5.1) Lemmas 5.1 and 5.2 show that we can construct the canonical parameter
θ and the cumulant function b(θ) from the variance function. As we have seen earlier, the
cumulant generating function is then defined as
Proposition 5.1 means that when modelling real-world data, we need to check the mean-variance
relationship. This will determine which EDM to use.
and
Z
µ Z
µ2
b(θ(µ)) = dµ = µdµ = .
V (µ) 2
Substituting µ = θ gives
θ2
b(θ) = .
2
Solution
a. For the scaled binomial distribution we have
Z
1 Z
1
θ = dµ = dµ.
V (µ) µ(1 − µ)
exp(z) dµ exp(z)
µ = and = h i2 = µ(1 − µ).
1 + exp(z) dz 1 + exp(z)
Therefore
Z
1 Z
1 dµ Z
1
θ = dµ = dz = µ(1 − µ)dz = z.
µ(1 − µ) µ(1 − µ) dz µ(1 − µ)
To express the cumulant function as a function of θ we use that θ = log(µ/(1 − µ)). Then
µ = 1+exp(θ)
exp(θ)
and so
1
!
b(θ) = − log(1 − µ) = − log = log (1 + exp(θ)) .
1 + exp(θ)
Next, Z
µ Z
µ Z
b(θ) = dµ = dµ = 1dµ = µ.
V (µ) µ
Thus, with θ = log(µ), we have
b(θ) = µ = exp(θ).
5.2 Quasi-likelihood
5.2.1 Introduction
It is possible that an EDM does not exist for a given combination of dispersion parameter ϕ
and variance function V (µ). For example, the Poisson and binomial distributions assume ϕ = 1.
But if we allow ϕ to be free, then there is no EDM for ϕ ̸= 1. Surprisingly, this does not stop us.
5.3. THE GAMMA DISTRIBUTION 67
Y ∼ Γ(s, r),
exp(−ry)rs y s−1
p(y | r, s) = for y > 0.
Γ(s)
The graph below shows the gamma density for different values of the shape parameter but with
a fixed mean.
• For s = 1, the gamma distribution is the exponential distribution.
• For s > 1, the density is unimodal and becomes increasingly symmetric as s → ∞.
• For s < 1, the density tends to ∞ as y → 0.
1
M. Maron (2007): Threshold effect of eucalypt density on an aggressive avian competitor. Biological
Conservation, 136, 100–107.
2
Dunn, P. K. and Smyth, G.K (2018): Generalized linear models with examples in R Vol. 53. New York:
Springer.
68 CHAPTER 5. GENERALIZED LINEAR MODELS
2.0
s = 1/2
s=1
s=2
1.5
s=4
density
1.0
0.5
0.0
1 1
1
y y
ϕ
p(y | µ, ϕ) = exp −
ϕµ y ϕµ Γ( ϕ1 )
b(θ) = − log(−θ).
1
µ = b′ (θ) = − ,
θ
1
V (µ) = b′′ (θ) = = µ2 .
θ2
5.3.2 Implementation in R
Use the argument family=Gamma in the command glm()! Note the capital letter G.
5.4. THE INVERSE GAUSSIAN DISTRIBUTION 69
In practice we do not use the canonical link. The canonical link function only permits negative
values for the linear predictor η = xT β and hence imposes a boundary on the parameter space
for the coefficients β. In contrast, the log link does not impose such a boundary, is more
numerically stable and makes it easier to interpret parameters.
For a log link function use family=Gamma(link="log").
Y ∼ IG(µ, ϕ),
1 (y − µ)2
s
p(y | µ, ϕ) = exp −
2πϕy 3 2ϕµ2 y
The inverse Gaussian distribution is useful for continuous distributions on the positive real line
where the variance increases very rapidly with the mean.
X0 = 0,
t
Xt = + σWt .
µ
3
Dunn, P. K. and Smyth, G.K (2018): Generalized linear models with examples in R Vol. 53. New York:
Springer.
70 CHAPTER 5. GENERALIZED LINEAR MODELS
where Wt is a Brownian motion (or Wiener process). This is a Brownian motion with drift µ1 . A
random walk is superimposed on top of a systematic tendency for the process {Xt } to increase
with time t. The hitting time for the process to hit fixed level Xt = 1 has an inverse Gaussian
distribution:
T = inf{t > 0 | Xt = 1} ∼ IG(µ, σ 2 ).
The plot below shows three realisations of the Brownian motion with drift, using the same
parameters for µ and ϕ. The dots show the hitting times for the three realisations.
Brownian Motion
Time
1
g(µ) = .
2µ2
In practice it is rarely used. The log link is numerically more stable and makes it easier to
interpret parameters.
Y ∼ Binomial(m, µ).
Suppose we increase the size parameter m but keep E(Y ) constant. What happens to the
distribution of Y ?
• The light bars show the probability mass function (pmf) for the binomial distribution as
the size parameter m increases.
0.15
0.20
0.15
0.15
0.10
pmf
pmf
pmf
0.10
0.10
0.05 0.05
0.05
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
x x x
Y ∼ Poisson(λ).
Proof Consider the moment generating function (mgf) of the (unscaled) binomial distribution
72 CHAPTER 5. GENERALIZED LINEAR MODELS
M (t) = E exp(tY )
m
!
m r
= exp(rt) µ (1 − µ)m−r
X
r=0 r
m
! r
m
= exp(t)µ (1 − µ)m−r
X
r=0 r
m
= exp(t)µ + 1 − µ
h im
= 1 + µ exp(t) − 1 .
h i
λ exp(t) − 1 m
M (t) = 1+
m
m
a
= 1+ for a = λ[exp(t) − 1].
m
M (t) −→ exp(a) = exp λ[exp(t) − 1]
• ratio between the final weight and initial weight (possibly with a log transformation,
in which case this is also the difference of the logs)
The family could be gamma, since the outcome is positive and continuous, with a log or
identity link. The family could also be gaussian with an identity link, particularly if the
response variable is log-transformed.
If the outcome is final weight, then the initial weight needs to be a predictor. The treatment
group (vitamin D supplementation versus control diet) should always be a predictor.
c. The outcome variable is the number of times a given car has broken down in 10 years. As
this is count data we may consider a Poisson model. The predictor variables are: mileage,
model, and service history. An offset is not required as the cars all have the same follow-up
time (10 years). The Poisson family is compatible with the log link, square root link, and
identity link.
Chapter 6 Maximum likelihood estimation for
GLMs
pi (y | β) for β ∈ Rp ,
We normally work in terms of the log likelihood, as the log likelihood is the sum of individual
contributions from independent observations
n
= log L(β | y) = log pi (yi | β)
X
l(β | y)
i=1
then β (1) has more support from the data y than β (2) .
75
76 CHAPTER 6. MAXIMUM LIKELIHOOD ESTIMATION FOR GLMS
b | y) − l(β | y)
l(β > 0 ∀β ̸= β,
b
so has the highest support from the data among all possible parameter values.
The score function U : Rp → Rp is the first derivative of the log likelihood:
∂l(β | y)
U (β | y) = .
∂β
U (β
b | y) = 0.
The Fisher information (or expected information) matrix I(β) is positive semi-definite, that is
∂ 2 l(β | y)
J(β | y) = − .
∂β∂β T
a. Let L denote the likelihood function and U the score function, then for a Poisson distribu-
tion with mean µ we have
n
µyi −µ
=
Y
L(µ | y) e ,
i=1 yi !
n
= log(L(µ|y)) = yi log(µ) − µ − log(yi !)
X
l(µ | y)
i=1
n
hX i
= log(µ) yi − nµ + constant,
i=1
n P
dl(µ|y) i=1 yi
U (µ | y) = = − n,
dµ µ
1 n
nµ
X
E U (µ | Y ) = E Yi − n = −n = 0.
µ i=1 µ
b. Let J denote the observed information and I the Fisher information, then
dU (µ | y)
Pn
d2 l(µ | y) yi
J(µ | y) = − = − = i=1
,
dµ2 dµ µ2
1 n
n
X
I(µ) = E J(µ | y) = E Yi = .
µ2 i=1 µ
1. Consistency:
p
As n → ∞ we have β
b → β.
2. Invariance under reparameterisation:
Suppose γ is an alternative parameterization to β. Then for some invertible function s
we have
γ = s(β) and β = s−1 (γ).
The maximum likelihood estimates then satisfy
γb = s(β)
b and b = s−1 (γ).
β b
3. Asymptotic unbiasedness:
As n → ∞,
√ b
n E(β) − β → 0.
5. Asymptotic normality:
For sufficiently large n we can use the approximation:
β
b ∼ N (β, I(β)−1 ).
For further details see Sections 4.4 - 4.9 in the recommended textbook by Dunn and Smyth.1
U (β)
b = 0.
We need an expression for the score function U (β) for GLMs. We also need an expression for
the Fisher information matrix for GLMs, that is
∂ 2 l(β)
I(β) = E −
∂β∂β T
so that we can calculate standard errors using the large sample approximation
β
b ∼ N β, I(β)−1 .
Yi ∼ EDM(µi , ϕ/wi )
g(µi ) = xTi β
Recall from Section 4.3 that Yi as defined above has pdf/pmf given by
h i
w
i yi θi − b(θi )
p(yi |θi , ϕ) = a(yi , ϕ/wi ) exp .
ϕ
1
Dunn, P. K. and Smyth, G.K (2018): Generalized linear models with examples in R Vol. 53. New York:
Springer.
6.2. MAXIMUM LIKELIHOOD FOR GLMS 79
Also recall that the mean parameter µi for observation i can be mapped onto the canonical
parameter θi using the canonical link function. This allows us to write the log likelihood in
canonical form:
n
l(β, ϕ) = li (β, ϕ)
X
i=1
h i
n w
i θi yi − b(θi )
= + log a(yi , ϕ/wi )
X
i=1 ϕ
where implicitly θi = θi (β) is a function of β. We can use this to derive the score function.
Lemma 6.1. h i
n wi yi − µi ∂θi
U (β) =
X
.
i=1 ϕ ∂β
i=1 i=1 ∂β
n
∂li (θi ) ∂θi
=
X
i=1 ∂θi ∂β
h i
wi yi − b′ (θi ) ∂θi
n
=
X
i=1 ϕ ∂β
h i
wi yi − µi ∂θi
n
=
X
.
i=1 ϕ ∂β
∂θi
= xi ,
∂β
h i
n w i y i − µi
U (β) =
X
xi ,
i=1 ϕ
n
wi V (µi )
= xi xTi .
X
I(β)
i=1 ϕ
80 CHAPTER 6. MAXIMUM LIKELIHOOD ESTIMATION FOR GLMS
∂θi xi
= ,
∂β g ′ (µ i )V (µi )
h i
n wi yi − µi xi n
Wi g ′ (µi )
U (β) = =
X X
yi − µi xi ,
i=1 ϕ g ′ (µi )V (µi ) i=1 ϕ
n n
wi Wi
= x xT = xi xTi ,
X X
I(β) 2 V (µ ) i i
i=1 ϕ[g ′ (µ i )] i i=1 ϕ
wi
where Wi = is the ith working weight.
[g ′ (µ i )] V (µi )
2
∂θi
= xi , and
∂β
h i h i
n wi yi − µi ∂θi n w i y i − µi x i
U (β) = =
X X
.
i=1 ϕ ∂β i=1 ϕ
Note:
• The maximum likelihood estimate β
b solves the score equation U (β)
b = 0 which we can
write as
n h i
wi yi − µi (β) =
X
b x
i 0
i=1
independent of ϕ.
6.2. MAXIMUM LIKELIHOOD FOR GLMS 81
• Suppose the model has an intercept term, then the first column of the design matrix X is
a column of ones (xi1 = 1 ∀i). Writing µb i = µi (β),
b the score equation for column 1 solves
n h i
= 0.
X
wi yi − µb i
i=1
This generalizes the result for linear models with an intercept term that the (weighted)
sum of residuals is zero.
n
∂ 2 li (β) n
wi V (µi )xi xTi
= = (6.1)
X X
I(β) E − T .
i=1 ∂β∂β i=1 ϕ
n
∂ 2 li (β) n
∂Ui
= =
X X
J(β) − T − T
i=1 ∂β∂β i=1 ∂β
n
∂Ui ∂θi
=
X
− T
i=1 ∂θi ∂β
h i
n
∂ wi yi − µi xi ∂θi
=
X
−
i=1 ∂θi ϕ ∂β T
h i
∂ wi yi − b (θi ) xi T
n ′
=
X
− xi
i=1 ∂θi ϕ
n
wi b′′ (θi )xi xTi
=
X
i=1 ϕ
n
wi V (µi )xi xTi
=
X
i=1 ϕ
Recall that the canonical link function of the normal distribution is the identity link. Therefore
Compare this to the expression in Proposition 6.1. Note that for a normal linear model with
wi = 1 for all i = 1, . . . , n:
1X n h i 1X n h i
U (θ|y) = yi − θi xi = yi − xTi β xi .
ϕ i=1 ϕ i=1
Then setting the score function to zero we obtain the normal equations we derived in
ST221/ST231:
n h i
yi − xTi β xi =
X
0
i=1
or in matrix form
(y − Xβ)T X = 0
which is solved by setting β = (X T X)−1 X T y.
Next, to obtain the Fisher information, we take the expectation of minus the Hessian of the
score function, which gives
∂U (θ|y) n
wi ∂θi n
wi
X
= = = = xi xTi .
X
I(β) E(J(β)) −E −E xi T
∂β T i=1 ϕ ∂β i=1 ϕ
Compare this to the expression in Proposition 6.2 using the fact that V (µ) = 1 for a (weighted)
normal linear model.
6.2. MAXIMUM LIKELIHOOD FOR GLMS 83
The working weights should not be confused with the prior weights w1 , . . . , wn defined by us
when we fit the model.
Proposition 6.3 (Score function for GLM with a general link). For a general link function g()
we have
∂θi xi
= and
∂β g ′ (µi )V (µi )
n
Wi g ′ (µi )
U (β) =
X
y i − µi x i
i=1 ϕ
As µi = b′ (θi ) we have
∂µi
= b′′ (θi ) = V (µi )
∂θi
and so
∂θi 1
= .
∂µi V (µi )
∂µi
g ′ (µi ) = xi
∂β
and so
∂µi xi
= .
∂β g (µ
′
i)
h i
n wi yi − µi ∂θi
U (β) =
X
i=1 ϕ ∂β
h i
n wi yi − µi 1 xi
=
X
Proposition 6.4 (Fisher information for a GLM with a general link). For a general link function
g() the Fisher information matrix is
n
Wi
= xi xTi .
X
I(β)
i=1 ϕ
Proof of Proposition 6.4 (non-examinable): Again, we will use Lemma 6.1, that is
h i
n wi yi − b′ (θi ) ∂θi
U (β) =
X
.
i=1 ϕ ∂β
We first consider the observed information
h i
∂U (β) ∂ n
X wi yi − b′ (θi ) ∂θi
J(β) = − = −
∂β T ∂β T i=1 ϕ ∂β
h i
n w y − b′ (θ )
n h
wi b′′ (θi ) ∂θi i ∂θi i i i ∂ 2 θi
=
X X
−
i=1 ϕ ∂β ∂β T i=1 ϕ ∂β∂β T
where the last step follows by the product rule of differentiation. Now, in the above result the
first term does not depend on y. The second term, when we replace yi with Yi , has expectation
zero as E(Yi ) = µi = b′ (θi ). Hence
n
wi b′′ (θi ) ∂θi ∂θi
=
X
I(β)
i=1 ϕ ∂β ∂β T
Now b′′ (θi ) = V (µi ) and by Proposition 6.3
∂θi xi
= .
∂β g ′ (µ i )V (µi )
This allows us to write the Fisher (or expected) information matrix in terms of µi :
n
wi
= xi xTi .
X
I(β)
i=1 ϕ[g ′ (µ i )] V (µi )
2
6.3. NUMERICAL MAXIMUM LIKELIHOOD ESTIMATION FOR GLMS 85
(k)
Let β̃ be our current estimate of β. Using first order Taylor approximation we can approximate
(k)
the score function in the neighbourhood of β̃ :
(k)
(k) ∂U (β̃ ) h (k)
i
U (β) ≈ U (β̃ )+ β − β̃
∂β T
(k) (k) (k)
= U (β̃ ) − J(β̃ )(β − β̃ )
Hence
(k) (k) (k)
h i
U (β) ≈ U (β̃ ) − I(β̃ ) β − β̃
n g(k) n g(k)
Wi Wi h (k) i
= g (µ
′
fi ) yi − µ
(k) (k)
xi xTi β − β
X X
fi xi − e
i=1 ϕ i=1 ϕ
n g(k)
Wi
e (k)
= g (µ
′
fi ) yi − µ
(k) (k)
+ xTi β xTi β
X
fi − xi
i=1 ϕ
n g(k)
Wi
= zei (k) xTi β
X
− xi
i=1 ϕ
where
(k) wi
W
g
i =
[g ′ (µ
fi )]2 V (µ
(k)
fi (k) )
is the current working weight and
(k)
zei (k) = g ′ (µ
fi (k) ) yi − µ
fi (k) + xTi β
e
This is equivalent to estimating β for a linear model on the working observations, that is
ϕ
T
Z
f
i ∼ N x i β, .
g(k)
W i
6.3. NUMERICAL MAXIMUM LIKELIHOOD ESTIMATION FOR GLMS 87
The maximum likelihood estimate for this weighted linear model solves the approximate score
equation and is given by
−1
(k+1) (k) (k) (k) (k) (k)
β
e = XT W
g X XT W
g zei (k) where W
g = diag(W
g
1 ,...,W
g
n ).
(k+1)
This new estimate β
e becomes our new value of βe for the next iteration. We continue until
e (k+1) is the same as the previous one, that is β
the new estimate β e (k) .
n g(k)
Wi
(k+1)
zei (k) − xTi β =
X
e xi 0.
i=1 ϕ
But then
(k+1) (k) (k+1)
zei (k) − xTi β
e = g ′ (µ
fi (k) ) yi − µ
fi (k) + xTi β
e − xTi β
e
e (k) = β
e (k+1) .
= fi (k) ) yi − µ
g ′ (µ fi (k) as β
e (k+1) ) 1X n
g(k) g ′ (µ
U (β = Wi fi (k) ) yi − µ
fi (k) xi = 0.
ϕ i=1
1 Xn
g(k) x xT
Wi i i
ϕ i=1
and thus exactly equal to I(βe(k) ), the Fisher information matrix for our GLM.
Hence we can use the asymptotic result
n
g(k) x
X
i xi )
T −1
βb ∼ N β, ϕ W i .
i=1
Solution:
Let β
e be our starting value for β. We then solve the approximate score equation
n g
Wi
zei − xTi β xi =
X
0
i=1 ϕ
where
µ
fi = E(Yi | β)
e
zei = g ′ (µ
fi ) yi − µfi + xTi β
e and
wi
W
g
i =
[g ′ (µ
fi )]2 V (µ
fi )
g(µi ) = µ,
g ′ (µi ) = 1.
V (µi ) = 1.
wi = 1.
Hence
wi
W
g
i = = 1.
[g (µ
′ fi )]2 V (µ
fi )
Furthermore
zei = g ′ (µ
fi ) yi − µ
fi + xTi β
e = e + xT β
yi − xTi β i
e = yi .
which are the usual normal equations for a normal linear model. Note that these equations no
longer depend on the starting value β
e for β. We can write the equations in matrix notation as
T
y − Xβ X = 0
β
b = (X T X)−1 X T y.
6.4. ESTIMATING THE DISPERSION PARAMETER 89
6.4.2 Derivation
The last iteration of the IWLS algorithm is based on a linear approximation
ϕ
Z
c
i ∼ N xTi β,
Wi
d
where
zbi = g ′ (µb i ) yi − µb i + g(µb i )
with
wi
g(µb i ) = xTi β
b and W
d
i = h i2 .
V (µb i ) g ′ (µb i )
90 CHAPTER 6. MAXIMUM LIKELIHOOD ESTIMATION FOR GLMS
Under the normal weighted linear model approximation the estimator of ϕ is given as
1 X n 2
ϕb = Wi i
d z
b − x Tb
i β .
n − p i=1
Note the denominator is n − p as we lose p degrees of freedom from estimating the p parameters.
Substituting expressions for W
d and zb :
i i
1 X n 2
ϕb = Tb
Wi zbi − xi β
d
n − p i=1
1 X n 2
wi
= g ′
(µ i ) y i − µ i +x Tb
i β − xTb
i β
n − p i=1 V (µb i ) g ′ (µb i ) 2
h i b b
1 X n
wi h i2 2
= i2 g (µ
′
bi yi − µb i
n − p i=1 V (µb i ) g ′ (µb i )
h
1 X n
wi 2
= yi − µb i
n − p i=1 V (µb i )
In R we can get the Pearson residuals from a fitted GLM with the command
residuals([Link], type="pearson")
We will see later that there are many possible types of residual for a GLM.
The Pearson estimator for ϕ is the one used by the summary() function in R.
V ∼ Γ(µ, ϕ).
Call:
glm(formula = Volume ~ log(Height) + log(Girth), family = Gamma(link = "log"),
data = trees)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.69111 0.78784 -8.493 3.11e-09 ***
log(Height) 1.13288 0.20138 5.625 5.04e-06 ***
log(Girth) 1.98041 0.07389 26.802 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(p)
E ri −→ 0,
(p)
Var ri −→ ϕ(1 − hi )
where hi is the leverage of the ith observation (see later).
92
7.1. RESIDUALS FOR GLMS 93
1 if T > 0
sign(T ) = 0 if T = 0
−1 if T < 0.
Recall from Chapter 4 that the total deviance from a fitted model is
n
= wi d(yi , µb i )
X
D(y, µ)
b
i=1
This generalizes the result for (unweighted) linear models that the deviance is the sum of squares
of the residuals.
In general, the deviance residual is a non-linear function of the outcome variable Yi and is not
guaranteed to have expectation zero:
(d)
E(ri ) ̸= 0.
Like the response residuals, the working residuals do not account for difference in variance
between observations. You need the working weights Wc to interpret them.
i
94 CHAPTER 7. DIAGNOSTICS FOR GLMS
Yi ∼ N (xTi β, ϕ)
V (µ) = 1,
g(µ) = µ,
d(y, µ) = (y − µ)2 .
(r)
ri = yi − µb i = yi − xTi β.
b
Next consider the Pearson residuals. Recall that V (µb i ) = 1 and wi = 1. Hence
(p) yi − µb i
ri = q = yi − µb i = yi − xTi β.
b
V (µb i )/wi
For the deviance residuals recall that the unit deviance of a normal linear model is given by
d(y, µ) = (y − µ)2 and so
q
(d)
ri = sign(yi − µi ) wi d(yi , µb i )
b = sign(yi − µb i ) |yi − µb i | = yi − µb i = yi − xTi β.
b
Finally, for the working residuals, recall that the working observations are defined as
Note that if we consider a weighted linear model, that is wi ̸= 1 for some i, then we have two
sets of residuals
(p) (d) √
ri = ri = wi (yi − xTi β)
b
(r) (w)
ri = ri = yi − xTi β.
b
Note that these correspond to the weighted and unweighted residuals discussed in Chapter 1.
7.2. LEVERAGE AND STANDARDIZED RESIDUALS 95
Yi ∼ N (xTi β, ϕ/wi ).
E(Ri ) = 0,
(1 − hi )ϕ
Var(Ri ) = ,
wi
where hi = H ii is the ith diagonal element of the hat matrix
−1
H = X XT W X XT W
96 CHAPTER 7. DIAGNOSTICS FOR GLMS
8
6
8 10 12 14 16 18
So the hat value hi measures how far away the predictor variable xi is from the mean x .
Suppose x ∈ {0, 1} is a binary predictor with
• n0 observations with xi = 0 and
• n1 observations with xi = 1.
Then
if xi = 0,
( 1
hi = n0
1
n1
if xi = 1.
You should expect to see observations with high leverage if they have unusual values for the
predictor variables:
• for continuous variables if they have values far from mean;
• for categorical variables if they belong to a sparse category.
7.2. LEVERAGE AND STANDARDIZED RESIDUALS 97
where h
b =H
i
c for the working hat matrix
ii
−1
H
c = X XT W
dX XT W
d
and W
d = diag(W c ) is a diagonal matrix of working weights.
c ,...,W
1 n
(sp)
This is also the standardized Person residual ri .
We can also standardize the deviance residual using the working hat matrix
q
(sd) wi d(yi , µb i )
ri =
sign(yi − µb i ) q .
(1 − hc)ϕ
i
b
7.3 Influence
Model fitting should not depend on a handful of observations. We therefore need to consider
what happens if we drop an observation from the data set and refit the model. If the estimates
or predicted values change substantially, we say the observation is influential.
ϕ
Yi ∼ N µi , ,
wi
µi = xi β T .
We will use a superscript (i) to denote a quantity estimated after deleting observation i. Then,
the effect on the estimated parameters can be measured as
ri
(i)
b −β
β b = (XW X)−1 xi wi
1 − hi
b (Proof omitted).
where W = diag(w1 , . . . , wn ) and ri = yi − xTi β
This quantity is known as dfbeta. It can be calculated without refitting the model.
The effect on fitted values is given by
ri ri
b (i)
xTj (XW X)−1 xi wi
(i)
µb j − µb j = xTj β
b −β = = H ji .
1 − hi 1 − hi
1 X n
ri 2 ri2 n
DiCook = 2
= wj H 2ji
X
wj H ji
pϕb j=1 1 − hi (1 − hi )2 pϕb j=1
ri2 ri2
= H T
W H = (W H)ii
(1 − hi )2 pϕb ii (1 − hi )2 pϕb
ri2 1 wi ri2 hi
= h i w i = b − h ) (1 − hi )
(1 − hi )2 pϕb p ϕ(1 i
7.3.3 Summary
1. Model fitting should not depend on a handful of observations.
2. Look for observations with high influence:
• large absolute residuals and/or
• high leverage.
3. Investigate observations with much larger influence than the rest (if any).
4. Sensitivity analysis to check stability of results if you do delete an observation.
100 CHAPTER 7. DIAGNOSTICS FOR GLMS
A red line shows a smooth curve fitted to the residuals. If the structural part of the model is
correct (link function and predictor variables), the smooth curve should remain close to the
value zero on the y-axis.
Residuals vs Fitted
3
106 174
2
Pearson Residuals
1
0
−1
−2
−3
132
0 2 4 6 8
Predicted values
glm(y ~ x)
7.4.2 QQ plot
With which=2 you get a quantile-quantile (QQ) plot with
• sorted absolute values of the standardized deviance residuals on the y-axis;
• theoretical quantiles of the standard half-normal distribution on the x-axis.
Ideally, the points should line up in a straight line through the origin and with slope 1. The
(sd)
observations with the 3 largest values of ri are labelled with their row numbers.
7.4. DIAGNOSTIC PLOTS IN R 101
Q−Q Residuals
Theoretical Quantiles
glm(y ~ x)
The absolute value of the standardized deviance residuals has a half-normal distribution when
the saddlepoint approximation applies:
• exact for normal and inverse Gaussian,
• approximate for
– Gamma for shape > 3 (ϕ < 1/3),
– Poisson for y ≥ 3,
– Binomial for 3 ≤ y ≤ m − 3.
Scale−Location
132
174
106
1.5
Std. Pearson resid.
1.0
0.5
0.0
0 2 4 6 8
Predicted values
glm(y ~ x)
observations with the 3 largest values of Cook’s distance are labelled with the row number. This
plot is normally omitted if you do not specify the which argument.
Cook's distance
0.08
132
0.06
Cook's distance
47
0.04
106
0.02
0.00
Obs. number
glm(y ~ x)
A red line shows a smooth curve fitted to the residuals. Note that Cook’s distance can be
(sp)
calculated from ri and h
b .
i
• The observations with the 3 largest values of DiCook are labelled with their row numbers.
• Dashed grey lines show contours of the Cook’s statistic for DCook = 0.5 and DCook = 1 to
highlight highly influential observations.
• Contours may not be shown if they lie outside the boundaries of the plot, but a legend is
still produced.
Residuals vs Leverage
3
106
47
2
Std. Pearson resid.
1
0
−1
−2
−3
132
Cook's distance
Leverage
glm(y ~ x)
For binary data, only the first diagnostic plot is of interest. Do not try to interpret the other
plots (except leverage plot which=4).
Binned residual plots may be more visually appealing for discrete data.
β
b ∼ Np (β, I(β)−1 ),
where I(β) is the Fisher (or expected) information matrix. Here
I(β)−1 = ϕ (X T W
dX)−1 .
where Wd is a diagonal matrix of working weights weights from the final iteration of the IWLS
algorithm.
where h i1/2
SE(βbj ) = ϕ (X T W
dX)−1
jj
1 n h
(P ) 2
i
=
X
ϕb ri
(n − p) i=1
As in the normal linear model, we use the t-distribution to account for additional uncertainty
due to estimation of ϕ, for example confidence limits are
104
8.2. CONFIDENCE INTERVALS BASED ON PROFILE LIKELIHOOD 105
8.1.3 Quantiles in R
All distributions in R have a function with prefix q that gives the quantiles of the distribution,
for example
qnorm(0.975) #Standard normal distribution
[1] 1.959964
qt(0.975, df=20) # t-distribution on 20 df
[1] 2.085963
qt(0.975, df=2) # t-distribution on 2 df
[1] 4.302653
(note that the uncertainty becomes much larger when you have very few d.f.)
The general rule for a confidence interval with coverage 100 × (1 − α) is to call these quantile
functions with argument 1 − α/2.
i
1 X
= wi d(yi , µi ) + log a∗ (yi , ϕ/wi )
X
−
2ϕ i i
1
= − D(y, µ(β)) + log a∗ (yi , ϕ/wi )
X
2ϕ i
Suppose β A and β B are two competing possible values for β. We can compare the relative
support for the two values by comparing the log likelihood
1
log L(β A ) − log L(β B ) = − [D(y, µ(β A )) − D(y, µ(β B ))]
2ϕ
Hence differences in deviance are scaled differences in log likelihood
D(y, µ(β A )) − D(y, µ(β B )) = −2ϕ [log L(β A ) − log L(β B )]
L(β A )
!
= −2ϕ log
L(β B )
106 CHAPTER 8. MODEL CHOICE AND HYPOTHESIS TESTS
We focus interest on the ith element of β, and eliminate nuisance parameters βj for j ̸= i by
minimizing the deviance over all values of β that have βi = b.
There is a different profile deviance function for each of the p elements of β. Hence the subscript
i in Diprof (b).
We can create a set of values for parameter βi most consistent with the data by choosing a set
n o
b : Diprof (b) ≤ K
for some cut-off value K. Note that such a confidence interval is not restricted to be symmetric!
K is chosen based on the repeated sampling principle:
• We want to use statistical procedures that give the right answer most of the time.
• We want to choose procedures that have good properties under hypothetical repetition,
assuming the model is correct.
• For confidence intervals the key property is coverage.
– Coverage is the proportion of times that a confidence interval generated using the
same procedure contains the true value under repeated sampling.
– By convention we use 95% coverage.
If ϕ is free:
We plug in the estimate ϕb and adjust for the additional uncertainty from estimating ϕ. Then,
for n → ∞,
Diprof (βi ) − Diprof (βbi )
∼ F1,n−p .
ϕb
Hence we choose our cut-off as the 95th percentile of the F1,n−p distribution.
8.2. CONFIDENCE INTERVALS BASED ON PROFILE LIKELIHOOD 107
In R:
qchisq(0.95, df=1)
[1] 3.841459
qf(0.95, df1=1, df2=20)
[1] 4.351244
qf(0.95, df1=1, df2=2)
[1] 18.51282
For a confidence interval with coverage 100 × (1 − α), call these quantile functions with argument
1 − α.
Recall the Wald statistics used to derive the CI based on asymptotic normality. The Wald
statistics are based on a quadratic approximation (for n → ∞) to the likelihood.
8.2.4 Example
Below the computation of confidence intervals in R is demonstrated. The relevant R code is
available on Moodle.
The Wald confidence intervals can be computed from the quantities provided by the model
summary output.
To compute profile confidence intervals we use the command confint(). This is a numerically
expensive operation, so R will give a warning message.
library(faraway)
data(wcgs)
wcgs$height = wcgs$height * 0.0254
wcgs$weight = wcgs$weight * 0.453592
108 CHAPTER 8. MODEL CHOICE AND HYPOTHESIS TESTS
wcgs$bmi = wcgs$weight/wcgs$heightˆ2
n = 3131 p = 8
Deviance = 1639.18499 Null Deviance = 1777.31898 (Difference = 138.13399)
To compute the Wald confidence intervals we will need to extract the appropriate summary
statistics, as below.
[Link] <- coef([Link])
SE <- sqrt(diag(vcov([Link])))
CI <- cbind(estimate=[Link],
lower=[Link] - 1.96 * SE,
upper=[Link] + 1.96 * SE)
CI
estimate lower upper
(Intercept) -8.308770209 -9.808140159 -6.80940026
bmicatoverweight 0.126945642 -0.149749854 0.40364114
bmicatobese 0.467395714 -0.206581090 1.14137252
age 0.062874304 0.039711377 0.08603723
cigs 0.022455987 0.014319544 0.03059243
dbp 0.002943465 -0.017287862 0.02317479
sdp 0.019614957 0.007305223 0.03192469
dibepB -0.684546170 -0.965991606 -0.40310073
The above gives the Wald confidence intervals on the scale of the parameter. We transform the
intervals to obtain the intervals for the relevant odds rations.
round(exp(CI[-1,]), 2)
estimate lower upper
bmicatoverweight 1.14 0.86 1.50
bmicatobese 1.60 0.81 3.13
8.3. HYPOTHESIS TESTS FOR GLMS 109
The confint() function computes confidence intervals from the profile deviance.
CI2 <- confint([Link])
2.5 % 97.5 %
bmicatoverweight 0.86 1.50
bmicatobese 0.78 3.03
age 1.04 1.09
cigs 1.01 1.03
dbp 0.98 1.02
sdp 1.01 1.03
dibepB 0.38 0.67
The profile likelihood based confidence intervals are similar but not all identical to the Wald
confidence intervals.
Please try for yourself the code provided in Moodle for the lime data where you also need to
estimate the dispersion parameter ϕ.
βbi
Zi = .
SE(βbi )
Under H0 : βi = 0,
Zi ∼ N (0, 1)
The p-value is the probability under hypothetical repeated sampling that Zi is more extreme
than the observed value ziobs .
Since we have a two-sided alternative hypothesis, we take the absolute value.
h i
P |Zi | ≥ ziobs = 2 1 − Φ ziobs
but now ϕ is unknown. So we plug in our estimate of ϕ from the Pearson residuals to get
standard error −1 1/2
SE(βi ) =
b b Td
ϕ X WX .
ii
βbi
Ti = .
SE(βbi )
8.3. HYPOTHESIS TESTS FOR GLMS 111
with pB , β
b ,µ
B b B , ϕB defined similarly.
b
b A ) − D(Y , µ
D(Y , µ b B)
∆ = ∼ χ2pB −pA .
ϕ
112 CHAPTER 8. MODEL CHOICE AND HYPOTHESIS TESTS
and Fχ2 (·, d) is the cumulative distribution function of the χ2d distribution.
Note we use the estimate ϕbB from the larger model, which is a valid estimator for both models.
A large difference in deviance is evidence against model A. Hence the p-value is
∆obs
!
P ∆≥∆ obs
= 1 − FF , p B − pA , n − pB
pB − pA
and FF (·, d1 , d2 ) is the cumulative distribution function of the Fd1 ,d2 distribution.
8.3. HYPOTHESIS TESTS FOR GLMS 113
n = 3131 p = 4
Deviance = 1728.09442 Null Deviance = 1777.31898 (Difference = 49.22456)
Let’s consider what happens if we remove bmicat from the model.
glm.wcgs2 <- glm(chd ~ age,
family=binomial, data=wcgs)
sumary(glm.wcgs2)
n = 3154 p = 2
Deviance = 1738.35613 Null Deviance = 1781.24374 (Difference = 42.88761)
Note the change in the sample size n that is reported at the bottom of the summary. The
variable bmicat has missing data as all participants with BMI less than 18.5 were classified as
114 CHAPTER 8. MODEL CHOICE AND HYPOTHESIS TESTS
NA.
We remove these participants from the data and then refit the model.
# Fitting the model on the reduced dataset
glm.wcgs2 <- glm(chd ~ age,
family=binomial, data=subset(wcgs, bmi>=18.5))
sumary(glm.wcgs2)
Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.938165 0.549709 -10.8024 < 2.2e-16
age 0.074565 0.011311 6.5923 4.332e-11
n = 3131 p = 2
Deviance = 1734.33688 Null Deviance = 1777.31898 (Difference = 42.98210)
We can now perform an Analysis of Deviance comparing model [Link] against [Link]:
anova(glm.wcgs2, [Link])
Response: chd
Residual statistics —
R2 , Radj
2
—
— AIC
Call:
lm(formula = log(DMFT) ~ Sugar + Indus, data = dental)
Residuals:
Min 1Q Median 3Q Max
-1.57067 -0.31668 0.02093 0.33304 1.27374
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.373565 0.199409 1.873 0.0644 .
Sugar 0.018351 0.004076 4.502 2.08e-05 ***
IndusNonInd -0.203221 0.143950 -1.412 0.1616
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Call:
glm(formula = log(DMFT) ~ Sugar + Indus, family = gaussian(),
data = dental)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.373565 0.199409 1.873 0.0644 .
Sugar 0.018351 0.004076 4.502 2.08e-05 ***
IndusNonInd -0.203221 0.143950 -1.412 0.1616
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
∆ DM0 − DM1
= ∼ Fp−1,n−p ,
p−1 ϕbB (p − 1)
where in our example n = 90 and p = 3 and so the relevant F -distribution has 2 and 87
degrees of freedom. The observed value of p−1
∆
is given by
38.584 − 26.989
= 18.69
2 × 0.3102
Therefore the information provided on the null and residual deviance for the GLM summary
is equivalent to the information on the F -statistics in the summary of the normal linear
model.
8.5. MODEL CHOICE 117
Furthermore, the Akaike Information Criterion (AIC) introduced in ST231 for the normal
linear model can be extended to GLMs and is routinely reported in the glm summary output.
The AIC is defined as
AIC = −2 log L(β, b ϕb
MLE ) + 2p ,
′
where
• L is the likelihood of the chosen model,
• ϕbMLE is the maximum likelihood estimate of ϕ and not the estimate introduced earlier
based on the Pearson residuals,
• p′ is the number of parameters which includes ϕ if the chosen EDM has a free dispersion
parameter.
We note that
• only differences in AIC are relevant,
• the AIC can be used to compare non-nested models,
• the AIC is not consistent.
An alternative that is consistent is the Bayesian information criterion (BIC) which is
defined as
BIC = −2 log L(β, b ϕ
b
MLE ) + p log(n).
′
Thus, provided log(n) > 2, the BIC imposes a heavier penalty for model complexity and will
therefore tend to choose a simpler model than the AIC.
• Prediction:
• Causal reasoning:
George Box (1919-2013), one of the greatest applied statisticians of the 20th century, wrote:
It has been said that “all models are wrong but some models are useful.” In
other words, any model is at best a useful fiction. There never was, or ever will
be, an exactly normal distribution or an exact linear relationship. Nevertheless,
enormous progress has been made by entertaining such fictions and using them as
approximations.
• Nesting defines a partial ordering between models, which can be represented graphically
by a directed acyclic graph.
• Mi ⪯ Mj if there is a path from Mi to Mj following the arrows.
• If there is no path either way, then the two models are not nested.
8.6. VARIABLE SELECTION - REVIEW 119
V (µ) = µ and ϕ = 1.
are called overdispersed and cannot be represented with a Poisson model. There are several
alternatives:
• the quasi-Poisson distribution,
• robust "sandwich" standard errors,
• the negative binomial distribution.
∂U (β)
b
U (β) ≈ U (β)
b + (β − β)
b
∂β T
= J(β)(
b β b − β)
≈ I(β)(
b β b − β)
120
9.1. OVERDISPERSION FOR COUNT DATA 121
Hence
Var [U (β)] ≈ I(β)
b Var(β)
b I(β)
b
Next we obtain an empirical estimate of the variance of U (β) by taking a scaled sum of squares
of its individual components:
n
n cT
Ω
X
b ≡ U
cU
i i
(n − p) i=1
where
d (y − µ
W b i )g ′ (µ
b i )xi
i i
U
c
i =
ϕ
and we make the necessary adjustment for degrees of freedom lost from estimating β.
Hence
Var [U (β)] ≈ Ω
b
and so
Var(β)
b ≈ b −1 Ω
I(β) b −1
b I(β)
Var(Y ) > µ.
The negative binomial distribution arises when we fix the target number of successes S = s
and we count the number of failures F until we reach the target.
f +s−1
!
P(F = f ) = π s (1 − π)f
s−1
for f ∈ N0 .
The expectation of the negative binomial distribution is given by
(1 − π) s
µ = E(F ) =
π
We can rewrite the probability mass function in terms of µ as
f +s−1 s s µf
!
P(F = f ) =
s−1 (µ + s)f +s
Yi ∼ Poisson(λi ),
λi ∼ Γ(µ, 1/s),
where we have parameterized the gamma distribution in mean value form with mean µ and
dispersion 1/s.
Then (skipping the derivation)
Γ(y + s) s s µy
P(Yi = y) = .
Γ(s)y! (µ + s)s+y
Exercise 21 - Overdispersion
Explore overdispersion in Computer Practical 4.
Solution: Solutions to Practical can be found on moodle.
E(Y ) = 1 + X100
using the command y <- rnorm(100, mean=1+mydata[,100]).
9.2. INTRODUCTION TO PREDICTION 125
To select from the 100 potential explanatory variables, we fit 100 simple linear regressions,
using each explanatory variable in turn and choose the predictor variables that are significantly
associated with the response (at a 5% significance level).
We then fit a normal linear model with the chosen predictor variables which produces the
following results:
Call:
lm(formula = y ~ ., data = [Link])
Residuals:
Min 1Q Median 3Q Max
-2.13577 -0.55479 0.05966 0.57150 1.93034
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.08301 0.08889 12.184 < 2e-16 ***
V6 -0.22977 0.09327 -2.464 0.01557 *
V35 -0.27887 0.09728 -2.867 0.00512 **
V54 0.26847 0.08019 3.348 0.00117 **
V85 0.18997 0.08259 2.300 0.02365 *
V100 0.76339 0.08704 8.770 7.56e-14 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
E(Y ) = 1 + X100 ,
where X100 is our fifth predictor variable. When we fit the a normal linear model to this new
data set, we obtain the following results:
126 CHAPTER 9. OVERDISPERSION, PREDICTION AND CLASSIFICATION
Call:
lm(formula = new.y ~ ., data = [Link])
Residuals:
Min 1Q Median 3Q Max
-3.2440 -0.6340 -0.0391 0.8171 1.9605
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.88582 0.10332 8.573 1.98e-13 ***
V6 0.24212 0.10842 2.233 0.0279 *
V35 -0.16283 0.11308 -1.440 0.1532
V54 -0.06810 0.09321 -0.731 0.4669
V85 0.01671 0.09601 0.174 0.8622
V100 0.83288 0.10118 8.231 1.04e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
9.2.6 Cross-validation
If the sample size is too small to be able to divide the data into a training and a test set, we
may use cross-validation.
K-fold cross-validation works as follows:
• Divide the data into K subsets.
• One of the K subsets acts as the test set while the others are used as a training set.
• Repeat such that each of the K subsets are used once as a test set.
• Combine the evaluation results.
Leave-one-out cross-validation:
• One observation acts as the “test set” while the others are used for training.
• Repeat such that each observation acts once as the “test set”.
9.3. SCORING RULES FOR PROBABILISTIC PREDICTIONS 127
Suppose our outcome is binary Yi ∈ {0, 1} and we consider predictions for observations in the
validation set. Let µb i be the predicted value of P(Yi = 1) from our [Link] do we assess the
accuracy of our predictions? We need some way to compare the prediction µb i with the observed
outcome yi .
Weather forecasters never give a deterministic forecast of future weather. They always use
probabilities. Suppose we aggregate all predictions together that have the same forecast
probability of rain.
Forecast (in %) 10 20 30 40 50 60 70 80 90
Days 10 40 105 192 282 200 116 44 11
Rainy Days 3 6 30 76 120 132 70 38 8
% Rainy Days 30 15 29 40 43 66 60 86 73
A calibration plot shows how good the probabilistic predictions are. Each point represents
a group of days with the same forecast probability of rain. For each group we calculate the
observed frequency of rain and compare it with the probabilistic prediction from the model.
Well calibrated predictions should fall on the diagonal line.
128 CHAPTER 9. OVERDISPERSION, PREDICTION AND CLASSIFICATION
100
Frequency of rain (%)
60
20
0
0 20 40 60 80 100
Example: The weather forecast says the probability of rain tomorrow is 1%. But it does rain
tomorrow. The purpose of a scoring rule is to assess the quality of the probabilistic predictions.
A scoring rule penalizes the mismatch between prediction µb i = 0.01 and observation y1 = 1.
In this module we consistently use the notation µi to represent the expectation for response of
the ith unit of observation under the model.
• We need to consider what happens when the model is not true.
• Hence we need a new notation for P(Yi = 1) distinct from µi .
• For binary Yi we use πi to represent the true-but-unknown probability that Yi = 1.
• We call π1 , . . . , πn the out-of-model probabilities.
A scoring rule is
• proper if Eπ [S(Y , µ)] is minimized over µ when µi = πi for i = 1 . . . n.
• strictly proper if no other values of µ attain the minimum.
In this context, the expectation Eπ is the out-of-model expectation with P(Yi = 1) = πi .
N
= (yi − µi )2 .
X
S(y, µ)
i=1
9.3. SCORING RULES FOR PROBABILISTIC PREDICTIONS 129
It is the sum of squares of the response residuals, and is equal to the deviance for a Gaussian
outcome variable.
Example The Brier score in the weather example is equal to 227.88.
Proof that Brier score is a proper scoring rule:
We have
N h i
Eπ [S(y, µ)] = Eπ (Yi −πi + πi − µi )2
X
i=1
N h i
= Eπ (Yi − πi )2 + 2(Yi − πi )(πi − µi ) + (πi − µi )2
X
i=1
N N
= πi (1 − πi ) + (µi − πi )2
X X
i=1 i=1
The log score is equal to minus the log likelihood for a binary outcome:
∂Eπ (S) πi 1 − πi
= − +
∂µi µi 1 − µi
µi − π i
= .
µi (1 − µi )
∂Eπ (S)
= 0,
∂µi
However, sometimes we are not able to find individuals with a high probability of being positive.
The figure below comes from a model to predict credit defaults and shows the distribution of
fitted probability of default (µb i ) over the individuals in the data set.
• Very few individuals have high predicted risk of default.
• We can see three clusters: low, medium, and high risk.
^
Histogram of µ
1000 1500 2000
Frequency
500
0
the classifier can be summarized by two conditional probabilities. The statistics we use depend
on what we condition on. We should be extremely skeptical if you hear reports that a classifier
is “90% accurate”.
We will use this simple example of a 2 × 2 table to illustrate the calculations. A table like this
is referred to as a confusion matrix.
Choosing the denominator to be the true status we obtain the following two conditional
probabilities:
• Sensitivity:
P(Predict + | True +) = 90/100 = 0.9.
• Specificity:
P(Predict − | True −) = 70/100 = 0.7.
9.4. PREDICTION VERSUS CLASSIFICATION 133
Choosing the denominator to be the predicted status we obtain another set of conditional
probabilities:
• Positive predictive value (PPV)
Note that the PPV depends on sensitivity, specificity and the proportion of positive individuals
in the population.
A hybrid approach is used in machine learning using precision, which is identical to the PPV,
and recall, which is identical to sensitivity. As an example consider the confusion matrix:
Then,
• Precision (= PPV)
• Recall (= Sensitivity)
[Link] F1 score
In machine learning, the F1 score sometimes used to evaluate predictive accuracy:
1 1 1 1
= +
F1 2 Precision Recall
[Link] Fβ score
A generalization of the F1 score is
1 1 1
!
β2
= +
Fβ 1 + β2 Precision Recall
1.0
0.8
0.6
Sensitivity
0.4
0.2
0.0
Suppose the outcome is independent of all of our predictor variables. For the plot below a
predictor variable was simulated independent of the outcome and then a model was fitted to
the data.
1.0
0.8
0.6
Sensitivity
0.4
0.2
0.0
Such a classifier appears as a diagonal line on an ROC plot. This is because what we predict is
independent of the true value.
Hence
Sometimes we can see that one classifier dominates another, if it always has higher specificity
for given sensitivity (and vice versa).
1.0
0.8
0.6
Sensitivity
0.4
0.2
with pay
without pay
0.0
The above plot shows the ROC curves for two models fitted to the credit default data. One model
uses all available predictors. For the second model we remove the most important predictor
variable pay from the model. Predictions from this reduced model (in blue) are always worse
than the full model (in red).
V (y) = y3.
138
10.1. THE SADDLEPOINT APPROXIMATION 139
The saddlepoint approximation is not exact for the Poisson distribution. However, we can
obtain the saddlepoint approximation applying Stirling’s formula:
√
y! ≈ 2πy exp(−y)y y
M (t) = (1 − 2ϕt)−1/2
140 CHAPTER 10. FURTHER TOPICS
1
!
Z
d
= exp − exp td dy
2ϕ
q
2πϕV (y)
1
!
Z
d
= exp − ∗ dy
2ϕ
q
2πϕV (y)
where
ϕ
ϕ∗ = .
1 − 2tϕ
Hence
1
s !
ϕ∗ Z d
M (t) = exp − ∗ dy
2ϕ
q
ϕ 2πϕ∗ V (y)
s
ϕ∗ Z
= e | µ, ϕ∗ )dy
p(y
ϕ
s
ϕ∗
=
ϕ
1
s
=
(1 − 2tϕ)
This implies that the absolute value of the standardized deviance residual has a half-normal
distribution. v
u d(Yi , µ bi)
u
(sd)
Ri = t ∼ N+ (0, 1).
(1 − h b )ϕ
i
b
When the saddlepoint approximation holds, we can compare the absolute value of the standard-
(sd)
ized deviance residuals ri with the quantiles of the half-normal distribution.
(sd)
Note that Ri does not, in general, have a normal distribution. We know that the deviance
residuals do not have expectation zero.
10.2.3 Limitations
We cannot use the residual deviance to measure goodness of fit when the saddlepoint approxi-
mation does not apply, e.g. for binary outcomes.
We cannot use it for EDMs with a free dispersion parameter that must be estimated, e.g. normal,
gamma, inverse Gaussian.
142 CHAPTER 10. FURTHER TOPICS
Then n
Wi g ′ (µi ) xi
!
E [U (β)] = E(Yi − µi ) =
X
0.
i=1 ϕ
Now,
∂U (β) n
Wi g ′ (µi )xi
! !
∂µi
=
X
E −
∂β i=1 ϕ ∂β
n
Wi g ′ (µi )xi xTi
!
=
X
i=1 ϕ g ′ (µi )
n
Wi xi xTi
=
X
i=1 ϕ
= I(β).
10.3. QUASI-LIKELIHOOD FOR GLMS 143
Furthermore,
!2
n
Wi g ′ (µi )
Var [U (β)] = xi xTi Var(Yi )
X
i=1 ϕ
n
Wi2 [g ′ (µi )]2 ϕV (µi )
! !
= xi xTi
X
i=1 ϕ2 wi
n
!
Wi
= xi xTi
X
i=1 ϕ
= I(β)
since
wi
Wi = .
V (µi ) [g ′ (µi )]2
U (β)
b = 0
E [U (β)] = 0.
1. Budworms are moth caterpillars that can destroy garden flowers. The output below shows
the results of fitting a model in R to a toxicology experiment where different doses of
insecticide was used to kill budworms. In addition to the dose ldose, the experiment also
recorded the sex of the budworm (M=male, F=female).
Call:
glm(formula = cbind(numalive, numdead) ~ -1 + sex + ldose, family = binomial,
data = budworm)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
sexF 3.4732 0.4685 7.413 1.23e-13 ***
sexM 2.3724 0.3855 6.154 7.56e-10 ***
ldose -1.0642 0.1311 -8.119 4.70e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The default link function for the binomial family is logit. So the mean µi is related to the
linear predictor by !
µi
log = ηi
1 − µi
144
10.3. QUASI-LIKELIHOOD FOR GLMS 145
To find the LD50 dose, which kills half the budworms, we want µi = 0.5. Hence
0.5
ηi = log = log (1) = 0.
0.5
Now we substitute the fitted values
αF = 3.4732,
β = −1.0642,
giving
ldosei = 3.4732/1.0642 = 3.26.
Thus a LD50 dose is 3.26 (units were not provided).
b. To calculate the probability of death we first calculate the linear predictor for male
budworms. (Note the use of the different intercept term αM ).
ηi = αM + β × ldosei
= 2.3724 − 1.0642 × 3.26
= −1.097.
Finally, we must remember that the expectation µi is the probability that individual
i is alive (When we use the two-column outcome variable for the binomial model, the
expectation is the probability of the first event). Hence, the predicted probability that
the LD50 dose for a female budworm will kill a make budworm is p = 1 − 0.25 = 0.75.
If you ever get confused about which probability you are modelling (death or survival) in
a toxicology model then remember that more poison kills more things. Look at the
sign of the dose term. If it is negative then you are modelling the probability of survival
(decreasing probability of survival with dose). If it is positive then you are modelling the
probability of death (increasing probability of death with dose).
146 CHAPTER 10. FURTHER TOPICS
2. Consider the following box plots of outcome Y against a numeric variable X that takes
integer values from 4 to 10.
a. Suggest an appropriate variance function for modelling these data.
b. Suggest an appropriate link function for modelling these data.
Justify your answer in both cases.
12
10
8
y
6
4
2
0
4 5 6 7 8 9 10
Solution: This kind of question tests your ability to formulate a GLM based on a summary of
the data, such as a graph or table. Your answer does not have to be definitive. Some flexibility
is allowed in your answer as long as you justify your reasoning.
a. The mean of Y appears to be increasing with X, and so does the variance. This suggests
that the variance function should be a power function, i.e. either V (µ) = µ (Poisson) or
V (µ) = µ2 (gamma) are appropriate. We can rule out V (µ) = µ3 (inverse Gaussian) as
this implies a very rapid increase in variance with the mean, which is not visible in the
graph.
You may have spotted that the variable Y is not discrete. You can see this from the
individual outlier points plotted for X = 4. This is not sufficient reason to rule out the
Poisson variance function V (µ) = µ because this is shared by the quasi-Poisson family
Var(Y ) = ϕµ for ϕ ̸= 1, and the quasi-Poisson can be applied to non-integer data.
b. We can see from the graph that Y ≥ 0 for all observations. All the whiskers from the box
plot are non-negative and there are no negative outliers. This suggests that we need a
link function appropriate for non-negative data such as the log link.
10.3. QUASI-LIKELIHOOD FOR GLMS 147
3. The data set esoph concerns a study of esophageal cancer conducted in the Ille-et-Vilaine
department of France in the 1970s. Participants have been classified by age group (agegp),
alcohol consumption in g/day (alcgp) and tobacco consumption in g/day (tobgp). The
categories are:
• agegp: 25-34, 35-44, 45-54, 55-64, 65-74, 75+ years
• alcgp: 0-39, 40-79, 80-119, 120+ g/day
• tobgp: 0-9, 10-19, 20-29, 30+ g/day
In each class defined by the cross-classification of these variables, ncases gives the number
of participants in the study with esophageal cancer and ncontrols gives the number of
healthy controls without cancer.
The output on the below shows the results of fitting a model to these data in R.
Call:
glm(formula = cbind(ncases, ncontrols) ~ agegp + alcgp + tobgp,
family = binomial(), data = esoph)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -6.8954 1.0859 -6.350 2.16e-10
agegp35-44 1.9809 1.1041 1.794 0.072786
agegp45-54 3.7763 1.0680 3.536 0.000407
agegp55-64 4.3352 1.0650 4.070 4.69e-05
agegp65-74 4.8964 1.0764 4.549 5.39e-06
agegp75+ 4.8265 1.1213 4.304 1.67e-05
alcgp40-79 1.4346 0.2501 5.737 9.63e-09
alcgp80-119 1.9807 0.2848 6.956 3.51e-12
alcgp120+ 3.6029 0.3850 9.357 < 2e-16
tobgp10-19 0.4381 0.2283 1.919 0.055039
tobgp20-29 0.5126 0.2730 1.878 0.060398
tobgp30+ 1.6410 0.3441 4.769 1.85e-06
∆ = Deviance1 − Deviance2
= 367.953 − 82.337
= 285.616
where model 2 is the larger model (with smaller deviance) and model 1 (here the null
model) is nested in model 2. Note that a GLM is nested in another, if both have the same
EDM, This has a χ2d distribution with
d = p2 − p1 = 12 − 1 = 11
degrees of freedom.
The question does not ask you to calculate the p-value, but it is clearly very small. A
difference of 285.6 on 11 degrees of freedom is a very large and highly significant difference.
c. This is a logistic regression model (binomial model with logit link). Hence for the predictor
variables, the coefficients are log odds ratios. To calculate the odds ratio we take the
exponential transformation
log OR = 1.6410
OR = exp(1.6410) = 5.16
The 100 × (1 − α)% confidence interval is calculated on the scale of the log odds ratio
4. Continuing with the same data set we look at analysis of deviance. The single-argument
form of the anova() function tests the effect of adding each predictor in sequential order,
starting with the null model and ending with the full model with predictors agegp, alcgp,
and tobgp.
Since there are 3 predictors this gives us 3 p-values.
Analysis of Deviance Table
Solution:
This is less of a typical exam question and more a revision question designed to reinforce your
understanding of the two different forms of anova() in R.
• In the first form, there is a single argument which is a fitted model. The anova() function
tests a nested set of models starting with the null model (no predictors, but using the
150 CHAPTER 10. FURTHER TOPICS
same EDM and link function) and adding terms sequentially as they are given in the
model formula until the full model is reached.
• All models use the same EDM and link function. The full model contains an intercept
term and so has the null model nested in it.
• In the second form of anova(), there are two arguments which are both fitted models.
The first model is the smaller model nested in the second larger model. This is because
the first model uses the same EDM, the same link function and its predictor is a subset of
the predictors of the second model.
The key to understanding this question is that rows 2 and 3 of the first anova() output
correspond to the models
cbind(ncases, ncontrols) ~ agegp
and
cbind(ncases, ncontrols) ~ agegp + alcgp
which are the two models compared in the second anova() output. This second anova() output
contains exactly the same quantities found in rows 2 and 3 of the first output, but in a
different order.
Only one calculation is required. This is for column Df, which is the difference in the residual
degrees of freedom (Resid. Df) between the two models: Df = 82 − 79 = 3.
With this information, all you need to do is work out the correspondence between the two tables
and then apply the same principles to the third, empty table, which corresponds to rows 3 and
4 of the first anova() output. The completed table is
Analysis of Deviance Table
5. The density of the scaled binomial distribution with mean µ ∈ (0, 1) and sample size
m > 0 is !
m
p(y | µ, m) = µmy (1 − µ)m(1−y)
my
Recall that for the scaled binomial, considered as an EDM, the dispersion parameter ϕ = 1
and the prior weight w = m.
Write down the log likelihood of the the scaled binomial and use this to give an expression
for the unit deviance in terms of y and µ. Pay attention to the special cases y = 0 and
y = 1.
Solution: This is a typical bookwork question. Starting with the canonical form
!
w(θy − b(θ))
p(y | θ, ϕ) = a(y, ϕ/w) exp
ϕ
Let
t(y, µ) = θy − b(θ).
Then the unit deviance is defined as
The first step is to put the log density into canonical form
where C(m, y) is a normalizing term not depending on µ which we do not need to specify. This
is recognizable as an EDM in canonical form with
!
µ
θ = log ,
1−µ
b(θ) = − log(1 − µ),
ϕ = 1,
w = m.
It is important to recognize that m is the weight and is not included in the expression for the
unit deviance, which depends only on y and µ.
The expression for t(y, µ) is
!
µ
t(y, µ) = y log + log(1 − µ)
1−µ
= y log(µ) + (1 − y) log(1 − µ).
152 CHAPTER 10. FURTHER TOPICS
Hence
The subtlety of unit deviance calculations is that the standard formula may not work on the
boundaries of the support of the distribution. For the binomial model, the above expression for
d(y, µ) contains terms in log(y) and log(1 − y) which are invalid for y = 0 and y = 1 respectively.
For the boundary cases we use the limiting formula
d(y, µ) = 2 lim
′
t(y, µ ) − t(y, µ) .
′
µ →y
For y = 0 we have
Hence
d(0, µ) = 2 0 − t(0, µ) = −2 log(1 − µ).
Similarly, for y = 1 we have
Hence
d(1, µ) = 2 0 − t(1, µ) = −2 log(µ).
An important consistency check when you derive an expression for the unit deviance is that it
should be zero when y = µ. This can be confirmed for all three expressions given above.