kosukeimai / MatchIt

R package MatchIt
205 stars 43 forks source link

best way to estimate interactions? #191

Open michalovadek opened 6 months ago

michalovadek commented 6 months ago

first of all let me say that the documentation @ngreifer has written up is incredible, many thanks for this. I was wondering about the best way to estimate interaction terms. If treatments could be more than binary then the simplest approach would be to prepare the dataset as such (e.g. create a 4-level interaction variable from two binary ones). But given that this is not presently possible, I was wondering what the most fool proof way of estimating a treatment-covariate interaction effect is after matching. I'm assuming it's the following but thought I would check, as it doesn't explicitly feature in your otherwise super thorough guide to the package. And sorry if this is more of a marginaleffects question!

eff <- avg_comparisons(fit, variables = c("treat"), by = c("otherbinaryvar"),
                           vcov = ~cluster,
                           newdata = subset(m.data, treat == 1),
                           wts = "weights")

But this only produces two contrasts. Is it possible to obtain all four contrasts in one function call?

ngreifer commented 6 months ago

Thank you for the kind words! I'm a bit unclear on what you mean. Are you saying you have a 4-category treatment? Or a binary treatment and a binary moderator? Or a binary treatment and a 4-category moderator? Or a binary treatment and two binary moderators? And I mean what you actually have, not however you are thinking you might specify your models, but substantively what you want to test.

michalovadek commented 6 months ago

sorry I should have been clearer. In this instance I meant trying to test the simple interaction of a binary treatment and a binary moderator, but if the solution generalizes to multicategory moderators, that would be useful to know. In my specific case it actually doesn't matter which of the two variables you designate as treatment and which as moderator, which probably just adds confusion

ngreifer commented 6 months ago

If you have a binary moderator, wouldn't you expect to have two treatment effects, one for each level of the moderator, in which case you would expect to see exactly two contrasts? This is what you get when running the avg_comparisons() code you posted.

Also I am a bit concerned when you say it doesn't matter which variable is the treatment or moderator; matching can only balance the groups of one variable, so which is the treatment and which is the moderator matter quite a lot in terms of the interpretation of the effect.

michalovadek commented 6 months ago

I do now see that this was poorly thought through and worded on my end. Feel free to close, but if you want to indulge an extended discussion, I can sketch out more concretely what I had in mind:

Let's say we have two independent binary variables, race (B/W) and gender (M/F) and we are interested in their interaction. Ideally we could balance for each of the four possible conditions (BM, BF, WM, WF), but this is presently not possible. I would then want to obtain all six (not four, as I wrongly wrote above) unique contrasts (BM - BF, BM - WM, etc.) between the four conditions, ideally in one comparisons() call.

ngreifer commented 6 months ago

I see. Yeah, this isn't really a MatchIt question, it's a marginaleffects question. But I can answer it anyway :)

If you have your 4 groups defined by 2 binary variables, e.g., race and gender, and you have your outcome model fit as follows:

fit <- glm(y ~ race * gender * (x1 + x2), data = data)

you can run the following to get all pairwise comparisons between all combinations of race and gender:

avg_predictions(fit, variables = c("race", "gender"), hypothesis = "pairwise")

This produces the 6 contrasts as requested.

michalovadek commented 5 months ago

thanks a lot. So I take it there is presently no (hacky) way to get matched sets for the n groups (or n - 1)?

ngreifer commented 5 months ago

Here is a hacky way, and in a few months MatchIt will support multi-category treatments.