2
$\begingroup$

I would like to calculate contrasts of predictive probabilities. In a nutshell, I would like to compute the difference in differences of probabilities. The code below illustrates exactly my problem.

Among men, I calculate the predictive probability of the event by smoking:

p1 = prob. of event|sex==1 and smoking == 1;

p2 = prob. of event|sex==1 and smoking == 0

Among women, I calculate the predictive probability of the event by smoking.

p3 = prob. of event|sex==0 and smoking == 1;

p4 = prob. of event|sex==0 and smoking == 0

Now, what I want to calculate is the (p4-p3)-(p2-p1) along with 95% CI. In the example below, the difference in differences would be around -0.06 [e.g., (0.44-0.5)-(0.52-0.52)]. How do we estimate the SE/95% CI for that difference in R?

library(ggeffects)
N = 100
set.seed(123456)
hypertension <- round(runif(N))
age <- rnorm(N,45,5)
sex <- round(runif(N))
smoking <- round(runif(N))
model <- glm(hypertension ~ age + sex + smoking +sex*smoking, family = "binomial")
summary(model)
ggpredict(model, c("smoking", "sex")) 

Thank you so much for any tips/suggestions.

All the best,

Jacob

$\endgroup$
0

1 Answer 1

1
$\begingroup$

The bootstrap will probably be your easiest route here. Something like

df = data.frame(hypertension, age, sex, smoking)

stat = function(dat, j) {
  dat = dat[j,]
  model <- glm(hypertension ~ age + sex + smoking +sex*smoking,
    family = "binomial", data = dat)
  pred <- ggpredict(model, c("smoking", "sex"))
  pred <- pred$predicted    
  (pred[4] - pred[2]) - (pred[3] - pred[1]) # double-check the indexing here!
}

library(boot)
br <- boot(df, stat, R = 1000)
boot.ci(br)

would work. By the way, I think the ggpredict call is something of a bottleneck in the above code, so I would write your own function to compute the predictions (preferably using predict(model, ...) to handle the interactions).

$\endgroup$
3
  • $\begingroup$ Elegant solution, indeed. However, are there any alternatives such as delta-method-based approaches? I would like to explore some aspects via simulation too. $\endgroup$
    – Jacob
    Commented Jul 24, 2021 at 21:23
  • $\begingroup$ @Jacob You can use the delta method, sure. Just write out this difference-in-differences as a function of the coefficients and then take the gradient. However, that's going to be a lot of analytical work compared to the bootstrap. $\endgroup$
    – Cat
    Commented Jul 24, 2021 at 22:05
  • $\begingroup$ Fantastic suggestions and explanations. Thank you. $\endgroup$
    – Jacob
    Commented Jul 25, 2021 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.