Skip to content
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

ENH: Add optional parameters for summary_col to indicate FEs #9191

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 37 additions & 1 deletion statsmodels/iolib/summary2.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@

def summary_col(results, float_format='%.4f', model_names=(), stars=False,
info_dict=None, regressor_order=(), drop_omitted=False,
include_r2=True):
include_r2=True, fixed_effects=None, fe_present='Yes',
fe_absent=''):
"""
Summarize multiple results instances side-by-side (coefs and SEs)

Expand Down Expand Up @@ -504,6 +505,14 @@
If True, only regressors in regressor_order will be included.
include_r2 : bool, optional
Includes R2 and adjusted R2 in the summary table.
fixed_effects : list[str], optional
List of categorical variables for which to indicate presence of
fixed effects.
fe_present : str, optional
String to indicate the presence of fixed effects. Default is "Yes".
fe_absent : str, optional
String to indicate the absence of fixed effects. Default is empty
string.
"""

if not isinstance(results, list):
Expand Down Expand Up @@ -562,6 +571,19 @@
idx.append(index[i + 1])
summ.index = idx

# add fixed effects info
if fixed_effects:
if not info_dict:
info_dict = {}

Check warning on line 577 in statsmodels/iolib/summary2.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/iolib/summary2.py#L577

Added line #L577 was not covered by tests
for fe in fixed_effects:
info_dict[fe + ' FE'] = (lambda x,
fe=fe,
fe_present=fe_present,
fe_absent=fe_absent: fe_present
if any(param.startswith(f'C({fe})')
for param in x.params.index)
else fe_absent)

# add infos about the models.
if info_dict:
cols = [_col_info(x, info_dict.get(x.model.__class__.__name__,
Expand All @@ -581,6 +603,20 @@

summ = summ.fillna('')

# fixed effects processing
if fixed_effects:
index_series = pd.Series(summ.index, index=summ.index)

Check warning on line 608 in statsmodels/iolib/summary2.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/iolib/summary2.py#L608

Added line #L608 was not covered by tests
skip_flag = index_series.apply(lambda x: any(x.startswith(f'C({fe})')
for fe in fixed_effects))
skip_next_flag = skip_flag.shift(fill_value=False)
final_skip = skip_flag | skip_next_flag
summ = summ[~final_skip]

Check warning on line 613 in statsmodels/iolib/summary2.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/iolib/summary2.py#L611-L613

Added lines #L611 - L613 were not covered by tests

r_squared_rows = summ.index[summ.index.str.contains('R-squared')]
r_squared_section = summ.loc[r_squared_rows]
summ = summ.drop(index=r_squared_rows)
summ = pd.concat([summ, r_squared_section])

Check warning on line 618 in statsmodels/iolib/summary2.py

View check run for this annotation

Codecov / codecov/patch

statsmodels/iolib/summary2.py#L615-L618

Added lines #L615 - L618 were not covered by tests

smry = Summary()
smry._merge_latex = True
smry.add_df(summ, header=True, align='l')
Expand Down