0% found this document useful (0 votes)
81 views46 pages

26GeneralizedLinearModelBernoulliAnnotated PDF

Uploaded by

Farah Stat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views46 pages

26GeneralizedLinearModelBernoulliAnnotated PDF

Uploaded by

Farah Stat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

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:
2
y = X + ✏, ✏ ⇠ N(0, I).

Another way to write this model is

2
8 i = 1, . . . , n, yi ⇠ N(µi , ), µi = x0i ,

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:

8 i = 1, . . . , n, yi ⇠ Bernoulli(⇡i ),

exp(x0i )
⇡i = ,
1 + exp(x0i )
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 x0i .

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 ⇡)1 k
for k 2 {0, 1}
Pr(y = k) = f (k) =
0 otherwise

Thus,

Pr(y = 0) = f (0) = ⇡ 0 (1 ⇡)1 0


=1 ⇡

and
Pr(y = 1) = f (1) = ⇡ 1 (1 ⇡)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
X
E(y) = kf (k) = 0 · (1 ⇡) + 1 · ⇡ = ⇡
k=0

1
X
2
E(y ) = k2 f (k) = 02 · (1 ⇡) + 12 · ⇡ = ⇡
k=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, yi ⇠ Bernoulli(⇡i ),

where
exp(x0i )
⇡i =
1 + exp(x0i )

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
✓ ◆
⇡i
g(⇡i ) = log
1 ⇡i

exp(x0i ) 1
= log
1 + exp(x0i ) 1 + exp(x0i )

= log[exp(x0i )] = x0i .

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 15 / 46


Thus, the logistic regression model says that,

