Skip to content

BUG: f_test and t_test shape mismatch in MNLogit #457

Closed

Description

@josef-pkt

MNLogit cov_params is for flattened array, params is 2d

It looks like this is not adjusted for f_test and t_test.

>>> mlogit_res.t_test(np.ones(36))
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    mlogit_res.t_test(np.ones(36))
  File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new\statsmodels\statsmodels\base\model.py", line 1078, in t_test
    raise ValueError('r_matrix and params are not aligned')
ValueError: r_matrix and params are not aligned
>>> mlogit_res.t_test(np.ones(6))
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    mlogit_res.t_test(np.ones(6))
  File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new\statsmodels\statsmodels\base\model.py", line 1095, in t_test
    _sd = np.sqrt(self.cov_params(r_matrix=r_matrix, cov_p=cov_p))
  File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new\statsmodels\statsmodels\base\model.py", line 1014, in cov_params
    tmp = np.dot(r_matrix, np.dot(cov_p, np.transpose(other)))
ValueError: matrices are not aligned

Note, the proper test for discrete should be Wald test based on asymptotic normality, but it has the same structure as f_test and t_test.

I don't know which other models might be affected (VAR has 2d params, sysreg works with 1d params)

my suggestion callback for params transform, on mailing list

Activity

  1. jseabold commented on Sep 12, 2012

    @jseabold
    Member

    Briefly until I have time to look at this in detail. cov_params should stay 2d, we just need to be consistent about how we ravel and reshape. 'F' order now (I think, but I'd have to check again). I think what we should do is have MultivariateModel that does the reshaping and then pushes things up to t_test/f_test or whatever we need rather than model specific callbacks. That way, it's handled consistently and in one place, and I think will cover all multivariate / multiequation models.

  2. josef-pkt commented on Sep 18, 2012

    @josef-pkt
    MemberAuthor

    I think working with 2d params should be an exception (or I would even prefer it raveled)

    The problem with multivariate / multiequation models is that 2d only works if we have the same number of exog (and maybe same exog) in each equation.

    Alexandre's sysreg works mostly (or exclusively IIRC) with 1d params because or when there is no restriction that the number of parameters is the same for each equation. (he reshaped data and residuals when necessary)

    Similar for panel data, the versions I worked with don't assume balanced panels and work with stacked data only.

    Same for two-stage models (not available yet), Heckman, ZeroInflated with explanatory variable in the zero equation.

    And even multinomial/conditional choice models can have a richer structure across endog-choices, that doesn't fit the 2d params shape.

    There are a few models where 2d params can be more convenient, VAR, SUR, pure multivariate model, balanced panel (?)

  3. josef-pkt commented on Feb 6, 2013

    @josef-pkt
    MemberAuthor

    needs checking, this might have been fixed by the L1 penalization merge

  4. jseabold commented on Feb 6, 2013

    @jseabold
    Member

    I'm a bit curious about API here. I don't think patsy yet handles equations (or it's been discussed). Or how to signify for which equation you'd like to test a parameter?A dictionary ?

  5. josef-pkt commented on Feb 6, 2013

    @josef-pkt
    MemberAuthor

    I think each variable name will need to have an equation reference/qualifier.
    Internally, I think it's just the flattened params corresponding to the cov_params which are also for flattened params.

    MNLogit and VAR are still simpler because of the rectangular params, but for sysreg or other models with different number of parameters per equation, we need explicit mapping.

    We should check what Stata is doing.

  6. josef-pkt commented on Aug 7, 2013

    @josef-pkt
    MemberAuthor

    This is still broken, now it fails already in the call to patsy see #669

    summary() works because it has special code for MNLogit that doesn't require the names for all parameters.

    One possibility is to add an extra attribute params_names so we don't have to patch exog_names when k_exog is not equal to the number of params. (mentioned once in another issue)

    I removed high priority and moved to 0.6 milestone, because t_test and f_test are currently t-distribution and not normal distribution based.

  7. jseabold commented on Oct 9, 2014

    @jseabold
    Member

    I'm kind of wanting to punt on this for 0.6. It's not a regression, so shouldn't be a release blocker I don't think and people haven't been exactly clamoring for this feature to be fixed. I am willing to move this up my priority list and get this working though. Thoughts?

    Food for thought, this is how Stata works syntactically.

    Give the outcome name. Test all coefficients are zero.
    
    [Outcome1]
    
    Test specific coefficient of equation.
    
    [Outcome1]coef
    
    Test that all coefficients are equal across models
    
    [Outcome1=Outcome2]
    
    Test within an outcome
    
    [Outcome1]: coef1 coef2
    
    Arbitrary test
    
    [Outcome1]coef1 + [Outcome2]coef2)/2 = 2 - [Outcome2]coef3
    

    We can make this more Pythonic I think by switching the bracketing. To be like __getitem__ access for variables within equations. Either we'll need to do some pre-processing or patsy will need to get some machinery for multiple equations.

  8. modified the milestones: 0.7, 0.6 on Oct 15, 2014
  9. jseabold commented on Oct 15, 2014

    @jseabold
    Member

    Punting to 0.7

  10. self-assigned this
    on Jul 24, 2019
  11. modified the milestones: 0.7, 0.11 on Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions