Skip to content

Fix for [Bug]: KeyError in DoubleMLPLIV.fit() with multiple instruments and store_predictions=True #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

vnastl
Copy link

@vnastl vnastl commented Jan 9, 2023

Description

In the case of multiple instruments, the function DoubleMLPLIV.fit() throws an error when executed with the parameter 'store_predictions=True', as reported in [Bug]: KeyError in DoubleMLPLIV.fit() with multiple instruments and store_predictions=True.

This occurs because the list of learner names is defined as follows in the case of multiple instruments:

line 269 in double_ml_pliv.py: param_names = ['ml_l', 'ml_r'] + ['ml_m_' + z_col for z_col in self._dml_data.z_cols]

The predictions for the nuisance functions are however saved using the keys 'ml_l', 'ml_m', 'ml_r', 'ml_g'.

line 397 - 405 in double_ml_pliv.py: 
        preds = {'predictions': {'ml_l': l_hat['preds'],
                                 'ml_m': m_hat['preds'],
                                 'ml_r': r_hat['preds'],
                                 'ml_g': g_hat['preds']},
                 'models': {'ml_l': l_hat['models'],
                            'ml_m': m_hat['models'],
                            'ml_r': r_hat['models'],
                            'ml_g': g_hat['models']}
                 }

To store the predictions if store_predictions=True in DoubleMLPLIV.fit(), we use preds['predictions'] but iterate over param_names. Hence, the reported KeyError occurred.

To solve this error, I propose to add the predictions and models made for each instrument in the case of multiple instruments, i.e. for the list of learners

['ml_m_' + z_col for z_col in self._dml_data.z_cols],

to the dictionaries preds['predictions'] and preds['models'], respectively.
The proposed code passes all (unit) tests.

Reference to Issues or PRs

[Bug]: KeyError in DoubleMLPLIV.fit() with multiple instruments and store_predictions=True #184

@SvenKlaassen
Copy link
Member

Thanks again, @vnastl. This problem should be solved with #182. Therefore, I will close this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants