A Generalized Linear Model for Bernoulli Response Data
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 1 / 46 Consider the Gauss-Markov linear model with normal errors: y = X + ✏, ✏ N(0, 2I). ⇠ Another way to write this model is
2 i = 1,...,n, y N(µ , ),µ= x0 , 8 i ⇠ i i i
and y1,...,yn are independent.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 2 / 46 This is a special case of what is known as a generalized linear model.
Here is another special case:
i = 1,...,n, y Bernoulli(⇡ ), 8 i ⇠ i
exp(xi0 ) ⇡i = , 1 +exp(xi0 )
and y1,...,yn are independent.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 3 / 46 In each example, all responses are independent, and each response is a draw from one type of distribution whose parameters may depend on explanatory
variables through a linear predictor xi0 .
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 4 / 46 The second model, for the case of a binary response, is often called a logistic regression model.
Binary responses are common (success/failure, survive/die, good customer/bad customer, win/lose, etc.)
The logistic regression model can help us understand how explanatory variables are related to the probability of “success.”
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 5 / 46 Example: Disease Outbreak Study
Source: Applied Linear Statistical Models, 4th edition, by Neter, Kutner, Nachtsheim, Wasserman (1996)
In a health study to investigate an epidemic outbreak of a disease that is spread by mosquitoes, individuals were randomly sampled within two sectors in a city to determine if the person had recently contracted the disease under study.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 6 / 46 Response Variable
yi = 0 (person i does not have the disease)
yi = 1 (person i has the disease)
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 7 / 46 Potential Explanatory Variables
age in years
socioeconomic status
1 = upper 2 = middle 3 = lower
sector (1 or 2)
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 8 / 46 Questions of Interest
The potential explanatory variables and the response were recorded for 196 randomly selected individuals.
Are any of these variables associated with the probability of disease and if so how?
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 9 / 46 We will demonstrate how to use R to fit a logistic regression model to this dataset.
Before delving more deeply into logistic regression, we will review the basic facts of the Bernoulli distribution.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 10 / 46 y Bernoulli(⇡) has probability mass function ⇠
k 1 k ⇡ (1 ⇡) for k 0, 1 Pr(y = k)=f (k)= 2{ } ( 0 otherwise
Thus,
0 1 0 Pr(y = 0)=f (0)=⇡ (1 ⇡) = 1 ⇡ and 1 1 1 Pr(y = 1)=f (1)=⇡ (1 ⇡) = ⇡. Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 11 / 46 The variance of y is a function of the mean of y.
1 E(y)= kf (k)=0 (1 ⇡)+1 ⇡ = ⇡ · · Xk=0
1 E(y2)= k2f (k)=02 (1 ⇡)+12 ⇡ = ⇡ · · Xk=0
Var(y)=E(y2) [E(y)]2 = ⇡ ⇡2 = ⇡(1 ⇡)
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 12 / 46 The Logistic Regression Model
For i = 1,...,n, y Bernoulli(⇡ ), i ⇠ i where exp(xi0 ) ⇡i = 1 +exp(xi0 )
and y1,...,yn are independent.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 13 / 46 The Logit Function
The function ⇡ g(⇡)=log 1 ⇡ ✓ ◆ is called the logit function.
The logit function maps the interval (0, 1) to the real line ( , ). 1 1 ⇡ ⇡ is a probability, so log(1 ⇡ ) is the log(odds), where Pr(A) the odds of an event A 1 Pr(A). ⌘
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 14 / 46 Note that ⇡ g(⇡ )=log i i 1 ⇡ ✓ i ◆
exp(x0 ) 1 =log i 1 +exp(x0 ) 1 +exp(x0 ) i i
=log[exp(xi0 )] = xi0 .
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 15 / 46 Thus, the logistic regression model says that,
y Bernoulli(⇡ ), where i ⇠ i
⇡i log = x0 1 ⇡ i ✓ i ◆ In Generalized Linear Models terminology, the logit is called the link function because it “links” the mean of
yi (i.e., ⇡i) to the linear predictor xi0 .
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 16 / 46 For Generalized Linear Models, it is not necessary
that the mean of yi be a linear function of .
Rather, some function of the mean of yi is a linear function of .
For logistic regression, that function is
⇡i logit(⇡ ) = log = x0 . i 1 ⇡ i ✓ i ◆
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 17 / 46 When the response is Bernoulli or more generally, binomial, the logit link function is one natural choice. However, other link functions can be considered.
Some common choices (that are also available in R) include the following:
1
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 18 / 46 1 logit: ⇡ log = x0 . 1 ⇡ ✓ ◆ 2 probit: 1 (⇡)=x0 ,
1 where ( ) is the inverse of N(0, 1) CDF. ·
3 Complementary log-log (cloglog in R):
log( log(1 ⇡)) = x0 .
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 19 / 46 Although any of these link functions (or others) can be used, the logit link has some advantages when it comes to interpreting the results (as we will discuss later).
Thus, the logit link is a good choice if it can provide a good fit to the data.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 20 / 46 The log likelihood function for logistic regression is
n yi 1 y `( y)= log[⇡ (1 ⇡ ) i ] | i i i=1 Xn = [y log(⇡ )+(1 y )log(1 ⇡ )] i i i i i=1 Xn = [y log(⇡ ) log(1 ⇡ ) +log(1 ⇡ )] i{ i i } i i=1 Xn ⇡i = yi log +log(1 ⇡i) 1 ⇡i i=1 ✓ ◆ Xn = [y x0 log(1 +exp x0 )] i i { i } = Xi 1
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 21 / 46 For Generalized Linear Models, Fisher’s Scoring Method is typically used to obtain an MLE for , denoted as ˆ.
Fisher’s Scoring Method is a variation of the Newton-Raphson algorithm in which the Hessian matrix (matrix of second partial derivatives) is replaced by its expected value (-Fisher Information matrix).
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 22 / 46 For generalized Linear Models, Fisher’s scoring method results in an iterative weighted least squares procedure.
The algorithm is presented for the general case in Section 2.5 of Generalized Linear Models 2nd Edition (1989) by McCullough and Nelder.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 23 / 46 For sufficiently large samples, ˆ is approximately normal with mean and a variance-covariance matrix that can be approximated by the estimated inverse of the Fisher Information Matrix; i.e.,
1 ˆ · N( ,ˆI ( ˆ)) ⇠
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 24 / 46 Inference can be conducted using the Wald approach or via likelihood ratio testing as discussed in our course notes on likelihood-related topics.
For example, a Wald confidence interval for c0 with approximate coverage probability of 0.95 is given by
1 c0 ˆ 1.96 c ˆI ( ˆ)c ± 0 q
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 25 / 46 Interpretation of Logistic Regression Parameters
Let x =[x1, x2,...,xj 1, xj , xj+1,...,xp]0. Let x˜ =[x1, x2,...,xj 1, xj + 1, xj+1,...,xp]0.
In other words, x˜ is the same as x except that the jth explanatory variable has been increased by one unit.
Let ⇡ = exp(x0 ) and ⇡˜ = exp(x˜0 ) . 1+exp(x0 ) 1+exp(x˜0 )
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 26 / 46 The Odds Ratio
⇡˜ ⇡ ⇡˜ ⇡ =exp log 1 ⇡˜ 1 ⇡ 1 ⇡˜ 1 ⇡ ⇢ ✓ ◆ ⇡˜ ⇡ =exp log log 1 ⇡˜ 1 ⇡ ⇢ ✓ ◆ ✓ ◆ =expx˜0 x0 { } =exp(x + 1) x { j j j j} =exp . { j}
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 27 / 46 ⇡˜ ⇡ Thus, 1 ⇡˜ =exp( j)1 ⇡ . All other explanatory variables held constant, the
odds of success at xj + 1 are exp( j) times the odds of
success at xj.
This is true regardless of the initial value xj.
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 28 / 46 A one unit increase in the jth explanatory variable (with all other explanatory variables held constant) is associated with a multiplicative change in the odds of
success by the factor exp( j).
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 29 / 46 P L E E = 100 ( i - a) % ( ; p ; Uj )
U = # L E ( p ;) E ( 100 It a) % P ( exp ( ;) exp exp ;) )
If (L , U ) is a 100(1 ↵)% confidence interval for , j j j then
(exp(Lj), exp(Uj))
is a 100(1 ↵)% confidence interval for exp( ). j
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 30 / 46 Also, note that
exp(x0 ) 1 ⇡ = = 1 1 +exp(x0 ) + 1 exp(x0 ) 1 = . 1 +exp( x ) 0 Thus, if (L ,U ) is a 100(1 ↵)% confidence interval for j j x0 , then a 100(1 ↵)% confidence interval for ⇡ is 1 1 , . 1 +exp( L ) 1 +exp( U ) ✓ j j ◆
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 31 / 46 > d=read.delim("http://dnett.github.io/S510/Disease.txt") > head(d) Ianort THIS id age ses sector disease savings VARIABLE IN 1 1 33 1 1 0 1 LwtTHIS EXAMPLE . 2 2 35 1 1 0 1 33 6 1 1 0 0 4 4 60 1 1 0 1 5 5 18 3 1 1 0 6 6 26 3 1 0 0 > > d$ses=factor(d$ses) > d$sector=factor(d$sector)
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 32 / 46 > o=glm(disease˜age+ses+sector, + family=binomial(link=logit), + data=d)
> = aze SEI se=3 , 2) X , , Sester[1, > summary(o)
Call: glm(formula = disease ˜ age + ses + sector, family = binomial(link = logit), data = d)
LEARN ABOUT THESE LATER . WE Win Deviance- Residuals: Min 1Q Median 3Q Max -1.6576 -0.8295 -0.5652 1.0092 2.0842
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 33 / 46 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept)[email protected] -2.293933 0.436769 -5.252 1.5e-07 *** age 0.026991 0.008675 3.111 0.001862 ** ses2 0.044609 0.432490 0.103 0.917849 ses3 0.253433 0.405532 0.625 0.532011 sector2 1.243630 0.352271 3.530 0.000415 *** Test of --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for binomial family taken to be 1)
WE WILL LEARN ABOUT THIS LATER .
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 34 / 46 . NULL Moser : logit ( Hi ) = M ( IT ,=Tz= - . = Tn )
' Fuu Morsel : logit ( Hi ) = Xi @
- - 2 ) n i
Null deviance: 236.33 on 195 degrees of freedom Residual deviance: 211.22 on 191 degrees of freedom a AIC: 221.22 - Zld.E) n -5 -24=(5+45)lodi,=( Number of Fisher Scoring iterations: 3
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 35 / 46 > coef(o) (Intercept) age ses2 ses3 sector2 -2.29393347 0.02699100 0.04460863 0.25343316 1.24363036
' B ' § B' 3 $4 B's > round(vcov(o),3) VEr ( E) (Intercept) age ses2 ses3 sector2 (Intercept) 0.191 -0.002 -0.083 -0.102 -0.080 agebees -0.002 0.000 0.000 0.000 0.000 ses2 -0.083 0.000 0.187 0.072 0.003 ses3 -0.102 0.000 0.072 0.164 0.039 sector2 -0.080 0.000 0.003 0.039 0.124
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 36 / 46 { bs : Zech - 2llEb'D) extra }