Repository navigation
Description
Is your feature request related to a problem? Please describe
A popular statistical output in psychology and biology (and I am sure many other fields which I am less familiar with) is a series of p-values for the levels of an interaction effect of one categorical and one binary variable. Generally the idea is that a treatment is applied to a wide variety of entity categories, and the researcher is interested whether statements can be made which differentiate the effect of the treatment across categories.
Describe the solution you'd like
It would be really cool to have this as a method of the model fit class.
Describe alternatives you've considered
I have hacked this together for a number of scientific articles (1,1.1,2 --- these are self-contained examples, of which at least the first should be reliably reproducible), and while the code is short, afaict efficient, and certainty not spaghetti, it is not very generalizable. If you can walk me through it, I would be happy to try and contribute it.
Additional context
I do have a broader question which might also impact how this feature could be implemented. The way I have handled post-hoc t-tests thus far, is:
- Establish that the interaction term is significant in a model which includes both main effects and the interaction term (
value ~ binary*categorical). - Set up a second model which expresses the entire binary treatment effect per-category (
value ~ categorical + binary:categorical). - Set up an f-contrast of the
binary:categoricallevels, and ascertain that the significance is the same as obtained under1.--- I assume it always should be. - Set up t-tests (with
fit.t_test()) for all combinations of thebinary:categoricallevels. - Control for multiple comparisons
This is, however, insufficient. While this tells me in how far I can make meaningful statements about whether binary[T.True]:categorical[T.A] is different than binary[T.True]:categorical[T.B], it doesn't tell me whether a significant interaction effect might actually lead to the vanishing of the main binary treatment effect for that particular categorical variable. I could include additional t-tests for the individual terms, but then the f-contrast becomes an entirely different one than that of the interaction term in the initial model.
I'm unsure how to proceed here, should the t-tests between the interaction effects be pooled together with the t-tests for the significance of each individual interaction effect? Or should they be performed separately, along with separate multiple comparison corrections?
Activity
josef-pkt commentedon May 18, 2019
I didn't try to figure out exactly what you mean.
We have an interaction plot function based on data, and it would be very useful to have something similar as post-estimation result for models.
The closest this that we have right now is
t_test_pairwisewhich does all pairwise comparisons as a Wald t_test for the parameters of an estimated model.I uses the formula information in design_info to create the contrasts/restriction matrix for all pairwise comparisons.
Something similar could be applied to comparing the interaction effects, between continuous and discrete, or to other similar automatically constructed contrasts.
I don't know what information is in the
design_infofor this case.related
wald_test_termsis a bit similar but just for zero/no-effect contrasts which either uses formula information or string matching on column names to identify which columns have parameters equal to zero under the Null hypothesis.Where do you have the code that you already have?
josef-pkt commentedon May 18, 2019
I'm not sure about this part.
Sounds like you want to test the main effect under the presence of the interaction effect. There is no problems doing this as a Wald t-test, but many (R) statisticians argue against doing that.
For example, if you include only the interaction terms like binary[T.True]:categorical[T.B] and other T.x and the binary[T.False]:categorical[T.B] minus reference term or (minus constant) but no main effect, then the joint hypothesis that all those terms that contain
binary[T.True]are zero would be a test for the Null that the binary has no effect if we allow for arbitrary effect of the categorical variable.The joint test that all terms that contain categorical[T.x] are equal to the value of the reference category (or zero) would test that categorical has no effect allowing for arbitrary effect of the binary variable.
If we restrict to the specific case of binary * categorical, then it should be possible to construct tests for all interesting hypothesis as Wald tests from the full model.
(I just realized that in my quick reading for the previous comment, I assumed categorical*continuous interaction)
Aside: I'm not a big fan of sequential testing, like type 2 Anova, because the maintained hypotheses changes for each test and it is not really clear what the underlying assumptions for the different tests are. (AFAIK, historically they were just special cases or computation shortcuts when it is possible to construct interaction effects in an orthogonal way.)
TheChymera commentedon May 18, 2019
Sorry for being verbose and confusing, as usual :D
I have compiled for you a working example even more minimal than those linked above; here it is:
Outputting:
This is the way I am currently going about the task. The idea is, looking to describe how the Treatment and the Type effects interact, that I want to be able to make comparisons between both all of the pairwise combinations of the levels and the levels as they stand compared to zero. Particularly, given how the data is set up in this case (and this is quite important to note if it happens IRL): while there is a significant Treatment main effect, it vanishes for
Treatment[T.True]:Type[C].So:
josef-pkt commentedon May 18, 2019
sorry, I didn't see that you had several links in the first message, I only got the nature article
I will look also at those.
For the first two points at the end: I have to think about it. I don't see anything obviously strange
my guess is that there is currently no much simpler, less boiler plate version, but the point would be to add (semi-)automatic support for it. That's what t_test_pairwise, and wald_test_terms, try to do.
I'm pretty sure supporting this directly in statsmodels would be useful, we should support more commonly used "special" cases.
josef-pkt commentedon May 19, 2019
aside:
The restrictions for the t- or f-test can be defined as strings if you have the param names, e.g.
This uses patsy to convert string null hypothesis into the contrast/restriction matrix