Skip to content

ENH: Restricted gls - #1673

Open
josef-pkt wants to merge 8 commits into
statsmodels:mainfrom
josef-pkt:restricted_gls
Open

josef-pkt wants to merge 8 commits into
statsmodels:mainfrom
josef-pkt:restricted_gls

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

WIP moving RLS out of the sandbox

added minimal tests
no refactoring yet

Performance:
uses GLS: builds full (nobs, nobs) sigma and cholsigminv

Names
k_constr, ... k_xxx instead of nconstraint
name of class: RestrictedGLS
rwexog, ... are misnomers because they are moment matrices for moment equations

open: names for constraints R, q

design

  • what do we want to cache in the model? reusable model for residual bootstrap?
  • cov_type default, move sandwich wrnorm_cov_params to results (reuse inherited code)
    (maybe: need to cache/attach pinvX for HCxx_se)
  • from_formula for constraints
  • other lazy attributes of model, e.g. diag of hatmatrix

@josef-pkt

Copy link
Copy Markdown
Member Author

The main point of PR at this stage is to be able to use comments on github for code review

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.03%) when pulling 66a55a4 on josef-pkt:restricted_gls into b472807 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

code review doesn't work: With a clean move, the diff doesn't show the original code for commenting

@josef-pkt

Copy link
Copy Markdown
Member Author

for unit tests:
Stata has cnsreg with HC1 (?) and cluster robust standard errors
It also allows weights for restricted WLS.

@josef-pkt

Copy link
Copy Markdown
Member Author

behavior of t_test and f_test of the constraint

first restriction was imposed in estimation

possible problems in f_test, negative F-statistic,
Singular error for testing only the imposed restriction might be ok

>>> rls_fit.t_test([[0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 0, 0]])
<class 'statsmodels.stats.contrast.ContrastResults'>
                             Test for Constraints                             
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
c0         -7.994e-15          0         -0      1.000     -7.99e-15 -7.99e-15
c1            -0.8267      0.148     -5.583      0.000        -1.125    -0.528
==============================================================================

>>> rls_fit.f_test([[0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 0, 0]])
<class 'statsmodels.stats.contrast.ContrastResults'>
<F test: F=array([[-4960.31131368]]), p=1.0, df_denom=42, df_num=2>

>>> rls_fit.f_test([[0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 0, 0]][1])
<class 'statsmodels.stats.contrast.ContrastResults'>
<F test: F=array([[ 31.16463173]]), p=1.57947139173e-06, df_denom=42, df_num=1>
>>> rls_fit.f_test([[0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 0, 0]][0])
Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    rls_fit.f_test([[0, 0, 0, 1, 1, 1, 1], [0, 0, 0, 1, 1, 0, 0]][0])
  File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new2_py27\statsmodels\statsmodels\base\model.py", line 1186, in f_test
    scale=scale, invcov=invcov, use_f=True)
  File "e:\josef\eclipsegworkspace\statsmodels-git\statsmodels-all-new2_py27\statsmodels\statsmodels\base\model.py", line 1278, in wald_test
    invcov = np.linalg.inv(cov_p)
  File "C:\Programs\Python27\lib\site-packages\numpy\linalg\linalg.py", line 445, in inv
    return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
  File "C:\Programs\Python27\lib\site-packages\numpy\linalg\linalg.py", line 328, in solve
    raise LinAlgError, 'Singular matrix'
LinAlgError: Singular matrix

@josef-pkt

Copy link
Copy Markdown
Member Author

nonrobust bse is

bse = np.sqrt(res_rls2.scale * np.diag(res_rls2.model.rnorm_cov_params))

default bse, is not the same as Stata's robust, difference is a constant fraction approx. 1.06
df correction ? np.sqrt(48 / (48 - 6.)) = 1.069045
HC in wrnorm_cov_params doesn't reuse scale, calculates scale sigmaSq with ddof
so maybe Stata does the nobs correction instead of df_resid

bse_robust_stata = res_rls2.bse * np.sqrt(res_rls2.model.endog.shape[0] / res_rls2.model.df_resid )

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) when pulling d7fae80 on josef-pkt:restricted_gls into b472807 on statsmodels:master.

