library(tidyverse)
library(tidymodels)
library(kableExtra)
library(randomForestSRC) # for pbc data
data(pbc, package = "randomForestSRC")Lecture 20: Logistic Regression Continued
BIOS 600 - Spring 2026
Announcements
- Practice problems today and Thursday
- Solutions will be posted after Thursday’s class
- Please reach out to me/HyungGyu/tutors with any questions on material leading up to the final
Announcements
Final Exam is Tuesday, May 5, 8am-11am in this room (McG 1305).
Final Exam will be similar length to previous exams, and will focus on material since Exam 02.
Course evaluation
- Please fill out the course evaluations
Computational setup
Quick review
Which R function do we use to run a linear regression? How about for a logistic regression?
What additional piece of information do you need to specify when running a logistic regression, compared to a linear regression?
Suppose you have an outcome variable \(Y\) that takes on one of two values, 0 or 1. What distribution does \(Y_i\) have (one observation)? How about \(Y_1, \ldots, Y_n\) (\(n\) observations)?
What is the logit function? (describe in words & symbols)
(Fill in the blank:) Negative logits correspond to probabilities less than ______.
Logistic regression from last time
Recall the cirrhosis dataset from last class.
Researchers are interested in predicting whether a patient died (status) based on the following variables:
ascites: whether the patient had ascites (abnormal fluid buildup in the abdomen) (1 = yes, 0 = no)bili: serum bilirubin in mg/dL (higher values may indicate liver damage/disease)stage: histologic stage of disease (ordinal categorical variable with stages 1, 2, 3, and 4)
Logistic regression model
fit2 <- glm(status ~ ascites + bili + as.factor(stage),
data = pbc,
family = "binomial")
tidy(fit2) |>
kable(digits = 3)| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | -3.143 | 1.054 | -2.981 | 0.003 |
| ascites | 2.868 | 1.067 | 2.689 | 0.007 |
| bili | 0.315 | 0.064 | 4.886 | 0.000 |
| as.factor(stage)2 | 1.251 | 1.095 | 1.142 | 0.253 |
| as.factor(stage)3 | 1.716 | 1.069 | 1.604 | 0.109 |
| as.factor(stage)4 | 2.167 | 1.075 | 2.015 | 0.044 |
Interpretation
\(\hat\beta_{\text{bili}}\): Each one mg/dL increase in bilirubin is associated with the odds of death being multiplied by exp(.315) = 1.37, holding ascites presence and ascites presence constant.
Another Equivalent Interpretation: A one mg/dL increase in bilirubin is associated with 37% higher odds of death, holding ascites presence and status constant.
Predicted probabilities
There is a one-to-one-relationship between \(p\) and \(logit(p)\). So, if we predict the log-odds, we can back-transform to get back to a predicted probability.
For instance, suppose a patient does not have ascites, has a bilirubin level of 5 mg/dL, and is a stage 2 patient. Their predicted log-odds are:
\[−3.14+0.31×5+1.25=−0.34\]
Thus, their predicted probability of dying is
\[\frac{\text{exp}(-0.34)}{1+\text{exp}(-0.34)} = 0.42\]
In R
# Example patient:
new_patient <- data.frame(
ascites = 0,
bili = 5,
stage = 2
)
# Get predicted log-odds (type = "link")
pred_logodds <- predict(fit2,
newdata = new_patient,
type = "link")
# Convert log-odds to odds
pred_odds <- exp(pred_logodds)
# Convert odds to probability
pred_prob <- pred_odds / (1 + pred_odds)Output
pred_logodds 1
-0.3180994
pred_prob 1
0.421139
Alternatively…
- We can also calculate the predicted probability directly using
predictby setting thetypeto be"response".
# Get predicted probability (type = "response")
pred_prob2 <- predict(fit2,
newdata = new_patient,
type = "response")
pred_prob2 1
0.421139
Application Exercise
Head to Canvas and download the template for Application Exercise 07. AE 07 will be due this Sunday at 11:59pm.
More practice
tidy(fit2) |>
kable(digits = 3)| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
| (Intercept) | -3.143 | 1.054 | -2.981 | 0.003 |
| ascites | 2.868 | 1.067 | 2.689 | 0.007 |
| bili | 0.315 | 0.064 | 4.886 | 0.000 |
| as.factor(stage)2 | 1.251 | 1.095 | 1.142 | 0.253 |
| as.factor(stage)3 | 1.716 | 1.069 | 1.604 | 0.109 |
| as.factor(stage)4 | 2.167 | 1.075 | 2.015 | 0.044 |
By hand, write out the following. Leave in terms of values given:
What is the predicted log odds of dying for a patient with ascites, a bilirubin level of 3 mg/dL, and stage 3?
Given this value, what is the predicted probability?
Hypothesis tests in logistic regression
Generally, we wish to know whether the OR = 1 or equivalently, whether the logit of \(p\) (a \(\beta\) coefficient) = 0. To test
\[H_0: \beta_k = 0\]
\[H_1: \beta_k \neq 0\]
we can compare the ratio of a parameter estimate to its standard error using the standard normal distribution
- (The reason we use Z instead of t is a bit technical, so we won’t go into it in class).
Confidence intervals
CIs for associations on the logit scale are given by
\[\hat \beta_j \pm z^*_{1-\alpha/2} \times \widehat {SE} (\hat\beta_j)\]
These are typically translated into confidence intervals in odds scale by exponentiating the lower and upper limits. That is,:
\[[\text{exp}(\hat \beta_j - z^*_{1-\alpha/2} \times \widehat {SE} (\hat\beta_j)), \text{exp}(\hat \beta_j + z^*_{1-\alpha/2} \times \widehat {SE} (\hat\beta_j))]\]
Connection between hypothesis tests and confidence intervals
If we wish to test \(H_0: \beta_k=\beta_0\), at \(\alpha=.05\), we can test this using a confidence interval
Check whether \(\beta_0\) is contained in a \((1-\alpha)100\%\) confidence interval
If not, reject \(H_0\)
For example, to test \(H_0: \beta_k=0\) at \(\alpha=.05\), check if 0 is contained in a 95% confidence interval
Why?
Recap
Logistic regression review
Interpreting continuous and categorical coefficients in a logistic regression model
Predicting probabilities using a logistic regression model object
Next class
- More review for final