Repository navigation
Conversation
|
only 36 lines of code to implement control function for all GLM families, the advantage of reusable parts 👍 If we allow for only one endogenous regressor, then we can easily add a family option for the 1st stage. Adding multiple endogenous regressors: update In the test case we only have a binary 1st stage regressor (i.e. 2 sample case). So using Logit as a first stage instead of OLS does not change the results of the second stage (last 3 parameters in test case). same if I use Probit as a first stage, GLM-binomial with probit link |
05c802f to
b7195a5
Compare
| ) | ||
| res = mod.fit() | ||
| sli = slice(-3, None, None) | ||
| assert_allclose(res.params[sli], params2[sli], rtol=1e-7) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
| res = mod.fit() | ||
| sli = slice(-3, None, None) | ||
| assert_allclose(res.params[sli], params2[sli], rtol=1e-7) | ||
| assert_allclose(res.bse[sli], bse2[sli], rtol=1e-6) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
| sli = slice(-3, None, None) | ||
| assert_allclose(res.params[sli], params2[sli], rtol=1e-7) | ||
| assert_allclose(res.bse[sli], bse2[sli], rtol=1e-6) | ||
| assert_allclose(res.tvalues[sli], tvalues2[sli], rtol=1e-5) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
|
CodeQL complains about |
|
returning the second stage outcome model results, attached to res_gmm inside This has the standard GLM model attached, predict is the standard GLM predict. We can compute conditional expectation for given unobserved random term (control function values), e.g. set them to zero for prediction ignoring the random effect. One possibility to make this cleaner: Add |
|
aside to the test case |
| res_out = res.results_outcome | ||
| assert_allclose(res_out.params, params2[sli], rtol=1e-7) | ||
| assert_allclose(res_out.bse, bse2[sli], rtol=1e-6) | ||
| assert_allclose(res_out.tvalues, tvalues2[sli], rtol=1e-5) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
| assert_allclose(res_out.params, params2[sli], rtol=1e-7) | ||
| assert_allclose(res_out.bse, bse2[sli], rtol=1e-6) | ||
| assert_allclose(res_out.tvalues, tvalues2[sli], rtol=1e-5) | ||
| assert_allclose(res_out.pvalues, pvalues2[sli], rtol=1e-5) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
| assert_allclose(res.params[sli], params2[sli], rtol=1e-7) | ||
| assert_allclose(res.bse[sli], bse2[sli], rtol=1e-6) | ||
| assert_allclose(res.tvalues[sli], tvalues2[sli], rtol=1e-5) | ||
| assert_allclose(res.pvalues[sli], pvalues2[sli], rtol=1e-5) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
|
|
||
| # check attached outcome model | ||
| res_out = res.results_outcome | ||
| assert_allclose(res_out.params, params2[sli], rtol=1e-7) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
| # check attached outcome model | ||
| res_out = res.results_outcome | ||
| assert_allclose(res_out.params, params2[sli], rtol=1e-7) | ||
| assert_allclose(res_out.bse, bse2[sli], rtol=1e-6) |
Check failure
Code scanning / CodeQL
Unhashable object hashed
|
I guess this is useful enough to include in 0.15 even if there is only one endogenous explanatory allowed. Need to check again what options for the functional form of the control function are available. Can we replicate the usual Wooldridge cases? missing docstrings, especially for class needs notebook for doc examples |
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new statsmodels.causal module that implements a control function approach for handling endogenous explanatory variables in GLM models. The implementation adds support for instrumental variable estimation using a two-stage control function method, currently supporting GLM models with one endogenous regressor.
- Adds GMMIVCF and GLMIVCF classes for GMM and GLM instrumental variable control function estimation
- Implements two-stage estimation with flexible control function specifications
- Provides comprehensive test coverage with reference data validation
Reviewed Changes
Copilot reviewed 4 out of 6 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| statsmodels/causal/init.py | Creates the new causal module with test framework integration |
| statsmodels/causal/ivcontrol_function.py | Core implementation of control function classes and estimation methods |
| statsmodels/causal/tests/test_ivcontrol.py | Test suite validating model functionality against reference results |
| statsmodels/causal/tests/results/temp_residinclude.csv | Test data containing instrument, endogenous, and outcome variables |
Comments suppressed due to low confidence (1)
statsmodels/causal/ivcontrol_function.py:126
- [nitpick] Parameter names use generic 's{i}' format which is not descriptive. Consider using more meaningful names that distinguish between first-stage and outcome parameters.
param_names=[f"s{i}" for i in range(len(params))])
| mod = GLMIVCF(endog, explan, endog_explan, instruments, | ||
| family=families.Binomial(), | ||
| family_first=families.Binomial(link=families.links.Probit()), | ||
| cf_kwds = {"resid": "resid_response"}, |
There was a problem hiding this comment.
[nitpick] Missing space after equals sign in dictionary. Should be cf_kwds = {"resid": "resid_response"} for consistency with Python formatting standards.
| def momcond(params, modelcf=None): | ||
| mod_first = modelcf.model_first | ||
|
|
||
| y2 = modelcf.endog | ||
| x2 = modelcf.explan | ||
|
|
||
| params1 = params[:2] | ||
| params2 = params[2:] |
There was a problem hiding this comment.
Magic number '2' is used for slicing parameters without explanation. Consider using a named constant or documenting why the first 2 parameters belong to the first stage model.
| def momcond(params, modelcf=None): | |
| mod_first = modelcf.model_first | |
| y2 = modelcf.endog | |
| x2 = modelcf.explan | |
| params1 = params[:2] | |
| params2 = params[2:] | |
| # Number of parameters belonging to the first-stage model | |
| NUM_FIRST_STAGE_PARAMS = 2 | |
| def momcond(params, modelcf=None): | |
| mod_first = modelcf.model_first | |
| y2 = modelcf.endog | |
| x2 = modelcf.explan | |
| params1 = params[:NUM_FIRST_STAGE_PARAMS] | |
| params2 = params[NUM_FIRST_STAGE_PARAMS:] |
| param_names=[f"s{i}" for i in range(len(params))]) | ||
| mod_gmm.model = self | ||
| res_gmm = mod_gmm.fit(start_params=params, maxiter=0) | ||
| res_out = res_outcome._results # res_outcome is wrapped results |
There was a problem hiding this comment.
Accessing private attribute _results suggests tight coupling. Consider using a public interface or documenting why direct access to private attributes is necessary.
|
semi-random idea to have flexibility in control function for which residuals and which transformation of them should be used: provide option to use callback function the the user can provide, function takes first stage results instance as argument and should return 1-dim or 2-dim control functions. This way we could hard code just a simple default, e.g. polynomial of resid_pearson, and user can use callback if other control functions are desired. If we allow for multiple endogenous regressors, then callback would have to be a list (or maybe better a tuple) of functions including possibly None for keeping default for an endogenous regressors. |
|
notebook with example of GLMIVCF is in my local notebook |
getting started with #7689
endogenous regressors in nonlinear models
currently GLM, and only one endogenous regressor
this add
statsmodels.causaldirectory