# When to exponentiate: the mean of the chain or at every step in the chain?

I am interested in when it is best to exponentiate a difference in log-odds

Here is a sample problem in the stan language, three groups of forty binary observations, group 1 with hit probability = 0.2, group 2 with hit probability of 0.5, group 3 with hit probability of 0.8. I want to calculate the odds ratio for the comparison of group 1's hit rate to group 2's, but I am not sure when to exponentiate the difference in log odds.

# create data
df <- data.frame(group = factor(rep(letters[1:3],times=40)), hit = rbinom(120, 1, prob = c(.2,.5,.8)))

# put data into list
dList <- list(N = nrow(df),
nG = nlevels(df$$group), gIndex = as.integer(df$$group),
hit = df$hit) # model string ms <- " data{ int<lower=1> N; int<lower=1> nG; int<lower=1,upper=nG> gIndex[N]; int<lower=0,upper=1> hit[N]; } parameters{ vector [nG] a; } model{ vector [N] mod; a ~ normal(0,10); hit ~ binomial_logit(1, a[gIndex]); } " # generate chains fit <- stan(model_code = ms, data = dList, warmup = 1e3, iter = 2e3, chains = 1) # logistic regression output (in log odds) print(fit, pars = "a", probs = c(0.025, 0.975)) # mean se_mean sd 2.5% 97.5% n_eff Rhat # a[1] -2.29 0.02 0.55 -3.48 -1.32 892 1.01 # a[2] -0.12 0.01 0.31 -0.68 0.51 1047 1.00 # a[3] 1.81 0.02 0.47 0.98 2.82 889 1.00 # extract posterior parameter estimates df <- data.frame(rstan::extract(fit, pars = "a"))  So now in the df object we have three chains of estimated log-odds for each group. I want to calculate the odds ratio for the comparison of group 1 (a[1]) to group 2 (a[2]) # Method 1 Using the first method, we can calculate the difference in log odds by subtracting the estimated log odds in the second group from the estimated log odds in the first group for every row of the data.frame library(dplyr) df <- df %>% mutate(diff12 = a.1 - a.2)  Now we can take the mean of that chain of difference scores, and then exponentiate that value exp(mean(df$diff12))
# [1] 0.1131707


# Method 2

The other way to do it is to once again subtract the log odds estimates for group 2 from the log odds estimates from group 1, but to then exponentiate each of those difference scores and take the mean of those exponentiated scores, like so:

df <- df %>% mutate(e_diff12 = exp(a.1 - a.2))
mean(df\$e_diff12)
# [1] 0.1372593


As you can see the odds ratios are different using the two different methods, so which is the more correct way to do it?

Odds ratio (g1 vs g2) = odds(Y=1|g1)/odds(Y=1|g2) = exp(log(odds(Y=1|g1))-log(odds(Y=1|g2))) = exp(a1-a2).

First "=" comes from the definition of odds ratio.

Second "=" comes from x/y = exp(log(a)-log(b))

Third "=" comes from the definition of logistic regression.

For any 2 subjects A and B, let $$X_A$$ and $$X_B$$ are their values on covariates, then

OR(A vs B) =exp($$X_A\beta - X_B\beta)$$ = exp($$(X_A-X_B)\beta)$$

• Thank you user158565. So method 2 was the more correct? Aug 2, 2019 at 3:21
• Yes. Method 2 is correct (not more correct). Aug 2, 2019 at 3:23
• I am not sure your R code is correct. I am not familiar with R. But exp(a.1 - a.2) is correct. Aug 2, 2019 at 3:33