remmeansr-marginaleffects

Interaction risk ratio from logistic regression in marginaleffects package in R


I'm have a logistic regression model predicting interest (0=no interest, 1=interest) with randomised experimental condition (0=control, 1=experimental), sex, sex:condition interaction, and several other covariates included as predictors.

Using marginaleffects package in R, I need to work out two things:

  1. Sex-specific risk ratio (RR) for the effect of the experimental condition on interest (marginalising over other covariates).
  2. The interaction term (i.e., ratio of risk ratios: RRmen / RRwomen) for how many times greater the RR was for men than women.

Here is the logistic regression model:

model <- glm(data=dt, formula=interest ~ condition*sex + other_covariates,
              family=binomial(link="logit"))

I have successfully completed objective (1) — calculating the sex-specific marginal risk ratios (RRmen & RRwomen) — using the following:

marginaleffects::avg_comparisons(model, comparison="ratio",
                variables="condition",by="sex")

However, I can't seem to figure out how to calculate (2) — the ratio of risk ratios (RRmen / RRwomen) — alongside its associated confidence intervals.

Any help would be greatly appreciated. If this is more straightforward to do using another package like emmeans I'm also happy to use that. Thanks!


Solution

  • You can use the hypothesis argument to conduct (non)linear tests on any quantity generated by the marginaleffects package. See the very detailed tutorial here:

    https://marginaleffects.com/vignettes/hypothesis.html

    You do not provide a minimal working example, so I can't provide a fully working solution, but your code could end up looking like this:

    avg_comparisons(model, 
       comparison="ratio",
       variables="condition",
       by="sex",
       hypothesis = "b1 / b2 = 1")
    

    Note that I have only added the last argument, telling marginaleffects that we want to check if the ratio of the first estimate to the second is different from 1. When I say "first" estimate, I mean the first row in the output of the same command if you just remove the hypothesis argument.