Skip to content

BUG: VAR cov_params pandas wrapper is broken #5305

Closed

Description

@mk1564

Hi,

I get error message for some models when I try to compute the standard errors for the coefficients. But the code in the package seems to be working well. Can you guys help me? Thanks!

z=result_bp.ys_lagged
cov=np.kron(scipy.linalg.inv(np.dot(z.T, z)), result_bp.sigma_u)
stderr = np.sqrt(np.diag(cov))
stderr.reshape((48, 4), order='C')

This one works fine if I manually input (48,4). The following is the error message when I try to use the attribute (stderr) in VAR.

ValueError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\internals.py in create_block_manager_from_blocks(blocks, axes)
4858
-> 4859 mgr = BlockManager(blocks, axes)
4860 mgr._consolidate_inplace()

~\Anaconda3\lib\site-packages\pandas\core\internals.py in init(self, blocks, axes, do_integrity_check)
3281 if do_integrity_check:
-> 3282 self._verify_integrity()
3283

~\Anaconda3\lib\site-packages\pandas\core\internals.py in _verify_integrity(self)
3492 if block._verify_integrity and block.shape[1:] != mgr_shape[1:]:
-> 3493 construction_error(tot_items, block.shape[1:], self.axes)
3494 if len(self.items) != tot_items:

~\Anaconda3\lib\site-packages\pandas\core\internals.py in construction_error(tot_items, block_shape, axes, e)
4842 raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 4843 passed, implied))
4844

ValueError: Shape of passed values is (4, 48), indices imply (4, 24)

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in ()
----> 1 result_bp.stderr

~\Anaconda3\lib\site-packages\statsmodels\base\wrapper.py in getattribute(self, attr)
39 obj = data.wrap_output(obj, how[0], *how[1:])
40 elif how:
---> 41 obj = data.wrap_output(obj, how=how)
42
43 return obj

~\Anaconda3\lib\site-packages\statsmodels\base\data.py in wrap_output(self, obj, how, names)
416 return self.attach_dates(obj)
417 elif how == 'columns_eq':
--> 418 return self.attach_columns_eq(obj)
419 elif how == 'cov_eq':
420 return self.attach_cov_eq(obj)

~\Anaconda3\lib\site-packages\statsmodels\base\data.py in attach_columns_eq(self, result)
528
529 def attach_columns_eq(self, result):
--> 530 return DataFrame(result, index=self.xnames, columns=self.ynames)
531
532 def attach_cov(self, result):

~\Anaconda3\lib\site-packages\pandas\core\frame.py in init(self, data, index, columns, dtype, copy)
377 else:
378 mgr = self._init_ndarray(data, index, columns, dtype=dtype,
--> 379 copy=copy)
380 elif isinstance(data, (list, types.GeneratorType)):
381 if isinstance(data, types.GeneratorType):

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _init_ndarray(self, values, index, columns, dtype, copy)
534 values = maybe_infer_to_datetimelike(values)
535
--> 536 return create_block_manager_from_blocks([values], [columns, index])
537
538 @Property

~\Anaconda3\lib\site-packages\pandas\core\internals.py in create_block_manager_from_blocks(blocks, axes)
4864 blocks = [getattr(b, 'values', b) for b in blocks]
4865 tot_items = sum(b.shape[0] for b in blocks)
-> 4866 construction_error(tot_items, blocks[0].shape[1:], axes, e)
4867
4868

~\Anaconda3\lib\site-packages\pandas\core\internals.py in construction_error(tot_items, block_shape, axes, e)
4841 raise ValueError("Empty data passed with indices specified.")
4842 raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 4843 passed, implied))
4844
4845

ValueError: Shape of passed values is (4, 48), indices imply (4, 24)

Activity

  1. josef-pkt commented on Oct 6, 2018

    @josef-pkt
    Member

    It looks like there is something wrong with the pandas wrapper.

    You could check the underlying result (note, I never tried that for VAR)

    If result_bp is your results instance returned by fit it should be the wrapped results, The underlying numpy array result instance is result_bp._results.
    So you could try whether result_bp._results.stderr returns correct results.

  2. mk1564 commented on Oct 6, 2018

    @mk1564
    Author

    Hi Josef,

    Thanks for your quick comment. It works! But the things is it gives an array but does not create nice looking table with variable names. Can you tell me how I can make one?

    logy logc logi logh
    L1.logy 0.525307 0.303396 2.778826 0.428730
    L1.logc 0.456111 0.263431 2.412786 0.372256
    L1.logi 0.086517 0.049969 0.457668 0.070611
    L1.logh 0.100109 0.057819 0.529566 0.081704
    L2.logy 0.529679 0.305921 2.801955 0.432299
    L2.logc 0.451868 0.260980 2.390341 0.368793
    L2.logi 0.085625 0.049454 0.452949 0.069883
    L2.logh 0.086582 0.050006 0.458013 0.070664

  3. josef-pkt commented on Oct 6, 2018

    @josef-pkt
    Member

    Does result_bp.params return a DataFrame?
    If yes, then you could recreate a DataFrame from result_bp._results.stderr with the same index and column names as params.

  4. josef-pkt commented on Oct 6, 2018

    @josef-pkt
    Member

    I cannot replicate this in the simple example from the sirf_errband PR

    >>> res_var = VAR(df[['r','gm','gp','gy']]).fit(maxlags=3)
    >>> res_var.stderr
                  r        gm        gp        gy
    const  0.076348  0.078605  0.071508  0.073120
    L1.r   0.073431  0.075602  0.068776  0.070326
    L1.gm  0.071758  0.073879  0.067208  0.068723
    L1.gp  0.077928  0.080232  0.072988  0.074633
    L1.gy  0.077077  0.079355  0.072190  0.073817
    L2.r   0.071566  0.073682  0.067029  0.068540
    L2.gm  0.072531  0.074675  0.067933  0.069464
    L2.gp  0.076706  0.078974  0.071843  0.073463
    L2.gy  0.077244  0.079527  0.072347  0.073977
    L3.r   0.073461  0.075633  0.068804  0.070355
    L3.gm  0.072795  0.074946  0.068180  0.069716
    L3.gp  0.078661  0.080986  0.073674  0.075334
    L3.gy  0.076787  0.079057  0.071919  0.073540
    

    However, when I access cov_params I get a similar error

    >>> res_var._results.cov_params.shape
    (52, 52)
    >>> res_var.cov_params
        construction_error(tot_items, blocks[0].shape[1:], axes, e)
      File "...\python-3.4.4.amd64\lib\site-packages\pandas\core\internals.py", line 4231, in construction_error
        passed, implied))
    ValueError: Shape of passed values is (52, 52), indices imply (13, 13)
    ...
    
  5. mk1564 commented on Oct 6, 2018

    @mk1564
    Author

    I have another model for which stderr works fine. The usual stderr commend creates a nice looking table but ._results.stderr does not. Can you tell me why with ._results I cannot make a table, even though the commend is essentially the same?

  6. josef-pkt commented on Oct 6, 2018

    @josef-pkt
    Member

    When the model is created with pandas DataFrames, then the results are converted back to pandas dataframes or series. This is done in the wrapper.

    >>> type(res_var.stderr)
    <class 'pandas.core.frame.DataFrame'>
    >>> type(res_var._results.stderr)
    <class 'numpy.ndarray'>
    

    So using the internal _results only has numpy methods available but not pandas.

  7. mk1564 commented on Oct 6, 2018

    @mk1564
    Author

    Oh I see. Thanks!

  8. josef-pkt commented on Oct 6, 2018

    @josef-pkt
    Member

    @mk1564
    We have PR #5220 that refactors and fixes some bugs in VAR/var_model.
    If you are able to checkout and run from a development PR branch, then it would very helpful if you could check whether your code works with that refactoring.
    Several names will be changed to more informative names, but it should be backwards compatible with deprecation, with maybe some exceptions.
    I'm not sure yet whether we want to include that refactoring in 0.10, because merging before a release doesn't give us much time to figure out refactoring problems.

  9. changed the title [-]VAR stderr[/-] [+]BUG: VAR cov_params pandas wrapper is broken[/+] on Oct 6, 2018
  10. josef-pkt commented on Oct 6, 2018

    @josef-pkt
    Member

    AFAICS, pandas wrapper for cov_params never worked, problem is similar to MNLogit when params is 2-D

    the wrapper uses "cov"

        def attach_cov(self, result):
            return DataFrame(result, index=self.param_names,
                             columns=self.param_names)
    

    but param_names and x_names define exog names for each single equation but not for the raveled
    equation cross exog list.

    I changed the title to reflect this.

  11. added this to the 0.11 milestone on Oct 6, 2018
  12. jbrockmendel commented on Oct 8, 2018

    @jbrockmendel
    Contributor

    This is fixed in sm2, https://github.com/jbrockmendel/sm2, could be fixed here too if improving things were allowed.

  13. added 2 commits that reference this issue on Jul 16, 2019
    32ec700
    5134c97
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions