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

Conversation

adrianmross
Copy link

Researchers tend to show they used categorical variables without all the coefficients, saying whether they included a fixed effect. This PR adds that feature.

Example

import numpy as np
import pandas as pd
import seaborn as sns
import statsmodels.formula.api as smf
from statsmodels.iolib.summary2 import summary_col

# Load the diamonds dataset
diamonds = sns.load_dataset('diamonds')

# Adapted regressions for the diamonds dataset
regressions = [
    (smf.ols('np.log(price) ~ carat', data=diamonds).fit(), 'log(Price) ~ Carat'),
    (smf.ols('np.log(price) ~ np.log(carat)', data=diamonds).fit(), 'log(Price) ~ log(Carat)'),
    (smf.ols('np.log(price) ~ C(cut)', data=diamonds).fit(), 'log(Price) ~ C(Cut)'),
    (smf.ols('np.log(price) ~ C(clarity)', data=diamonds).fit(), 'log(Price) ~ C(Clarity)'),
    (smf.ols('np.log(price) ~ carat + C(cut) + C(clarity)', data=diamonds).fit(), 'log(Price) ~ Carat + C(Cut) + C(Clarity)')
]

# Output summary of all regressions
summary = summary_col([reg[0] for reg in regressions],
                      model_names=[f'{i}. '+reg[1] for i, reg in enumerate(regressions, 1)]
                    )

summary
Screenshot 2024-04-02 at 9 50 22 PM

With more complicated regressions or broader categories, this can quickly overwhelm the summary.

Now, with amended function.

"""
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.
"""

summary = summary_col([reg[0] for reg in regressions],
                      model_names=[f'{i}. '+reg[1] for i, reg in enumerate(regressions, 1)],
                      fixed_effects=['cut', 'clarity'],
                    )
Screenshot 2024-04-02 at 9 50 30 PM

Much more concise.

@donbowen
Copy link

This looks like a nice feature!

@pep8speaks
Copy link

pep8speaks commented Apr 10, 2024

Hello @adrianmross! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2024-04-10 17:09:53 UTC

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.

None yet

3 participants