Skip to content

Heckman rebased - #7636

Open
josef-pkt wants to merge 39 commits into
statsmodels:mainfrom
josef-pkt:heckman_rebased
Open

josef-pkt wants to merge 39 commits into
statsmodels:mainfrom
josef-pkt:heckman_rebased

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

rebased version of #1935

no merge conflicts
extra commit: update pandas usage

no other changes yet.

@pep8speaks

pep8speaks commented Aug 10, 2021 •

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 194:5: E125 continuation line with same indent as next logical line
Line 524:9: E125 continuation line with same indent as next logical line
Line 658:1: W391 blank line at end of file

Line 132:14: E131 continuation line unaligned for hanging indent
Line 135:12: E131 continuation line unaligned for hanging indent
Line 140:14: E131 continuation line unaligned for hanging indent
Line 143:12: E131 continuation line unaligned for hanging indent
Line 148:14: E131 continuation line unaligned for hanging indent
Line 149:11: E131 continuation line unaligned for hanging indent
Line 151:16: E131 continuation line unaligned for hanging indent
Line 152:12: E131 continuation line unaligned for hanging indent
Line 157:14: E131 continuation line unaligned for hanging indent
Line 158:11: E131 continuation line unaligned for hanging indent
Line 160:16: E131 continuation line unaligned for hanging indent
Line 161:12: E131 continuation line unaligned for hanging indent
Line 260:14: E131 continuation line unaligned for hanging indent
Line 263:12: E131 continuation line unaligned for hanging indent
Line 268:14: E131 continuation line unaligned for hanging indent
Line 271:12: E131 continuation line unaligned for hanging indent
Line 276:14: E131 continuation line unaligned for hanging indent
Line 277:11: E131 continuation line unaligned for hanging indent
Line 279:16: E131 continuation line unaligned for hanging indent
Line 280:12: E131 continuation line unaligned for hanging indent
Line 285:14: E131 continuation line unaligned for hanging indent
Line 286:11: E131 continuation line unaligned for hanging indent
Line 288:16: E131 continuation line unaligned for hanging indent
Line 289:12: E131 continuation line unaligned for hanging indent
Line 558:29: E702 multiple statements on one line (semicolon)
Line 558:56: E703 statement ends with a semicolon

Comment last updated at 2021-08-11 00:44:26 UTC

@lgtm-com

lgtm-com Bot commented Aug 10, 2021

Copy link
Copy Markdown

This pull request introduces 6 alerts when merging d025730 into 47137eb - view on LGTM.com

new alerts:

  • 5 for Unused local variable
  • 1 for Unreachable code

@josef-pkt

josef-pkt commented Aug 10, 2021 •

Copy link
Copy Markdown
Member Author

there is still a python 2 compat left: except NameError: # for Python 2

and a lot of pep-8

pep-8 cleaning will add a lot of noise to history.
I think it's better to do a functional review before cleaning. A notebook can be based on unit test case.

@lgtm-com

lgtm-com Bot commented Aug 10, 2021

Copy link
Copy Markdown

This pull request introduces 6 alerts when merging 81f3c74 into 47137eb - view on LGTM.com

new alerts:

  • 5 for Unused local variable
  • 1 for Unreachable code

@josef-pkt

Copy link
Copy Markdown
Member Author

test failure/error of statespace in LinuxTest python_37_legacy_blas
no test failures in this PR, style check fails of course

def loglike(self, params):
return self.loglikeobs(params).sum(axis=0)

def nloglike(self, params):

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.

can be deleted. not used, I think

kwds.setdefault('centered', True)
return approx_fprime(params, self.loglike, **kwds).ravel()

def jac(self, params, **kwds):

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 is now named score_obs

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.

does it work with numdiff?
no unit test coverage

exog = self.exog
return np.dot(exog, params)

if self._results is None and params is None:

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.

parts here are not reached. can be deleted
I guess left over from an old pattern attaching _results to model

@josef-pkt

Copy link
Copy Markdown
Member Author

looks like all review comments in old PR #1935 have been resolved, code changed
extra exog handling in __init__ needs review. I didn't check if that follows the latest pattern where super handles missing and attaching attributes.

return data


def get_datamats(self):

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.

these can be added as attributes, no need for extra methods and repeated evaluation

@lgtm-com

lgtm-com Bot commented Aug 11, 2021

Copy link
Copy Markdown

This pull request introduces 6 alerts when merging 45f4d27 into 47137eb - view on LGTM.com

new alerts:

  • 5 for Unused local variable
  • 1 for Unreachable code

num_zvars = Z.shape[1]

xbeta = np.asarray(params_all[:num_xvars]) # reg eqn coefs
zbeta = np.asarray(params_all[num_xvars:num_xvars+num_zvars]) # selection eqn coefs

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.

