Skip to content

BUG/ENH IV2SLS GMM - #1105

Closed
josef-pkt wants to merge 36 commits into
statsmodels:masterfrom
josef-pkt:gmm_new
Closed

josef-pkt wants to merge 36 commits into
statsmodels:masterfrom
josef-pkt:gmm_new

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

see #1066 #1096

Fixes IV2SLS, finishes up, and adds tests against Stata

  • What's available is tested now, including spec_hausman.
  • Stata has a lot more, but ivreg2 mixes IV2SLS and IVGMM. (for example j-test is available in IVGMM.
  • reported tests in summary:
    • box-pierce, breush-pagan, durbin's h are not valid with endogenous on rhs,
    • use Cumby Huizinga 1992 instead (Baum Shaffer Stillman 2007)
    • reset test also needs to be adjusted (reduced form yhat as regressor)
  • still missing: IV tests, and information about first stage regression

Some improvement and refactoring of GMM using IVGMM as test case.

  • tests for iterated to convergence verifies against Stata
  • onestep or twostep doesn't match up yet, because fititer updates the weight matrix.
    (weights, invweights and cov_moments are still not fully separated, code mixes them up.)
    -> refactored, but Stata gmm oneway produces different results for bse and q, jval
    DONE Stata uses different defaults, I get good agreement using the same options
    onestep and iid replicates OLS
  • IVGMM still uses generic, nonlinear GMM optimization, not linear algebra,
    DONE added LinearIVGMM
  • missing under identification and weak instrument hypothesis tests
  • tests for exactly identified case ? (Stata gmm oneway handles exactly identified case)
    DONE unit tests with GMM replicating OLS at >10 decimals
    • missing: there is no special treatment for exactly identified case
  • Note: jtest is only valid if optimal GMM (no checks for this)
  • missing HAC kernels in sandwich_covariance, and automatic lag/bandwidth selection
  • cluster robust weights and cov_params not connected yet
  • shape restrictions on GMM model, still assumes same number of moments per observation, others?
  • CUE: method seems to work, Stata gmm doesn't have CUE, ivreg2 does have cue option, no unit test yet

Optimization Difficulties

Trying the datasets.randhie Poisson example as test case:

Examples/Usage

  • datasets.randhie Poisson
    • difference in params between gmm and poisson is around 5 decimals
    • bse gmm / bse poisson is approximately 2.5 (variance = mu not imposed in GMM)
    • bse gmm / bse negbin is between 1.1 and 1.2

@josef-pkt

Copy link
Copy Markdown
Member Author

Travis failure dta data file is not included in sdist (setup.py)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling f090213 on josef-pkt:gmm_new into 3b7082c on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member

We'll need to work with patsy to discuss a formula interface for this (and similar two-stage models) still, correct?

@josef-pkt

Copy link
Copy Markdown
Member Author

for IV2SLS and similar
I didn't look at how R handles this with formulas. My guess would be pipes.
An alternative is to just write several formulas to create the different dmatrices.

GMM needs in the general case only a nonlinear function, data is up to the user, GMM wouldn't care. I currently use endog and exog to cover the special case u = y - f(x), and additionally instruments z for the case E[z (y - f(x)] = 0.

In zero-inflated count models or similar we would also need two formulas, which might only be a problem if there are common elements that cannot be treated separately.

another design decision:
ivreg2 splits explanatory variables and instruments into rhs-endogenous, and included and excluded exogenous in the rhs. exog in my case in IV2SLS is rhs explanatory variables, not "exogenous" variables.
I don't need the split for the calculations so far, but some of the diagnostic tests like underidentification are based on it.

@jseabold

Copy link
Copy Markdown
Member

exog/instruments can usually work fine for the linear model. I used inflateX in ZIP, but I don't much care what it ends up as.

In the back of my head I have the use case that caused me to give up on SUR long ago, that derived variables do not carry type information with them. E.g., in R, you can do

z = y1 - x1

where x1 is endogenous, then only include z in the regression and R knows to instrument it because it's derived from an endogenous variable. We still don't have this capability but we get closer with formulas I think. I'm not sure if this applies directly to GMM. My understanding is that it's always something like E[Z u(B)] = 0.

@josef-pkt

Copy link
Copy Markdown
Member Author

about z = y1 - x1:
I'm not sure I like R's automatic behavior. How do you know what's a valid instrument?

Alexandre had a case like this in last years GSOC. I don't want to get too deep into this, because there is still the sysreg branch waiting for review and merging, which includes a lot of models related to this, including SUR.

The most general version of GMM is just based on moment conditions E[m(theta)] = 0 without data and instruments.
Example usecase: estimating distribution parameters based on quantiles or histogram data.

However, in this PR everything relating to GMM is based on extending the E[Z u(B)] = 0 case.
And possibly the E[Z(B) u(B)] = 0 case, in analogy to GEE. (I'm doing GMM to understand GEE. :)

@josef-pkt

Copy link
Copy Markdown
Member Author

about z = y1 - x1 again
I just remembered the discussion I had with Alexandre.
It's autocorrected in my version of IV2SLS
If you include x1 as an instrument (i.e. all exogenous variables), then, if you add z = y1 - x1 as an additional variable to exog (i.e. all rhs explanatory variables), it will be automatically instrumented out.

That's because I don't distinguish between endogenous in exog and exogenous in exog. All exog are instrumented, the real exogenous just instrument out themselves, because they are in exog and in instruments.
That's an advantage of the GMM formulation, that I carried over when I wrote IV2SLS.

Not yet implemented:
I thought of using the results from the first stage regression to identify exogenous exog, because they have zero (+-floating point errors) residuals, without needing the user to specify it. (linear case only, I guess)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling e5452b2 on josef-pkt:gmm_new into 3b7082c on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member

Looking at it now, stata has an option whether to include the intercept or not. Default is to exclude so it shouldn't be particular to the "incidental" parameters of the panel models, though that was my intuition as to why it was excluded by default. They don't though allow you to pick variables though. You can compare models with different variables and it will test only the overlapping ones. We should probably check that the names are not generic and reject these kinds of comparisons in this case to ensure we're not testing "x1" vs a different "x1".

@jseabold

Copy link
Copy Markdown
Member

I'm not sure what the api would be to allow comparisons across different generically named variables (not pretty).

@josef-pkt

Copy link
Copy Markdown
Member Author

I just found this
http://www.stata.com/statalist/archive/2005-08/msg00762.html

In the ivreg and gmm Stata references they recommend sigmamore. We get a positive definite difference matrix if we use the same estimate for sigma, the estimated error variance.

@josef-pkt

Copy link
Copy Markdown
Member Author

My spec_hausman method for IV2SLS uses one scale estimate, the one from OLS, which should be the same as sigmamore IIRC.

@jseabold

Copy link
Copy Markdown
Member

Ha, that's a good find. The Hausman test does not seem to be all that well regarded in many circles. I knew about sigma(more/less). It seems odd to me to use sigmamore since the more estimator is inconsistent under the alternative hypothesis IIRC, but I haven't looked at the proofs. One of the reasons I wanted to find the Gouriéroux and Monfort textbook/handbook.

@josef-pkt

Copy link
Copy Markdown
Member Author

You can compare models with different variables and it will test only the overlapping ones.

I don't see this in the Stata manual.

I think we can check that the variable/parameter names agree and if not raise. however, it might be better to add an option to override this check.
This could be applied also to the nested compare_lr compare_ftest, however there it's more likely that we have a differently parameterized model without exact subset of exog_names.

In GMM in this PR I don't have automatic param_names yet, and no inherited datahandling.

@josef-pkt

Copy link
Copy Markdown
Member Author

Ha, that's a good find. The Hausman test does not seem to be all that well
regarded in many circles. I knew about sigma(more/less). It seems odd to me
to use sigmamore since the more estimator is inconsistent under the
alternative hypothesis IIRC, but I haven't looked at the proofs. One of the
reasons I wanted to find the Gouriéroux and Monfort textbook/handbook.

The proofs will be under the assumption that the Null holds, so we can use
either. The only question could be power under the alternative. The IV
examples that I looked at recommend sigmamore.

@jseabold

Copy link
Copy Markdown
Member

You might just try inheriting from Model and see how far it gets you. It might be, ideally should be, free. I can help you through this code if/when you get a chance to do this.

@josef-pkt

Copy link
Copy Markdown
Member Author

Changed to inherit from base.Model.
However I needed to fix the xnames, because I don't have a one-to-one mapping between xnames and param_names.
Is currently a restrictive, not nice solution.

endog and exog are in some cases meaningless, because it's just a way to transport the data to the moment conditions.
I need to add endog to the exog array if moment conditions don't have a nice y - f(x) structure in IVGMM, like in Poisson with multiplicative heterogeneity and in the euler equation example in the gist, not in this PR yet.
In these cases I would just need data.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 02fab12 on josef-pkt:gmm_new into 3b7082c on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member

Right. We might/do need some special handling for these cases and for systems of equations, etc. You can see the panel PR for how I did this there. Mostly straight-forward just need to overwrite a few methods. I can give a hand here.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 462fc33 on josef-pkt:gmm_new into 3b7082c on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member Author

test coverage is only 67% so far
main parts: HAC covariance and CUE
DistQuantilesGMM still needs refactoring and unit tests

IV2SLS has essentially full test coverage in gmm.py, however many results are inherited and don't show up in the coverage report for gmm.py. (coverage percentages are misleading in cases like this.)

@josef-pkt josef-pkt closed this in 8b0f906 Dec 20, 2013
@josef-pkt
josef-pkt deleted the gmm_new branch July 10, 2014 02:22
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
@bashtage bashtage added the rejected Used for PRs with changes that are not acceptable label Jul 27, 2020
@josef-pkt josef-pkt added superseded Used for PRs with content that is superseded by a newer PR and removed rejected Used for PRs with changes that are not acceptable labels Dec 13, 2024
@josef-pkt

Copy link
Copy Markdown
Member Author

merged in #1258

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

superseded Used for PRs with content that is superseded by a newer PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants