-
-
Notifications
You must be signed in to change notification settings - Fork 40
Deal with / fix tidy() method in the _marginaleffects_ methods for model_parameters()
#1157
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
Conversation
…`model_parameters()` Fixes #1156
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @strengejacke, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request refines the model_parameters() function's integration with the marginaleffects package. The primary goal is to enhance the tidy() method's output by intelligently removing redundant columns, ensuring a cleaner and more focused presentation of model parameters. This improves data clarity and usability for users analyzing marginal effects.
Highlights
- Refined
marginaleffectsIntegration: Themodel_parameters.marginaleffectsmethod now includes logic to identify and remove superfluous columns from thetidy()output generated by themarginaleffectspackage. This ensures that only relevant data, such as variable names andbyvariables, are retained. - Improved Data Clarity: By stripping redundant columns, the output of
model_parameters()when used withmarginaleffectsobjects becomes more concise and easier to interpret, reducing visual clutter and focusing on essential statistical results. - Expanded Test Coverage: New test cases have been added to validate the correct handling of
marginaleffectsobjects, specifically verifying that redundant columns are successfully removed and that the output structure and column names are as expected, including scenarios involvingbyvariables.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request fixes an issue where model_parameters() for marginaleffects objects would include redundant columns from the original data. The change introduces logic to identify and remove these columns, cleaning up the output. The fix is accompanied by new unit tests that verify the corrected behavior.
My review focuses on improving the readability of the new logic. The implementation is correct, but breaking it down into smaller, named steps can enhance maintainability.
R/methods_marginaleffects.R
Outdated
| to_remove <- setdiff( | ||
| # all columns | ||
| union( | ||
| colnames(marginaleffects::components(model, "newdata")), | ||
| colnames(marginaleffects::components(model, "modeldata")) | ||
| ), | ||
| # columns we want to keep | ||
| marginaleffects::components(model, "variable_names_by") | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For improved readability and maintainability, consider breaking down the calculation of to_remove into intermediate variables. This makes the logic easier to follow by explicitly naming the sets of columns being manipulated.
all_data_cols <- union(
colnames(marginaleffects::components(model, "newdata")),
colnames(marginaleffects::components(model, "modeldata"))
)
by_cols <- marginaleffects::components(model, "variable_names_by")
to_remove <- setdiff(all_data_cols, by_cols)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes the tidy() method in the marginaleffects methods for model_parameters() by removing redundant columns that are not needed in the output. The fix addresses issue #1156 by ensuring that only relevant columns are retained in the model parameters output.
- Adds logic to identify and remove redundant columns from marginaleffects tidy output
- Implements comprehensive test coverage for the new column filtering functionality
- Updates package version to reflect the bug fix
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| R/methods_marginaleffects.R | Adds column filtering logic to remove redundant columns from tidy output |
| tests/testthat/test-marginaleffects.R | Adds test cases to verify correct column dimensions and names after filtering |
| DESCRIPTION | Increments package version number |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| ), | ||
| # columns we want to keep | ||
| marginaleffects::components(model, "variable_names_by") | ||
| ) |
Copilot
AI
Aug 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The marginaleffects::components() function calls may fail if the model object doesn't have the expected components. Consider adding error handling or checking if these components exist before calling colnames() on them.
| # columns we want to keep | ||
| marginaleffects::components(model, "variable_names_by") | ||
| ) | ||
| tidy_model <- tidy_model[, !colnames(tidy_model) %in% to_remove, drop = FALSE] |
Copilot
AI
Aug 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If to_remove is empty or contains non-existent column names, this subsetting could behave unexpectedly. Consider adding a check to ensure to_remove contains valid column names that exist in tidy_model.
| tidy_model <- tidy_model[, !colnames(tidy_model) %in% to_remove, drop = FALSE] | |
| # Only remove columns that exist in tidy_model | |
| valid_to_remove <- intersect(to_remove, colnames(tidy_model)) | |
| if (length(setdiff(to_remove, valid_to_remove)) > 0 && length(to_remove) > 0) { | |
| warning("Some columns specified for removal do not exist in tidy_model: ", paste(setdiff(to_remove, valid_to_remove), collapse = ", ")) | |
| } | |
| tidy_model <- tidy_model[, !colnames(tidy_model) %in% valid_to_remove, drop = FALSE] |
Fixes #1156