I'm wanting to know if there's an easy(ish) way in R to plot an odds ratio over the range of its continuous predictor? Obviously this would be a horizontal line normally with the y intercept at whatever the value of the OR is, but I'm interested in visualising the change in OR when specifying spline terms.
I've used the following code so far - plotting model-predicted logodds and odds is easy enough. But the only thing I can think of for the OR is to calculate the ratio between each successive pair of predicted odds (in a loop) and then plot that. There must be a simpler way?
library(tidyverse)
library(splines)
dat <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
head(dat)
# Run model
mod <- glm(admit ~ gre, data = dat, family = "binomial")
summary(mod)
# Create new df to predict on
newdat <- data.frame(gre = seq(0,800,1))
# Predict new fitted values and SE's on link scale
newdat <- cbind(newdat, pred_logodds = predict(mod, newdata = newdat, type = "link", se = F))
# Plot predicted logodds
ggplot(newdat, aes(x = gre, y = pred_logodds)) +
geom_line()
# Plot predicted odds
newdat <- newdat |>
mutate(pred_odds = exp(pred_logodds))
ggplot(newdat, aes(x = gre, y = pred_odds)) +
geom_line(size = 1)
# Plot predicted odds ratios?
# I then want to repeat the above with a model that now has a spline on the gre term
mod_spline <- glm(admit ~ ns(gre,5), data = dat, family = "binomial")
summary(mod_spline)
Does this work for calculating and plotting the odds ratio?
# Plot predicted odds ratios
library(data.table)
setDT(newdat)[, pred_odds_ratio := c(NA, pred_odds[-1] / pred_odds[-.N])]
ggplot(newdat, aes(x = gre, y = pred_odds_ratio)) +
geom_line(size = 1)
It seems relatively simple, and doesn't require a loop. You obviously get an NA for the first value because there's no previous one to compare to.
Something similar should then work for a model with a spline:
newdat[, pred_logodds_spline := predict(mod_spline, newdata = .SD, type = "link", se = F)]
newdat[, pred_odds_spline := exp(pred_logodds_spline)]
newdat[, pred_odds_ratio_spline := c(NA, pred_odds_spline[-1] / pred_odds_spline[-.N])]
ggplot(newdat, aes(x = gre, y = pred_odds_ratio_spline)) +
geom_line(size = 1)
Let me know if this helps, or is not what you meant.