# COMMunicationRESearch.NET

### Site Tools

logistic_regression

# Logistic Regression

Logistic Regression Tutorial

$$y = b_{0} + b_{1}x$$
$$p = \frac{1} {1 + e^{-y}}$$
$$ln(\frac{p}{1-p}) = b_{0} + b_{1}x$$

Logistic Regression Details Pt1: Coefficients

Logistic Regression Details Pt 2: Maximum Likelihood

Logistic Regression Details Pt 3: R-squared and p-value

# e.g. 1

mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
## view the first few rows of the data
head(mydata)
  admit gre  gpa rank
1     0 380 3.61    3
2     1 660 3.67    3
3     1 800 4.00    1
4     1 640 3.19    4
5     0 520 2.93    4
6     1 760 3.00    2
summary(mydata)
     admit             gre             gpa             rank
Min.   :0.0000   Min.   :220.0   Min.   :2.260   Min.   :1.000
1st Qu.:0.0000   1st Qu.:520.0   1st Qu.:3.130   1st Qu.:2.000
Median :0.0000   Median :580.0   Median :3.395   Median :2.000
Mean   :0.3175   Mean   :587.7   Mean   :3.390   Mean   :2.485
3rd Qu.:1.0000   3rd Qu.:660.0   3rd Qu.:3.670   3rd Qu.:3.000
Max.   :1.0000   Max.   :800.0   Max.   :4.000   Max.   :4.000  
sapply(mydata, mean)
sapply(mydata, sd)
> sapply(mydata, mean)
0.3175 587.7000   3.3899   2.4850
> sapply(mydata, sd)
0.4660867 115.5165364   0.3805668   0.9444602
> 
xtabs(~admit + rank, data = mydata)
> xtabs(~admit + rank, data = mydata)
rank
0 28 97 93 55
1 33 54 28 12
mydata$rank <- factor(mydata$rank)
mylogit <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
summary(mylogit)
Call:
glm(formula = admit ~ gre + gpa + rank, family = "binomial",
data = mydata)

Deviance Residuals:
Min       1Q   Median       3Q      Max
-1.6268  -0.8662  -0.6388   1.1490   2.0790

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.989979   1.139951  -3.500 0.000465 ***
gre          0.002264   0.001094   2.070 0.038465 *
gpa          0.804038   0.331819   2.423 0.015388 *
rank2       -0.675443   0.316490  -2.134 0.032829 *
rank3       -1.340204   0.345306  -3.881 0.000104 ***
rank4       -1.551464   0.417832  -3.713 0.000205 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 499.98  on 399  degrees of freedom
Residual deviance: 458.52  on 394  degrees of freedom
AIC: 470.52

Number of Fisher Scoring iterations: 4

> 
> confint(mylogit)
Waiting for profiling to be done...
2.5 %       97.5 %
(Intercept) -6.2716202334 -1.792547080
gre          0.0001375921  0.004435874
gpa          0.1602959439  1.464142727
rank2       -1.3008888002 -0.056745722
rank3       -2.0276713127 -0.670372346
rank4       -2.4000265384 -0.753542605
> 
> ## CIs using standard errors
> confint.default(mylogit)
2.5 %       97.5 %
(Intercept) -6.2242418514 -1.755716295
gre          0.0001202298  0.004408622
gpa          0.1536836760  1.454391423
rank2       -1.2957512650 -0.055134591
rank3       -2.0169920597 -0.663415773
rank4       -2.3703986294 -0.732528724
wald.test(b = coef(mylogit), Sigma = vcov(mylogit), Terms = 4:6)
l <- cbind(0, 0, 0, 1, -1, 0)
wald.test(b = coef(mylogit), Sigma = vcov(mylogit), L = l)