Logisticregression PDF
Logisticregression PDF
Logisticregression PDF
Examples:
ì Non - smoker
Whether or not a person Y =í
smokes Binary Response îSmoker
ìSurvives
Success of a medical Y =í
treatment îDies
60
65
Hgt
70
75
Ordinary linear regression is used a lot, and is
taught in every intro stat class. Logistic regression
is rarely taught or even mentioned in intro stats,
but mostly because of inertia.
We now have the computing power and
software to implement logistic regression.
π = Proportion of “Success”
In ordinary regression the model predicts the
mean Y for any combination of predictors.
What’s the “mean” of a 0/1 indicator variable?
å yi # of 1' s
y= = = Proportion of " success"
n # of trials
Goal of logistic regression: Predict the “true”
proportion of success, π, at any value of the
predictor.
Binary Logistic Regression Model
Y = Binary response X = Quantitative predictor
π = proportion of 1’s (yes,success) at any X
Equivalent forms of the logistic regression model:
Logit form Probability form
æ p ö e
β0 + β1 X
logç ÷ = b 0 + b1 X π =
è1- p ø β0 + β1 X
1+ e
What does this look like?
N.B.: This is natural log (aka “ln”)
Logit Function
no data Function Plot
1.0
0.8
0.6
y
0.4
0.2
-10 -8 -6 -4 -2 0 2 4 6 8 10 12
x
exp (bo + b1• x )
y=
1 + exp (bo + b1• x )
Binary Logistic Regression via R
> logitmodel=glm(Gender~Hgt,family=binomial,
data=Pulse)
> summary(logitmodel)
Call:
glm(formula = Gender ~ Hgt, family = binomial)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.77443 -0.34870 -0.05375 0.32973 2.37928
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 64.1416 8.3694 7.664 1.81e-14 ***
Hgt -0.9424 0.1227 -7.680 1.60e-14***
---
Call:
glm(formula = Gender ~ Hgt, family = binomial, data = Pulse)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 64.1416 8.3694 7.664 1.81e-14 ***
Hgt -0.9424 0.1227 -7.680 1.60e-14***
---
64.14 −0.9424 Ht
e
πˆ = 64.14 −.9424 Ht
1+ e
proportion of females at that
Hgt
> plot(fitted(logitmodel)~Pulse$Hgt)
> with(Pulse,plot(Hgt,jitter(Gender,amount=0.05)))
> curve(exp(64.1-0.94*x)/(1+exp(64.1-0.94*x)), add=TRUE)
1.0
jitter(Gender, amount = 0.05)
0.8
0.6
0.4
0.2
0.0
60 65 70 75
Hgt
Example: Golf Putts
Length 3 4 5 6 7
Made 84 88 61 61 44
Missed 17 31 47 64 90
Total 101 119 108 125 134
Deviance Residuals:
Min 1Q Median 3Q Max
-1.8705 -1.1186 0.6181 1.0026 1.4882
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.25684 0.36893 8.828 <2e-16 ***
Length -0.56614 0.06747 -8.391 <2e-16 ***
---
⎛ pˆ ⎞
log⎜ ⎟ vs. Length
⎝ 1− p ⎠
ˆ
1.5
1.0
Linear part of
logistic fit
logitPropMade
0.5
0.0
-0.5
3 4 5 6 7
PuttLength
Probability Form of Putting Model
1.0
e3.257 -0.566 Length
pˆ =
1 + e3.257 -0.566 Length
0.8
Probability Made
0.6
0.4
0.2
0.0
2 4 6 8 10 12
PuttLength
Odds
Definition:
p P(Yes)
= is the odds of Yes.
1 - p P( No)
p odds
odds = Ûp =
1- p 1 + odds
π odds
odds = ⇔π=
1− π 1+ odds
Fair die
Event Prob Odds
even # 1/2 1 [or 1:1]
X>2 2/3 2 [or 2:1]
roll a 2 1/6 1/5 [or 1/5:1 or 1:5]
1.0
e 0+1*x
π=
1 + e 0+1*x
0.8
π increases
0.6
by .231
Prob
x increases
0.4
by 1
by 1 a factor of 2.718
π increases by .072
0.0
-3 -2 -1 0 1 2 3
x
Odds
Logit form of the model:
⎛ π ⎞
log ⎜ ⎟ = β0 + β1 X
⎝ 1− π ⎠
p1 1− p 2
RR = OR = RR *
p2 1− p1
So when p is small, OR ≈ RR.
X is replaced by X + 1:
β0 + β1 X
odds = e
is replaced by
β0 + β1 ( X +1)
odds = e
So the ratio is
β0 + β1 ( X +1)
e β0 + β1 ( X +1)−( β0 + β1 X ) β1
β0 + β1 X
=e =e
e
Example: TMS for Migraines
Transcranial Magnetic Stimulation vs. Placebo
Pain Free? TMS Placebo
YES 39 22
NO 61 78
Total 100 100
data: datatable
X-squared = 6.8168, df = 1, p-value = 0.00903
> lmod=glm(cbind(Yes,No)~Group,family=binomial,data=TMS)
> summary(lmod)
Call:
Binary Logistic Regression
glm(formula = cbind(Yes, No) ~ Group, family = binomial)Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.2657 0.2414 -5.243 1.58e-07 ***
GroupTMS 0.8184 0.3167 2.584 0.00977 **
A Single Binary Predictor for a Binary Response
æ p ö
logç ÷ = b 0 + b1 X
è1- p ø
⇒ odds = e b 0 + b1 X
> summary(PuttModel)
Call:
glm(formula = Made ~ Length, family = binomial)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.25684 0.36893 8.828 <2e-16 ***
Length -0.56614 0.06747 -8.391 <2e-16 ***
---
Null deviance: 800.21 on 586 degrees of freedom
Residual deviance: 719.89 on 585 degrees of freedom
Two forms of logistic data
1. Response variable Y = Success/Failure or 1/0: “long
form” in which each case is a row in a spreadsheet
(e.g., Putts1 has 587 cases). This is often called
“binary response” or “Bernoulli” logistic regression.
2. Response variable Y = Number of Successes for a
group of data with a common X value: “short form”
(e.g., Putts2 has 5 cases – putts of 3 ft, 4 ft, … 7 ft).
This is often called “Binomial counts” logistic
regression.
> str(Putts1)
'data.frame': 587 obs. of 2 variables:
$ Length: int 3 3 3 3 3 3 3 3 3 3 ...
$ Made : int 1 1 1 1 1 1 1 1 1 1 ...
> longmodel=glm(Made~Length,family=binomial,data=Putts1)
> summary(longmodel)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.25684 0.36893 8.828 <2e-16 ***
Length -0.56614 0.06747 -8.391 <2e-16 ***
---
Null deviance: 800.21 on 586 degrees of freedom
Residual deviance: 719.89 on 585 degrees of freedom
> str(Putts2)
'data.frame': 5 obs. of 4 variables:
$ Length: int 3 4 5 6 7
$ Made : int 84 88 61 61 44
$ Missed: int 17 31 47 64 90
$ Trials: int 101 119 108 125 134
>
shortmodel=glm(cbind(Made,Missed)~Length,family=binomial,data=Putts2)
> summary(shortmodel)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.25684 0.36893 8.828 <2e-16 ***
Lengths -0.56614 0.06747 -8.391 <2e-16 ***
---
Null deviance: 81.3865 on 4 degrees of freedom
Residual deviance: 1.0692 on 3 degrees of freedom
Binary Logistic Regression Model
Y = Binary X = Single predictor
response
π = proportion of 1’s (yes, success) at any x
Equivalent forms of the logistic regression model:
æ p ö
Logit form logç ÷ = b 0 + b1 X
è1- p ø
b o + b1 X
Probability form e
p= b o + b1 X
1+ e
Binary Logistic Regression Model
Y = Binary X1,X2,…,X
X = Single
k = Multiple
predictor
response predictors
π = proportion of 1’s (yes,
at anysuccess)
x , x , …,
at xany x1 2 k
b o + b1 X 1 + b 2 X 2 +!+ b k X k
Probability form p = e
b o + b1 X 1 + b 2 X 2 +!+ b k X k
1+ e
Interactions in logistic regression
Consider Survival in an ICU as a function of
SysBP -- BP for short – and Sex
> intermodel=glm(Survive~BP*Sex, family=binomial, data=ICU)
> summary(intermodel)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.439304 1.021042 -1.410 0.15865
BP 0.022994 0.008325 2.762 0.00575 **
Sex 1.455166 1.525558 0.954 0.34016
BP:Sex -0.013020 0.011965 -1.088 0.27653
Lines are
very close
Prob of voting Yes
0.6
to parallel;
0.4
not a
significant
0.2
interaction
0.0
Accept.hat
Acceptance FALSE TRUE
0 18 7
1 7 23
18 + 23 = 41 correct out of 55
> with(MedGPA, table(Acceptance,Accept.hat))
Accept.hat
Acceptance FALSE TRUE
0 18 7
1 7 23