yi ⇠ Bernoulli(⇡i ), where
✓ ◆
⇡i
log = x0i
1 ⇡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 x0i .

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(⇡i ) = log = x0i .
1 ⇡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:

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
X
`( | y) = log[⇡iyi (1 ⇡i ) 1 yi
]
i=1
n
X
= [yi log(⇡i ) + (1 yi ) log(1 ⇡i )]
i=1
Xn
= [yi {log(⇡i ) log(1 ⇡i )} + log(1 ⇡i )]
i=1
Xn  ✓ ◆
⇡i
= yi log + log(1 ⇡i )
i=1
1 ⇡i
n
X
= [yi x0i log(1 + exp{x0i })]
i=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.,

ˆ ⇠· N( , Î 1 ( ˆ ))

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
q
c ± 1.96 c0 Î 1 ( ˆ )c

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.

exp(x0 ) exp(x̃0 )
Let ⇡ = 1+exp(x0 ) and ⇡
˜= 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 ⇡
= exp{x̃0
x }
0

= exp{(xj + 1) j xj 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
p; E Uj ) =
100 ( i -

a) %

# P( exp ( L ;) E
exp ( p ;) E
exp ( U
;) ) =
100 It a) %

If (Lj , Uj ) is a 100(1 ↵)% confidence interval for 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 ) exp(x0 ) +1
1
= .
1 + exp( x0 )

Thus, if (Lj ,Uj ) is a 100(1 ↵)% confidence interval for


x0 , then a 100(1 ↵)% confidence interval for ⇡ is
✓ ◆
1 1
, .
1 + exp( Lj ) 1 + exp( Uj )

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 31 / 46


> d=read.delim("http://dnett.github.io/S510/Disease.txt")

Lwt
> head(d) Ianort THIS
id age ses sector disease savings IN
VARIABLE
1 1 33 1 1 0 1 EXAMPLE
THIS .

2 2 35 1 1 0 1
3 3 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)
>
> summary(o)
X =
,
aze , Sester
SEI[1se=3
,
,
2)

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) -2.293933
[email protected]
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 @

lodid.
-
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
-24=(5+45)
-

Zl E) n -5

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' $4'
B's 3

> round(vcov(o),3)
VEr ( E) (Intercept) age ses2 ses3 sector2

agebees
(Intercept)

ses2
0.191 -0.002 -0.083 -0.102
-0.002
-0.083
0.000
0.000
0.000
0.187
0.000
0.072
-0.080
0.000
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 -

D)
2llEb' extra
}

)
¥%¥r%÷¥
" * " " " 1
" ⇒

> confint(o)
Waiting for profiling to be done...
2.5 % 97.5 %
(Intercept) -3.19560769 -1.47574975
age 0.01024152 0.04445014
ses2 -0.81499026 0.89014587
ses3 -0.53951033 1.05825383
sector2 ( 0.56319260
)<1.94992969
,

APPROXIMATE 95% CONFIDENCE


INTERVAL For Be .

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 37 / 46


FIT A REDUCED MODEL THAT EXCLUDES Socioeconomic

status ( 5€52 5€53 ) '

×=[}
,

1 age sector
, ,
> oreduced=glm(disease˜age+sector, ~ ~ -

+ family=binomial(link=logit),
+ data=d)
>
> anova(oreduced,o,test="Chisq")
Ho :p3=fy=o
Analysis of Deviance Table
Ho :fsesz=Bses3=O
Model 1: disease ˜ age + sector
Model 2: disease ˜ age + ses + sector
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 n -3 193 24dB)211.64 -2105.11=2 .ME ) -2L ,dEr)

2 M -
5 191 Zl ,=( E) 211.22 2 0.4193 0.8109 =p
.
VALUE

y ,
)
5-3
PM } 0.4193

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 38 / 46


SWITCH To REDUCED MODEL BECAUSE SES

NOT SIGNIFICANT
> o=oreduced
-
Nt -

> anova(o,test="Chisq")
[email protected]
Analysis of Deviance Table
Model: binomial, link: logit
-

zettel )

/ /
Response: disease

Terms added sequentially (first to last)


'D

)
Yann
Df Deviance Resid. Df Resid. Dev Pr(>Chi)
NULL 195 236.33
p( X. 312.013 )
[email protected]
age 1 12.013 194 224.32 0.0005283 ***
sector 1 12.677 193 211.64 0.0003702 ***

zedo.sk
,
) # ( PCXT ? 12.677 )
Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 39 / 46
> head(model.matrix(o))
(Intercept) age sector2
1 1 33 0
2 1 35 0
3 1 6 0
4 1 60 0
5 1 18 0
6 1 26 0
>
> b=coef(o)

E
> b
(Intercept) age sector2
-2.15965912 0.02681289 1.18169345

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 40 / 46


LIKELIHOOD BASED CONFIDENCE INTERVALS

> ci=confint(o)
Waiting for profiling to be done...
> ci
2.5 % 97.5 %
(Intercept) -2.86990940 -1.51605906
age 0.01010532 0.04421365
sector2 0.52854584 1.85407936

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 41 / 46


> #How should we interpret our estimate of
> #the slope coefficient on age?
> exp(b[2])

( K)
age
exp
1.027176
> #All else equal, the odds of disease are about 1.027
> #times greater for someone age x+1 than for someone
> #age x. An increase of one year in age is associated
> #with an increase in the odds of disease by about 2.7%.
> #A 95% confidence interval for the multiplicative
> #increase factor is
> exp(ci[2,])
2.5 % 97.5 %
1.010157 1.045206

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 42 / 46


> #How should we interpret our estimate of
> #the slope coefficient on sector?

( $3 )
> exp(b[3])
sector2 exp
3.25989
> #All else equal, the odds of disease are about 3.26
> #times greater for someone living in sector 2 than for
> #someone living in sector one.
> #A 95% confidence interval for the multiplicative
> #increase factor is
> exp(ci[3,])
2.5 % 97.5 %
1.696464 6.385816

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 43 / 46


> #Estimate the probability that a randomly
> #selected 40-year-old living in sector 2
> #has the disease. '

X. =
[ I
,
40
,
I
]
> x=c(1,40,1)
> 1/(1+exp(-t(x)%*%b)) I

[,1]
E)
'
I + expt -

x.
[1,] 0.5236198
> #Approximate 95% confidence interval
> #for the probability in question.
> sexb=sqrt(t(x)%*%vcov(o)%*%x) ← a
'
Etesx
> cixb=c(t(x)%*%b-2*sexb,t(x)%*%b+2*sexb)
> 1/(1+exp(-cixb))
[1] 0.3965921 0.6476635
±zWEx. ELITE '

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 44 / 46


> #Plot estimated probabilities as a function
> #of age for each sector.
>
> x=1:85
plot(x,1/(1+exp(-(b[1]+b[2]*x))),ylim=c(0,1),
type="l",col=4,lwd=2,xlab="Age",
ylab="Estimated Probability of Disease", cex.lab=1.3)
lines(x,1/(1+exp(-(b[1]+b[2]*x+b[3]))),col=2,lwd=2)
legend("topleft", legend=c("Sector 1","Sector 2"),
col=c(4,2),lwd=2)

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 45 / 46


1.0

Sector 1

Sector 2
0.8
Estimated Probability of Disease
0.6
0.4
0.2
0.0

0 20 40 60 80

Age

Copyright c 2017 Dan Nettleton (Iowa State University) Statistics 510 46 / 46

You might also like