Given these data, reflecting individual data on 206 subjects, two treatment groups ("uc" and "texting") and race ("nonblack" and "black").
My goal is a calculate a risk ratio (with 95% CI) for each treatment group. The risk ratio of interest is defined as the proportion with outcome == 1 in the 'nonblack' category divided by the proportion with outcome == 1 in the 'black' category. How do I calcuate these in R using a logistic (or Poisson model)?
agg_dat <- structure(list(trt_grp = structure(c(2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L), levels = c("texting", "uc"), class = "factor"), race = structure(c(2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L), levels = c("black", "nonblack"), class = "factor"),
ascertained = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels = c("yes",
"no"), class = "factor"), counts = c(21, 9, 24, 49, 32, 3,
63, 5)), class = "data.frame", row.names = c(NA, -8L))
Converting to a row for each individual and setting factor levels:
dat <- uncount(agg_dat, counts) |>
mutate(outcome = ifelse(ascertained == "yes", 1, 0)) |>
mutate(race = factor(race, levels = c("black", "nonblack")),
trt_grp = factor(trt_grp, levels = c("uc", "texting")))
It's straightforward to get predictions for each category of trt_grp and race:
m0 <- glm(outcome ~ race*trt_grp, family = "binomial", data = dat)
(newdata <- agg_dat |>
select(trt_grp, race) |>
unique())
predictions <- predict(m0, newdata, type = "response")
I'm stuck on how to use my R glm() model to estimate the confidence intervals for these risk ratios.
The point estimates for the risk ratios can be calculated from model predictions (on the "response" scale).
(rr_uc <- predicted[1]/predicted[2])
(rr_texting <- predicted[3]/predicted[4])
I wish to calculate 95% confidence intervals for 'rr_uc' and 'rr_texting'. It seems like the R margins package might be useful.
Finally, I'd like to estimate the ratio of risk ratios, i.e., rr_texting/rr_uc. The point estimate is straightforward. How do I calculate the confidence interval for the ratio of risk ratios?
effectsize
package to convert from CIs of odds ratios (which are available directly) to CIs of risk ratios. Or you could compute risk ratios directly from the 2x2 tables (again usingeffectsize
) $\endgroup$