I am running a Poisson regression model in R structured the following way:
mod <- glm(outcome ~ X + A + B + C + offset(log(time)), data = dat, family = poisson(link = "log"))
Where X
is my 2-level factor of interest and A
, B
, and C
are additional covariates.
How can I extract rate estimates and 95% CIs for each level of X
from mod
?
My anticipated output would be: Estimated rate for X_level1 (95% CI) Estimated rate for X_level2 (95% CI)
Thank you in advance!
Presumably you mean the marginal effect of X
on outcome
, that is, the estimated value of outcome
at each of the two levels of X
when all the other covariates are held at their mean values. To do this you could use the emmeans
package:
library(emmeans)
mod <- glm(outcome ~ X + A + B + C + offset(log(time)),
data = dat, family = poisson(link = "log"))
result <- as.data.frame(emmeans(mod, specs = 'X'))
result <- cbind(result[1], exp(result[2]),
CI = paste(round(exp(result[[5]]), 3),
round(exp(result[[6]]), 3), sep = '-'))
Giving you
result
#> X emmean CI
#> 1 level1 107.5459 104.587-110.589
#> 2 level2 147.1999 143.957-150.516
Data used
set.seed(1)
dat <- data.frame(X = factor(sample(c('level1', 'level2'), 100, TRUE)),
A = sample(100)/5,
B = sample(100)/2,
C = sample(100)/4,
time = 1:100)
dat$outcome <- with(dat, rpois(100, 50 * as.numeric(X) - A + B - C + time))
head(dat)
#> X A B C time outcome
#> 1 level1 5.6 45.5 19.50 1 85
#> 2 level2 9.6 15.5 9.25 2 94
#> 3 level1 6.6 36.0 10.75 3 94
#> 4 level1 9.0 18.0 20.25 4 43
#> 5 level2 4.2 24.5 0.25 5 137
#> 6 level1 6.2 20.5 22.50 6 31
Created on 2023-03-23 with reprex v2.0.2