Repository navigation
Description
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.pyinconsistent 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
statsmodels/statsmodels/base/wrapper.py
Line 38 in 32e739d
| if how and isinstance(how, tuple): |
statsmodels/statsmodels/base/wrapper.py
Line 92 in 32e739d
| 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.pyto wrap the following attributes:params, bse, pvalues, tvalues, conf_intregression/linear_model.pyto 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.pyforresid, fittedvaluesdiscrete/discrete_model.pyforresid_*attributesgenmod/generalized_estimating_equations.pyforcentered_residgenmod/generalized_linear_model.pyforresid_*attributesregression/linear_model.pyforsresid,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: forbse_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: forcov_reandcov_re_unscaled
Activity
josef-pkt commentedon Sep 4, 2015
@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?
jbrockmendel commentedon Jun 27, 2017
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 isVAR. 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:
Have
FooData.wrapmethods 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 extraynamethat is dropped from the columns ofparams, bse, .... My kludge for this has been to...Set
model.data.param_namesas apd.MultiIndex, then have ModelData override wrapping logic when it sees thatparam_namesis a MultiIndex.At the top of
wrap_outputcheck forisisntance(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 ofwrapin the first place.Increase the amount of information that can be passed into
ResultsWrapper._wrap_attrsand_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_intas 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?