Solved – Computing Confidence Intervals for Odds Ratio Based on Fitted Values

confidence intervallogisticr

I am trying to figure out how to get confidence intervals for odds ratios that were computed based on model fitted values from a logistic regression.

Here's an example with the basic idea:

#Load packages
library(vcd)
library(dplyr)
data(Arthritis)

#Create binary outcome for logistic regression
Arthritis$Y <- ifelse(Arthritis$Improved=="None", 0,1)

#Fit Model
fit <- glm(Y ~ Sex + Treatment, family=binomial, data=Arthritis)

#Create small set to predict on, get fitted values, SEs, and odds
df <- expand.grid(Sex=levels(Arthritis$Sex), 
                  Treatment=levels(Arthritis$Treatment)) %>%
  mutate(link=predict(fit, newdata=., type="link"),
         se=predict(fit, newdata=., type="link", se.fit=T)$se.fit,
         odds=exp(link))

#Odds ratio with 'Female, Placebo' as referent
df$OR <- df$odds/filter(df, Sex=="Female" & Treatment=="Placebo")$odds

Can I compute a confidence interval for the odds ratios? I've seen other responses showing how I can get a CI for the fitted estimates, but not for ratios based on those estimates. Thanks.

Best Answer

As I've commented at Stack Overflow, you could bootstrap them:

library(boot)
set.seed(42)
bootres <- boot(Arthritis, function(DF, i) {
  fit <- glm(Y ~ Sex + Treatment, family=binomial, data=Arthritis[i,])
  df <- expand.grid(Sex=levels(Arthritis$Sex), 
                    Treatment=levels(Arthritis$Treatment)) %>%
    mutate(link=predict(fit, newdata=., type="link"),
           se=predict(fit, newdata=., type="link", se.fit=T)$se.fit,
           odds=exp(link))
  df$odds/filter(df, Sex=="Female" & Treatment=="Placebo")$odds

}, R = 1000, strata = interaction(Arthritis$Treatment, Arthritis$Sex))

apply(bootres$t, 2, quantile, probs = c(0.025, 0.5, 0.975))
#      [,1]       [,2]      [,3]      [,4]
#2.5%     1 0.06236299  2.397555 0.3620032
#50%      1 0.21591348  6.295761 1.4285296
#97.5%    1 0.65358623 20.641313 5.2671106

I show stratified bootstrap here, but you get very similar results for non-stratified bootstrap.

Related Question