Skip to content

SUMM: Wrapper / data attach method consistency #2610

Open

Description

@ChadFulton

It seems to me that there are some consistency and naming issues with the way wrapping and metadata attachment are done; I don't know how to address them all at once, but here is a list of current behavior with some notes about issues. The main issues I feel like exist are:

  • attach_* methods weren't designed with multivariate series in mind; problems include:
    • inconsistent squeezing, some functions not defined for 2d data
    • inconsistent behavior with respect to column names (currently 2d DataFrames get column names, but 1d series do not - this behavior is tested in unit tests, so I'm not sure if anything relies on it)
    • no method for handling tiled or repeated columns (for example cov_params for 3 endog series, should be something like [y1_lower, y2_lower, y3_lower, y1_upper, y2_upper, y3_upper] or [y1_lower, y1_upper, y2_lower, y2_upper, y3_lower, y3_upper]).
  • I'm not sure what the future of the attach_* functions are - I didn't want to add new ones for my uses because it seems like a proliferation of one-off attach functions isn't a good idea.
  • wrapper.py inconsistent behavior with respect to passing arguments. Easy fix to match existing behavior (if that's what is desired).

It's also possible that I just don't fully understand the design plan of metadata attaching; if that's so, I'd be grateful for that information also.

wrapper.py

Here, just a consistency issue: wrappers for attributes allow passing arguments in the wrapper specification (see

if how and isinstance(how, tuple):
) but this is not allowed for method wrappers (
return data.wrap_output(func(results, *args, **kwargs), how)
).

Presumably this is just because someone needed it for attributes (attach_general_columns) but not for methods; it's an easy fix.

Current attach_* methods

I feel like there is room for improvement here in terms of consistency and functionality.

The following attach_* methods exist (and only do anything if we have Pandas data):

attach_columns
Returns a series or DataFrame, with parameter names (self.param_names) as index (row names). Used in:

  • base/model.py to wrap the following attributes: params, bse, pvalues, tvalues, conf_int
  • regression/linear_model.py to wrap the following attributes: chisq, HC0_se - HC3_se

attach_columns_eq
Returns a DataFrame with exog names as index (row names), ynames as columns. Used only in var_model.py for attributes: bse, params, pvalues, tvalues, stderr

attach_cov
Returns a DataFrame with parameter names as both column and row names. Used only for cov_params in various models.

attach_cov_eq
Returns a DataFrame with ynames as both column and row names. Used only in var_model.py for attribute sigma_u, sigma_u_mle.

attach_rows
Returns a Series or DataFrame with row labels attached. Used in:

  • base/model.py for resid, fittedvalues
  • discrete/discrete_model.py for resid_* attributes
  • genmod/generalized_estimating_equations.py for centered_resid
  • genmod/generalized_linear_model.py for resid_* attributes
  • regression/linear_model.py for sresid, weights, wresid, norm_resid

It has a couple of issues with it:

  • Inconsistent squeezing
  • The DataFrame version sets the column names to ynames, the Series version does not set a name.

attach_dates:
Returns a TimeSeries object with self.predict_dates as index.

attach_generic_columns
Returns a Series object with index (row names) equal to the value of an arbitrary attribute of the data object, where the attribute name is passed as an additional argument to the wrapper specification (see below) Used in:

  • regression/mixed_linear_model.py: for bse_re, fe_params, bse_fe

attach_generic_columns_2d
Returns a DataFrame object with with index (row names) equal to the value of an arbitrary attribute of the data object, where the attribute name is passed as an additional argument to the wrapper specification. In principle it allows the specification of different column names, but in practice all wrapping code will use the row names as the column names too. Used in:

  • regression/mixed_linear_model.py: for cov_re and cov_re_unscaled

Activity

  1. josef-pkt commented on Sep 4, 2015

    @josef-pkt
    Member

    @jseabold comments?

    @ChadFulton
    I'm not very familiar with these parts. I never ran into problems and didn't have to look into it so far. This was initially written by Wes and Skipper, and I think has grown as new needs came up.
    The last two look like additions to handle new cases in mixed models that could be the base for more generic handling overall.

    One specific item: "the Series version does not set a name."
    AFAIK most of this code has been written before pandas Series allowed a name, and it could be added now.

    I don't understand your tiled or repeated column case, e.g. cov_params for 3 endog series.
    What part is missing? Is this just a 2d array with special names?
    MNLogit and VAR are the only multivariate endog models right now.

    As mentioned before, I think we should consistently squeeze 1 dimensional arrays that are not internal use only. This should be consistent at least for univariate endog. Do you have or did you see cases where that wouldn't make sense?

  2. jbrockmendel commented on Jun 27, 2017

    @jbrockmendel
    Contributor

    I've been looking at this topic while working on MNLogit, which is actively broken (e.g. cov_params). As mentioned above, the other main problematic case is VAR. In particular I'm thinking of IRFs, where the covariance matrix is effectively five dimensional ((impulse x response) x (impulse x response) x time).

    A few different options that I've tried to differing extents:

    1. Have FooData.wrap methods try to infer whether they are multivariate and change their wrapping logic accordingly. The biggest problem here is the fact that MNLogit has (or may, once implemented) an extra yname that is dropped from the columns of params, bse, .... My kludge for this has been to...

    2. Set model.data.param_names as a pd.MultiIndex, then have ModelData override wrapping logic when it sees that param_names is a MultiIndex.

    3. At the top of wrap_output check for isisntance(obj, (pd.Series, pd.DataFrame)) and if so, just return it. Basically saying "if the user wants to treat this as a special case, let them". At some point the effort to make wrapping logic general defeats the purpose of wrap in the first place.

    4. Increase the amount of information that can be passed into ResultsWrapper._wrap_attrs and _wrap_methods, so that there is a more complete API for wrapping. My main misgiving about this is that if the specification of the wrapping logic isn't immediately adjacent to the method itself, the methods and wrapping will diverge over time.

    The up-side with this last approach is that it makes it easier to do things like return conf_int as a single labelled object instead of a (estimate, lower, upper) triple (or was it (lower, estimate, upper)...?).

    Most of the decision seems to boil down to making ModelData work harder to figure out what to do versus passing it more information directly. Thoughts?

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