not good names
which is which?

(I just wanted to quickly check whether binary parameters are before the main model linear parameters)

exog : array-like
Design / exogenous data
params : array-like, optional after fit has been called
Parameters of a linear model

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.

params should be the full params
split up with names params_main, params_select

@josef-pkt

Copy link
Copy Markdown
Member Author

X, Y, Z need mostly be renamed, we can still use them as short cut aliases only inside formulas

It's difficult to skim just pieces of code. Names are not self-explanatory.

@josef-pkt

Copy link
Copy Markdown
Member Author

in params the main model params should be first
zero-inflated models have inflation params first and that is annoying for consistency across models. e.g. comparing poisson with zip

@josef-pkt

Copy link
Copy Markdown
Member Author

too many small commit, partial squashing would be useful.

test_heckman could use a refactoring, e.g. stata results are included in test module not in a results module
refactoring to classes might also make it easier to read, and bring it closer to our usual pattern

@josef-pkt

Copy link
Copy Markdown
Member Author

I think I can squash all commits before "ENH: added log likelihood function for heckman". Those are all for 2-step AFAICS
The next group of commits I could leave mostly unsquashed. (those commits likely help understanding the code)
At the end are smaller cleanup commits that can be squashed

@josef-pkt

josef-pkt commented Jan 23, 2022 •

Copy link
Copy Markdown
Member Author

huge number of pep8 violation, line length and indentation.
This will mess up the history a lot, but github got better in finding module at previous commits

The results class has the results for the linear model,
The results for the selection equation are in select_res attribute.
In _fit_mle, the joint cov is split up, and AFAICS, the off-diagonal block is not kept.
Similarly, the extra params are split out and only the se are included in the results instance.

2-step has probit results instance

heckman_res.select_res._results, heckman_res2.select_res
(<statsmodels.discrete.discrete_model.ProbitResults at 0xa7911d370>,
 <statsmodels.base.model.LikelihoodModelResults at 0xa6458c550>)

So, this is similar to IV (2SLS) estimation, where main Results instance is for the final estimation and does not include the instrument regression.

I still don't know what would be a good structure #7903

@josef-pkt

Copy link
Copy Markdown
Member Author

llf for both 2-step and mle raises ValueError with shape mismatch in loglikeobs
ValueError: shapes (753,6) and (0,) not aligned: 6 (dim 1) != 0 (dim 0)

results params and cov_params do not match the underlying joint model, so many generic features will not work.

cov_type="hc0" in mle does not change bse. It looks like cov_type is silently ignored,
I'm not sure why, cov_type is send as kwarg to super().fit()
Heckman subclasses base.LikelihoodModel

@josef-pkt

Copy link
Copy Markdown
Member Author

I'm giving up for now on deciding on a proper model, results structure for endogenous sample selection models.
That requires quite a bit of background reading
Stata has also heckprobit and heckpoisson, heckpoisson requires numerical integration
A brief search for R finds https://cran.r-project.org/web/packages/sampleSelection/vignettes/selection.pdf
which also has more options for distributions.

The main option for getting heckman in this PR merged is to merge it without changing the basic structure as preliminary version, that will eventually (hopefully) replaced by something that fits better into a generic or general framework/pattern.

I think before going to endogenous sample selection, it will be easier to start with a continuous endogenous explanatory variables and control function approach, e.g. related to ivpoisson.

For binary endogenous explanatory variables, starting with treatment effect might be easier (and I know more about it).
(Treatment effect as in IPW is different because treatment not "endogenous" given observables. It assumes conditional independence or similar when conditioning on appropriate explanatory variables.)
But even endogenous treatment effect assumes that we have full sample information in contrast to selection model.
(kind of, we have observations for all individuals, but we only observe the chosen outcome and not the potential outcome.)
R sampleSelection includes tobit 5 which is switching regression.

@josef-pkt

Copy link
Copy Markdown
Member Author

@josef-pkt josef-pkt modified the milestones: 0.14, 0.15 Nov 30, 2022
@josef-pkt

Copy link
Copy Markdown
Member Author

I'm not sure whether to clean up the unit tests for pattern and style. That would be rewriting most of the test file.
The module includes data as json instead of in a csv.
Formatting is not pep-8.
Comparison to Stata does not use exported results module.

For the estimator and results pattern, I think now not to change it.
The plan is to get other classes with more generic names that will separate between full MLE and 2-stage which might not correspond to a full MLE model (e.g. if we include other control functions than inverse Mills ratio)
Heckman can stay as "Heckman" while others with be a variation "XxxSampleSelection" instead heckprobit, heckpoisson, ..., maybe "XxxSampleSelectionCF" and "XxxSampleSelectionMLE" separately.

@josef-pkt josef-pkt mentioned this pull request Jun 17, 2024
6 tasks
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