From 1746391774b0c3924a76c81cafd8a827fe67aa74 Mon Sep 17 00:00:00 2001 From: jsr-p Date: Thu, 8 Feb 2024 09:26:18 +0100 Subject: [PATCH 1/2] Handle case of negative model_ss for IV2SLS --- stargazer/translators/linearmodels.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/stargazer/translators/linearmodels.py b/stargazer/translators/linearmodels.py index 78aec67..52d6aa2 100644 --- a/stargazer/translators/linearmodels.py +++ b/stargazer/translators/linearmodels.py @@ -2,7 +2,7 @@ Compatibility layer with results from linearmodels. """ -from math import sqrt +from math import nan, sqrt from linearmodels.iv.results import IVResults, OLSResults from linearmodels.panel.results import ( @@ -39,6 +39,17 @@ linear_model_map_iv = dict() +def handle_resid_std_err(model): + if isinstance(model, IVResults): + try: + resid_std_err = sqrt(model.model_ss / model.df_resid) + except ValueError: # Negative model_ss IV + resid_std_err = nan + else: + resid_std_err = sqrt(model.model_ss / model.df_resid) + return resid_std_err + + def extract_model_data(model): data = {} if isinstance(model, (PanelEffectsResults, RandomEffectsResults, PanelResults)): @@ -62,7 +73,7 @@ def extract_model_data(model): data["cov_names"] = model.params.index.values data["conf_int_low_values"] = model.conf_int().lower data["conf_int_high_values"] = model.conf_int().upper - data["resid_std_err"] = sqrt(model.model_ss / model.df_resid) + data["resid_std_err"] = handle_resid_std_err(model) data["f_statistic"] = model.f_statistic.stat data["f_p_value"] = model.f_statistic.pval data["r2_adj"] = None From fcd4eae364833d60cd7813d02f02625ec2b2f454 Mon Sep 17 00:00:00 2001 From: jsr-p Date: Thu, 8 Feb 2024 12:28:31 +0100 Subject: [PATCH 2/2] Update resid_std_error linearmodels Update resid_std_error linearmodels Update resid_std_error linearmodels --- stargazer/translators/linearmodels.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/stargazer/translators/linearmodels.py b/stargazer/translators/linearmodels.py index 52d6aa2..46f9a57 100644 --- a/stargazer/translators/linearmodels.py +++ b/stargazer/translators/linearmodels.py @@ -2,7 +2,7 @@ Compatibility layer with results from linearmodels. """ -from math import nan, sqrt +from math import sqrt from linearmodels.iv.results import IVResults, OLSResults from linearmodels.panel.results import ( @@ -39,17 +39,6 @@ linear_model_map_iv = dict() -def handle_resid_std_err(model): - if isinstance(model, IVResults): - try: - resid_std_err = sqrt(model.model_ss / model.df_resid) - except ValueError: # Negative model_ss IV - resid_std_err = nan - else: - resid_std_err = sqrt(model.model_ss / model.df_resid) - return resid_std_err - - def extract_model_data(model): data = {} if isinstance(model, (PanelEffectsResults, RandomEffectsResults, PanelResults)): @@ -73,7 +62,7 @@ def extract_model_data(model): data["cov_names"] = model.params.index.values data["conf_int_low_values"] = model.conf_int().lower data["conf_int_high_values"] = model.conf_int().upper - data["resid_std_err"] = handle_resid_std_err(model) + data["resid_std_err"] = sqrt(model.s2) data["f_statistic"] = model.f_statistic.stat data["f_p_value"] = model.f_statistic.pval data["r2_adj"] = None