Skip to content

ENH: add MultivariateLS - #8919

Merged
josef-pkt merged 6 commits into
statsmodels:mainfrom
josef-pkt:multivariatels
Dec 15, 2023
Merged

josef-pkt merged 6 commits into
statsmodels:mainfrom
josef-pkt:multivariatels

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Jun 20, 2023 •

Copy link
Copy Markdown
Member

Multivariate linear model estimated with least squares

shape of params and similar structure in analogy to MNLogit

current version works for all basics, including results wrapper, t_test, ...

missing

  • rsquared, llf, other postestimation statistics
  • wrapper for 2-d params, bse, ... is missing column names (same in MNLogit)
  • undecided: df_resid for wald tests within and between "equations", nobs - k_vars or total nobs - total params, optional?
  • ...

name = 'x%d' % (i)
L = np.zeros([1, k_xvar])
L[i] = 1
L[0, i] = 1

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a bug in previous code

@josef-pkt

josef-pkt commented Jun 21, 2023 •

Copy link
Copy Markdown
Member Author

all green, (unrelated failures in pre Install dependencies)

core parts are finished and could be merged
still needs some api improvement and results statistics, and more unit tests.

how do we put rsquared for each equation into the summary()?
Do we add llnull? i.e. constant only model and LR (or similar) test for it?

Main usecase for MultivariateLS are cross-equation hypotheses, single equation (endog) results are the same as OLS and can also be obtained from a list of OLS results.

Stata mvreg allows for weights, i.e. we can extend the model to var_weights similar to WLS, using wendog, wexog in the computation.

test case is based on
https://stats.oarc.ucla.edu/stata/dae/multivariate-regression-analysis/
SAS seems to report results for each equation/endog separately
https://stats.oarc.ucla.edu/sas/dae/multivariate-regression-analysis/
ucla stats also has mplus https://stats.oarc.ucla.edu/mplus/dae/multivariate-regression-analysis/
but no corresponding page for R

@josef-pkt

josef-pkt commented Jun 21, 2023 •

Copy link
Copy Markdown
Member Author

AFAICS, we don't have a manova type mv_test that all slope coefficients are zero.

missing helper function for constructing L P M test for use with t_test and wald_test (instead of mv_test).
That will mainly be needed for MNLogit and other nonlinear models where we don't have (current) mv_test which are based on linear model assumptions.

AFAIR, that's just kron(L, M). What transformation/reshape for C matrix in affine constraints?
we can reuse for this the current code for building L and M matrices.

@josef-pkt

Copy link
Copy Markdown
Member Author

missing model methods and results attributes

res.resid AttributeError: 'MultivariateLSResults' object has no attribute 'resid'
res.predict NotImplementedError

The model inherits from base Model which does not define prediction.
We could inherit from RegressionModel and Results but that might add too many inherited methods.
Alternatives, add a PredictMixin, or explicitly adding it to MultivariateLS.

similar might apply to other post-estimation diagnostics.
get_prediction
get_influence outlier-influence and residual analysis.
...

@josef-pkt

Copy link
Copy Markdown
Member Author

maybe for later:
score_obs or least squares moment conditions and similar,
useful for GMM, diagnostic and specification tests, and, most likely, for first stage in control function instrumental variables.

@josef-pkt

Copy link
Copy Markdown
Member Author

my current version of example notebook https://gist.github.com/josef-pkt/8a492802eee3896c47b646ee11beb1f6

missing: mv_test L, M version, t_test, wald_test

res._fittedmod =_fittedmod
res.cov_resid = sscpr / df_resid
return MultivariateLSResultsWrapper(res)
return res

Check warning

Code scanning / CodeQL

Unreachable code

This statement is unreachable.
@josef-pkt

josef-pkt commented Jun 23, 2023 •

Copy link
Copy Markdown
Member Author

not clear: why does confint have endog at axis=0, and exog in axis=1? params, bse, ... has exog at axis=0 and endog at axis=1
AFAIR, I just copied the MNLogit code.

(Pdb) res1.conf_int().shape
(3, 6, 2)

the mnlogit confint shape is since Nov 28, 2011 (version 0.9)

@josef-pkt

Copy link
Copy Markdown
Member Author

missing cov_type:

We can use score_obs, horizontally stacked, nobs x (k_endog * k_exog), and use "generic" cov_type handling.
It looks like this is what MNLogit is doing, score_obs is horizontally stacked u * x, computed in 3-dim and reshaped.

With full score_obs available, we get generically all standard nonrobust cov_types, but not HC3 and similar extensions/variations.

hessian?

@josef-pkt

josef-pkt commented Sep 18, 2023 •

Copy link
Copy Markdown
Member Author

I guess I should fix the confint shape even though it will be inconsistent with MNLogit
This will likely need an adjustment in the wrapper code, e.g. copy-modify the existing method for different shape.
Currently there are no flags (kwargs) used in the wrapper that could be used for wrapping options.

@josef-pkt

Copy link
Copy Markdown
Member Author

reminder
The code in unit test had simple integer indexing [0] for pandas which raises FutureWarning.
integer indexing always needs iloc

@josef-pkt

Copy link
Copy Markdown
Member Author

2 unrelated test failure, one in statespace, one in test_corrpsd.py

I'm merging this now, but there is still work to do.

main open/undecided

  • names for flattened parameters
  • shape of conf_int, order of axes
  • interface details, e.g. names for contrasts (they are still the defaults and I have not yet looked at how to improve those.)

The other missing parts are enhancements and more unit test coverage.

@josef-pkt
josef-pkt merged commit 6614975 into statsmodels:main Dec 15, 2023
@bashtage bashtage modified the milestones: 0.15, 0.14 Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants