Logistic Regression Asssignment Solutions
Logistic Regression Asssignment Solutions
Solutions
David M. Rocke
35/15 85
= = 5.67
35/85 15
The odds of an MI are 5.67 times higher in a
diabetic individual than an individual with normal
blood glucose.
David M. Rocke Logistic Regression Assignment Solutions April 15, 2021 3 / 17
Write down the logistic regression model
formulation in detail for predicting MI from bg.
Specifically make sure you have defined the
coefficients in the model. Use “normal” as the base
level for the bg factor.
p = Pr(MI |bg )
p
ln = β0 + βmetdis xmetdis + βdiabetic xdiabetic
1−p
xmetdis = 1 iff bg = metdis
xdiabetic = 1 if bg = diabetic
βmetdis = log-odds ratio of metdis vs. normal
βdiabetic = log-odds ratio of diabetic vs. normal
David M. Rocke Logistic Regression Assignment Solutions April 15, 2021 4 / 17
Derive the likelihood equation for the model.
1
`−1 (x) =
1 + exp(−x)
p0 = `−1 (β0 )
p1 = `−1 (β0 + βmetdis )
p2 = `−1 (β0 + βdiabetic )
120 35
L(β0 , βmetdis , βdiabetic ) = p0 (1 − p0 )85
35
80 30
× p1 (1 − p1)50
30
50 35
× p2 (1 − p2 )15
35
David M. Rocke Logistic Regression Assignment Solutions April 15, 2021 5 / 17
Derive the maximum likelihood estimates for the
parameters of the model, using normal as the
default level.
p0 = 35/120 = 0.2917
p1 = 30/80 = 0.375
p2 = 35/50 = 0.70
β0 = log[35/85] = −0.8873
β0 + β1 = log[30/50] = −0.5108
β0 + β2 = log[35/15] = 0.8473
β1 = −0.5108 − (−0.8873) = 0.3765
β2 = 0.8473 − (−0.8873) = 1.7346
David M. Rocke Logistic Regression Assignment Solutions April 15, 2021 6 / 17
> logistic.example
bg mi non.mi
1 norm 35 85
2 metdis 30 50
3 diabetic 35 15
> logistic.example.mi
[,1] [,2]
[1,] 35 85
[2,] 30 50
[3,] 35 15
Call:
glm(formula = logistic.example.mi ~ bg, family = binomial, data = logistic.example)
Deviance Residuals:
[1] 0 0 0
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.8873 0.2008 -4.418 9.96e-06 ***
bgmetdis 0.3765 0.3061 1.230 0.219
bgdiabetic 1.7346 0.3682 4.711 2.47e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
p0 = 35/120
p1 = 30/80
p2 = 35/50
120 35
L = p0 (1 − p0 )85
35
80 30
× p (1 − p1)50
30 1
50 35
× p2 (1 − p2 )15
35
> logLik(logistic.example.glm)
’log Lik.’ -7.015692 (df=3)
The deviance is zero since this is a saturated model.
David M. Rocke Logistic Regression Assignment Solutions April 15, 2021 10 / 17
If the three parameters are β0 (the intercept),
βmetdis , and βdiabetic in that order, and if the
covariance matrix of the parameters is
!
0.04034 −0.04034 −0.04034
−0.04034 0.09367 0.04034
−0.04034 0.04034 0.13557
Call:
glm(formula = logistic.example.mi ~ bg, family = binomial, data = logistic.example)
Deviance Residuals:
[1] 0 0 0
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.8873 0.2008 -4.418 9.96e-06 ***
bgmetdis 0.3765 0.3061 1.230 0.219
bgdiabetic 1.7346 0.3682 4.711 2.47e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> print(ll2)
[1] -19.36386
> pchisq(x2,2,lower=F)
[1] 4.337673e-06
> drop1(logistic.example.glm,test="Chisq")
Single term deletions
Model:
logistic.example.mi ~ bg
Df Deviance AIC LRT Pr(>Chi)
<none> 0.000 20.031
bg 2 24.696 40.728 24.696 4.338e-06 ***