There are many examples on these forums of people calculating risk ratios from logistic regressions, but none of them seem to match my situation.
I have two predictor variables - one continuous and one binary. I'm interested in how the association between the continuous predictor and the outcome differs for group1 vs group2. I know this information is contained in the model coefficients, but I'd like to present the predicted risk ratio and confidence intervals for group1/group2 as a function of the continuous variable.
Here's some simulated data:
library(tidyr)
library(dplyr)
set.seed(42)
N <- 1000
dat <- data.frame(X1 = rnorm(N, 0, 1),
X2 = rbinom(N, 1, .5))
dat$Y <- 0.5*dat$X1 - 0.3*dat$X2 - (0.2*dat$X1*dat$X2) + rnorm(N, 0, 1)
dat$Y <- as.numeric(cut(dat$Y, breaks=c(-Inf, median(dat$Y), Inf)))-1
mod <- glm(Y~X1*X2, family='binomial', data=dat)
newdat <- expand.grid(X1 = seq(-3, 3, .25),
X2 = c(0, 1))
newdat$y_pred <- predict(mod, type='response', newdata = newdat)
newdat %>%
spread(X2, y_pred) %>%
mutate(relative_risk = `0`/`1`) -> newdat
Basically, I would like to know whether it is possible for me to obtain a confidence interval for that relative_risk
variable, and if so, how to compute it.
Bonus points if the method can be generalized to multilevel models.