@josef-pkt josef-pkt mentioned this pull request May 31, 2014
11 of 17 tasks
@josef-pkt josef-pkt added this to the 0.7 milestone Aug 17, 2014
@josef-pkt josef-pkt modified the milestones: 0.8, 0.7 Feb 4, 2015
@josef-pkt josef-pkt mentioned this pull request Jul 4, 2015
2 of 15 tasks
@josef-pkt josef-pkt removed the PR label Mar 5, 2016
@jseabold jseabold removed this from the 0.8 milestone Apr 2, 2021
@josef-pkt

Copy link
Copy Markdown
Member Author

unit tests are broken with recent numpy, genfromtxt, recarrays, structured dtypes don't behave "properly" for data handling anymore
design = np.column_stack((dta['Y'],dta['Y']**2,dta[['NE','NC','W','S']].view(float).reshape(dta.shape[0],-1)))
dta[['NE','NC','W','S']].view(float) does not create 4 columns, it still has the original columns

need to switch unit tests to using pandas before they can run.

@pep8speaks

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 32:20: E131 continuation line unaligned for hanging indent
Line 35:5: E131 continuation line unaligned for hanging indent
Line 36:19: E131 continuation line unaligned for hanging indent
Line 41:20: E131 continuation line unaligned for hanging indent
Line 45:19: E131 continuation line unaligned for hanging indent
Line 55:5: E131 continuation line unaligned for hanging indent
Line 61:5: E131 continuation line unaligned for hanging indent
Line 115:20: E131 continuation line unaligned for hanging indent
Line 118:5: E131 continuation line unaligned for hanging indent
Line 119:19: E131 continuation line unaligned for hanging indent
Line 124:20: E131 continuation line unaligned for hanging indent
Line 128:19: E131 continuation line unaligned for hanging indent
Line 145:7: E131 continuation line unaligned for hanging indent
Line 195:20: E131 continuation line unaligned for hanging indent
Line 199:19: E131 continuation line unaligned for hanging indent
Line 204:20: E131 continuation line unaligned for hanging indent
Line 208:19: E131 continuation line unaligned for hanging indent
Line 218:5: E131 continuation line unaligned for hanging indent
Line 220:5: E131 continuation line unaligned for hanging indent
Line 224:5: E131 continuation line unaligned for hanging indent
Line 226:5: E131 continuation line unaligned for hanging indent
Line 277:20: E131 continuation line unaligned for hanging indent
Line 280:5: E131 continuation line unaligned for hanging indent
Line 281:19: E131 continuation line unaligned for hanging indent
Line 286:20: E131 continuation line unaligned for hanging indent
Line 290:19: E131 continuation line unaligned for hanging indent
Line 302:5: E131 continuation line unaligned for hanging indent
Line 307:7: E131 continuation line unaligned for hanging indent
Line 308:5: E131 continuation line unaligned for hanging indent
Line 361:20: E131 continuation line unaligned for hanging indent
Line 364:5: E131 continuation line unaligned for hanging indent
Line 365:19: E131 continuation line unaligned for hanging indent
Line 370:20: E131 continuation line unaligned for hanging indent
Line 374:19: E131 continuation line unaligned for hanging indent
Line 382:5: E131 continuation line unaligned for hanging indent
Line 386:5: E131 continuation line unaligned for hanging indent
Line 388:5: E131 continuation line unaligned for hanging indent
Line 392:5: E131 continuation line unaligned for hanging indent
Line 445:20: E131 continuation line unaligned for hanging indent
Line 449:19: E131 continuation line unaligned for hanging indent
Line 452:5: E131 continuation line unaligned for hanging indent
Line 454:20: E131 continuation line unaligned for hanging indent
Line 464:7: E131 continuation line unaligned for hanging indent
Line 465:5: E131 continuation line unaligned for hanging indent
Line 466:5: E131 continuation line unaligned for hanging indent
Line 467:5: E131 continuation line unaligned for hanging indent
Line 468:5: E131 continuation line unaligned for hanging indent
Line 469:7: E131 continuation line unaligned for hanging indent
Line 471:5: E131 continuation line unaligned for hanging indent
Line 498:1: W391 blank line at end of file

@josef-pkt

Copy link
Copy Markdown
Member Author

rebased and force pushed

changed to using pandas for datahandling in unit tests

@josef-pkt

Copy link
Copy Markdown
Member Author

looks like only Stata results file fails style check ( no noqa yet)
There are more Stata results that are not used in unit tests yet.
covtype support still incomplete

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.

4 participants