Skip to content

groups specification under cov_type='hac-panel' on unbalanced panel data #3155

Closed

Description

@ZZWENG

A standard call to do do this on a panel data would be:

import statsmodels.formula.api as smf
smf.ols(fmla,data=regr_frame).fit(cov_type='nw-panel',cov_kwds={'maxlags':5,'time':regr_frame['date']})

I noticed that get_robustcov_results does

tt = (np.nonzero(np.diff(time) < 0)[0] + 1).tolist()
groupidx = lzip([0] + tt, tt + [len(time)])

to get the groups. However, this might not work if the panel data has non overlapping time stamps for two groups. For example, group A and B are adjacent. Group A has periods 1927 to 1945, group B has periods 1947 to 1967. Then the above code snippet would say these two are one group, while actually they are two groups.

Such input panel data usually results from dropping NAs in a stacked time series. But if I keep the NAs, get_robustcov_results gives me alignment error.

    731     for lag in range(1, nlags+1):
    732         xw0, xwlag = lagged_groups(xw, lag, groupidx)
--> 733         s = np.dot(xw0.T, xwlag)
    734         S += weights[lag] * (s + s.T)
    735     return S

ValueError: matrices are not aligned

Is there (Could there be) some additional cov_kwds (such as 'groups') to specify the groups manually? Or, is there nice way to avoid the alignment error for unbalance panel?

Activity

  1. changed the title [-]ols behavior under cov_type='hac-panel' on unbalanced panel data[/-] [+]groups specification under cov_type='hac-panel' on unbalanced panel data[/+] on Aug 12, 2016
  2. josef-pkt commented on Aug 12, 2016

    @josef-pkt
    Member

    can you try hac-groupsum ? (guess not, see below)

    It will take me a bit of time to remember (remind myself) how the aggregation is supposed to go in this.

    For the specific issue:
    Right now I don't see a reason not to add a optional group indes. as an explicit identification of groups instead of implicitly inferring it from the time array

    (hac-groupsum assumes overlapping time index IIRC)

  3. josef-pkt commented on Aug 12, 2016

    @josef-pkt
    Member

    I'm a bit puzzled.

    What's the implied correlation structure in your data? Do you have a full example.

    AFAIR, the standard usage for panel data sandwiches is to have the HAC part taking care of serial correlation, and the group part taking case of the cross-sectional correlation and heteroscedasticity.
    But maybe I don't remember this, and panel-hac is just serial correlation within a panel. In that case, adding the optional group index for identification would be an obvious explicit choice without changing the interpretation.
    (I might be mixing up the assumptions of hac-panel versus hac-groupsum)

  4. josef-pkt commented on Aug 12, 2016

    @josef-pkt
    Member

    To the last question:

    Or, is there nice way to avoid the alignment error for unbalance panel?

    At this stage there is no nan/missing handling nor is the time index checked for irregular or missing time periods. So, the assumption is no nans and for hac autocorrelation no gaps in the time series.
    Balanced panel, i.e. equal number of time periods for each group, is not be assumed.

  5. ZZWENG commented on Aug 15, 2016

    @ZZWENG
    Author

    Thank you Josef. So 'nw-panel' assumes equally spaced time periods within each group, no nans, but does not assume the panel is balanced. And it essentially extracts the time bounds from the groupidx and within each group, lagged terms are added to the overall S. (Cov = (x'x)^(-1) S (x'x)^(-1))
    That makes sense.
    However the edge case mentioned above satisfies the assumptions but the produced covariance is not correct, since the group bounds are wrong.

    I guess right now the users can find the group bounds themselves and use cov_nw_panel(results, nlags, groupidx, ...) function to get a correct covariance in this case. But would you consider adding the optional group kwd in future releases? Thanks.

  6. josef-pkt commented on Aug 15, 2016

    @josef-pkt
    Member

    (I added backport label to remind me again, since it already slipped my mind)

    A workaround would be to use group labels as time, but they would have to be sorted by decreasing number.
    Yes, I'm in favor of adding the option, especially given that the original design only require group indices (indices of break points). I switched to time index mainly for API consistency, and because most users will have it already available as part of their data(frame).
    I think it's just changing or adding two lines in each of the methods/functions, (plus 10 to 20 lines of unit tests). I will try to look into it in the coming week

  7. ZZWENG commented on Aug 16, 2016

    @ZZWENG
    Author

    Thank you. The workaround works perfectly.

  8. added a commit that references this issue on Aug 16, 2016
    417d850
  9. josef-pkt commented on Aug 16, 2016

    @josef-pkt
    Member

    @ZZZWENG PR is #3162 (travis is running the test suite now)

  10. added a commit that references this issue on Jan 27, 2017
    6c481f2
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions