Skip to content

MixedLM to test multiple longitudinal treatments for significant time effect #4787

Closed

Description

@TheChymera

Given this minimal self-contained example, I would be looking to determine:

  • whether there is a significant ANOVA effect for the treatment x day interaction,
  • and if so, to figure out which treatments in fact significantly differ from the control or from each other.

The data ins synthetic, so I know that the answers should be:

  • yes
  • and Substance3 differs from the control

This is also fairly obvious from the plot:

plot

Can I do this with statsmodels?
Currently I've gotten as far as deciding on the following modelling formula:

model = smf.mixedlm("value ~ treatment : day", df, groups='subject')

and I am getting the following fit summary:

               Mixed Linear Model Regression Results
===================================================================
Model:                 MixedLM     Dependent Variable:     value   
No. Observations:      168         Method:                 REML    
No. Groups:            12          Scale:                  0.1452  
Min. group size:       7           Likelihood:             -89.4977
Max. group size:       21          Converged:              No      
Mean group size:       14.0                                        
-------------------------------------------------------------------
                          Coef. Std.Err.   z    P>|z| [0.025 0.975]
-------------------------------------------------------------------
Intercept                 0.421    0.150  2.799 0.005  0.126  0.715
treatment[Substance1]:day 0.459    0.031 14.735 0.000  0.398  0.520
treatment[Substance2]:day 0.478    0.031 15.400 0.000  0.417  0.539
treatment[Substance3]:day 0.211    0.031  6.794 0.000  0.150  0.272
treatment[water]:day      0.622    0.031 20.011 0.000  0.561  0.682
subject Var               0.001                                    
===================================================================

I don't really know what ANOVA contrast to use here, because I do not want to determine significance relative to some intercept in the middle of everything, but rather for the treatments(X time) relative to each other. Aslo, I'm a bit confused by why the intercept is not in fact in the middle. I would have thought that it would be, and some of the treatmentXtime effects would have been negative.

Once I will have established this, how would I go about determining which treatments are the oddballs? Will it suffice to consult some (other) model fit summary (because here everything is significant), or will I have to run post-hoc t-tests for all combinations? if so, does statsmodels have such a functionality?

Activity

  1. josef-pkt commented on Jul 12, 2018

    @josef-pkt
    Member

    (I'm on vacation and was sightseeing for a few days, so only limited time and answer)

    Generic answer, I'm not sure it works for mixedlm, but it should.
    You don't have a reference trend, so the joint hypothesis is that all trends, coefficients for treatment-day interaction are the same.

    A simple reference contrast for the joint hypothesis would be, using either strings or a contrast matrix:

    contrast = "treatment[Substance2]:day - treatment[Substance1]:day,
    treatment[Substance3]:day - treatment[Substance1]:day,
    treatment[water]:day - treatment[Substance1]:day"
    
    results.wald_test(constrast)
    

    We don't have yet the automatic functions for pairwise contrasts with interactions, and no specific p-values for it. I recently added t_test_pairwise #4365 that currently only works for categorical main effects, and uses any of the multiple testing p-value correction like bonferroni.
    In this case, I think you have to manually create all 4*3/2 pairwise contrasts, and then use the multiple p-value correction function on the returned p-values.

    There are some open issues to improve this and tukeyhsd, triggered by #4323, but it will take some time to get there.

    Can I use your example (which I haven't looked at) as a test case when I get back to this or is the data for restricted usage?

  2. TheChymera commented on Jul 12, 2018

    @TheChymera
    Author

    (Also at a conference and sightseeing, so more after I'm back)

    In any case you can use the data CC-0, it is wholly synthetic.

  3. TheChymera commented on Jul 15, 2018

    @TheChymera
    Author

    @josef-pkt

    ok, so you said:

    You don't have a reference trend, so the joint hypothesis is that all trends, coefficients for treatment-day interaction are the same.

    What do you mean by the "joint hypothesis"? I also have trouble interpreting the p-values from the table. To go back to the generic p-value definition, it's the probability of observing results at least as extreme as those observed, given no effect --- but when I'm getting 0.005 for the Intercept in the summary, what would “no effect” here be relative to?

    A simple reference contrast for the joint hypothesis would be, using either strings or a contrast matrix:

    I think this might be part of what I am looking for. But I'm not sure whether it would be correct to just go ahead and do post-hoc tests on all treatmentXday contrast combinations. Shouldn't I first run an ANOVA on these contrasts? How would I do that?

  4. josef-pkt commented on Jul 16, 2018

    @josef-pkt
    Member

    What do you mean by the "joint hypothesis"?

    The joint hypothesis is that all trends are the same, i.e. it has multiple sub-hypothesis that all are satisfied under the Null. The alternative is that at least one trend is not the same as the others.
    This is only for the treatment:day interaction term. The intercept is not included in the "no-effect" hypothesis.
    wald_test is the general version of an F-test for joint hypothesis using the wald test. ANOVA could also use a likelihood ratio test, but I'm not familiar with that in the mixed model case.

    post-hoc tests on all treatmentXday contrast combinations

    It is possible to use the initial joint hypothesis test as a "gate keeper" to protect overall error rate, i.e. look at the individual pairwise comparisons only if the joint hypothesis is rejected.

    Alternatively, using a multiple testing corrected p-values for the pairwise-contrasts also protects the familywise error rate, or the FDR if those methods are used.

    anova_lm tests that terms are zero, which would test that there is no day and no treatment effect at all in your parameterization. To test the interaction effect separately from the main effects, you would need to use a different parameterization. AFAIR, "value ~ treatment * day" would separate out main and interaction effect.
    However, I don't think anova_lm supports MixedLM, i.e. it is written to apply only for OLS, and I'm not sure it does the right computation in the MixedLM case.

  5. TheChymera commented on Jul 19, 2018

    @TheChymera
    Author

    The alternative is that at least one trend is not the same as the others.

    Ok, so in the light of this, from the aforementioned summary I can conclude that for all treatments the probability of observing data at least as extreme as this if the treatment:time effects were all equal is smaller than 0.0005 --- but how can this be? Clearly the treatment:time interaction effects of substance 1,2, and water are indistinguishable.

    On a more general note, what kind of statements could I make based on the summary alone? Or are all rows misleading if commented on individually?

  6. josef-pkt commented on Jul 19, 2018

    @josef-pkt
    Member

    Based on the summary alone, you can only say that all coefficients are different from zero, i.e. the null hypothesis that the coefficient is zero is rejected for each one. (There is a significant day trend for each treatment, that would be true even if pvalues are multiplied by 5 to make a simple Bonferroni correction for the 5 treatment trend hypotheses.)

    The summary without reference level does not show any relative contrasts. I.e. there is no hypothesis test in the summary for difference in trends.
    To see this, you either need to reparameterize with a different contrast, or more generally use the wald_test and t_test with the differences I mentioned above.
    For example: a contrast with the difference between treatment[Substance3]:day and the other treatment trends should show that those differences are statistically significantly different from zero.

  7. TheChymera commented on Jul 19, 2018

    @TheChymera
    Author

    Ok, I think I understand now. And if I would like to explore whether any of them are significantly different from each other, what contrast would I use then? Clearly "treatment[Substance2]:day - treatment[Substance1]:day, treatment[Substance3]:day - treatment[Substance1]:day, treatment[water]:day - treatment[Substance1]:day" is just relative to Substance1, so if Substance2 and Substance3 difffer, I would not pick that up, right?

  8. TheChymera commented on Jul 30, 2018

    @TheChymera
    Author

    @josef-pkt

    I am trying to calculate all the individual product p-values explicitly (for loop...), and I can use wald_test:

    contrast = "treatment[Substance2]:day - treatment[Substance1]:day"
    results.wald_test(constrast)
    

    The t-test (more commonly used in this scenario), however, fails:

    Traceback (most recent call last):
      File "sprout-lengthANOVA.py", line 31, in <module>
        t = fit.t_test(contrast)
      File "/usr/lib64/python3.6/site-packages/statsmodels/regression/mixed_linear_model.py", line 2364, in t_test
        if r_matrix.shape[1] != self.k_fe:
    AttributeError: 'str' object has no attribute 'shape'
    

    Any idea why this is the case, and how I might be able to use the t-test? Or would this be a misapplication of the t-test?

  9. kshedden commented on Jul 30, 2018

    @kshedden
    Contributor
  10. josef-pkt commented on Jul 30, 2018

    @josef-pkt
    Member

    I opened #4817.
    I suspect that there is a work-around, but I won't be able to check for at least a few days.

  11. TheChymera commented on Jul 30, 2018

    @TheChymera
    Author

    @kshedden it's strange that you say there's a t-test but not a Wald test --- because atm I have no problem using the wald test, it's the t-test causing trouble.

    @josef-pkt What can I do in the mean time? I tried passing a list ([0,1,0,0,1], à la ANOVA), but that doesn't work either.

  12. josef-pkt commented on Jul 30, 2018

    @josef-pkt
    Member

    @TheChymera What's the exception that you get?
    try to use a 2-D array, e.g. np.array([[0,1,0,0,1]])

    There is no asarray and dimension adjustment in the MixedLM t_test code, AFAICS.

  13. TheChymera commented on Jul 30, 2018

    @TheChymera
    Author

    @josef-pkt ah, ok, that did the trick :)

    Is there any reason why I would prefer either the T-test or the Walt-test? I haven't been able to find any good rule of thumb for making this choice.

  14. josef-pkt commented on Jul 30, 2018

    @josef-pkt
    Member

    t_test are individual hypothesis tests, but vectorized to get the results for each separate test at the same time.
    wald_test is for a single joint hypothesis that assumes that all the individual hypotheses hold jointly against the alternative that at least one of them fails. That's similar to an F-test, and in the linear model f_test and wald_test produce the same result (except that wald_test uses use_t and can use either F or chi-square distribution for the p-values.

    However, t_test is also a Wald test (same theoretical background), and wald_test and t_test are equivalent if there is only one hypothesis, i.e. r_matrix has only one row.

  15. kshedden commented on Jul 30, 2018

    @kshedden
    Contributor

    There is a t-test and a wald-test inherited from the base class, but they likely don't work properly for mixedlm. For computational purposes, MixedLM is fit using a profiled (concentrated) likelihood. Those concentrated parameters are what the inherited wald/t-test will see, but it is probably not what you want them to see. t-test has been over-ridden with an implementation that should work for mixed models, but wald test has not been.

  16. TheChymera commented on Aug 4, 2018

    @TheChymera
    Author

    @josef-pkt are you sure fit.t_test(np.array([[0,1,0,0,1]])) would be what tests the null hypothesis that there is no differecnce between the 2nd and 5th levels?

    If I use that approach I get significance values which don't really make sense gven the data. However, fit.t_test(np.array([[0,1,0,0,-1]])) gives me what I would expect.

  17. josef-pkt commented on Aug 4, 2018

    @josef-pkt
    Member

    I had just copied the first example from your comment to add array, the second restriction matrix is correct.
    Testing that the difference between two coefficient is zero requires that one is positive and the other is negative in the restriction matrix.
    fit.t_test(np.array([[0,1,0,0,-1]])) has the null that 1 * params[1] + (-1) * params[4] = 0

  18. TheChymera commented on Aug 4, 2018

    @TheChymera
    Author

    Thank you so much for your clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions