Skip to content

Visualization: Collapse by [random] group#597

Merged
strengejacke merged 13 commits intomainfrom
strengejacke/issue130
Feb 15, 2026
Merged

Visualization: Collapse by [random] group#597
strengejacke merged 13 commits intomainfrom
strengejacke/issue130

Conversation

@strengejacke
Copy link
Member

Fixes #130

@gemini-code-assist
Copy link

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 introduces a new utility function, collapse_by_group, to facilitate the visualization and analysis of mixed models. It allows users to simplify complex datasets by averaging response variables based on specified random effect grouping factors, making it easier to interpret group-level trends and patterns.

Highlights

  • New Functionality: Introduced a new function, collapse_by_group, designed to average response variables over levels of random effect grouping factors in mixed models.
  • Data Handling Flexibility: Enabled the function to process either raw data points or partial residuals, providing flexibility for different analysis needs.
  • Export and Documentation: Ensured the new function is exported and properly documented for user accessibility and understanding.
Changelog
  • NAMESPACE
    • Exported collapse_by_group.
  • R/collapse_by_group.R
    • Added a new function collapse_by_group to collapse raw data or partial residuals by random effect groups.
    • Implemented logic to handle mixed models, identify grouping factors, and perform aggregation.
  • man/collapse_by_group.Rd
    • Added comprehensive documentation for the collapse_by_group function, including its purpose, parameters, return value, and examples.
Activity
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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

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

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 introduces a new function collapse_by_group for averaging raw data over random effect groups, which is a valuable addition for visualizing mixed models. The implementation is generally solid, but I have a few suggestions to improve its robustness and correctness. I've identified a high-severity issue with the handling of ordinal response variables that could lead to incorrect results, a medium-severity issue with brittle column selection logic, and a medium-severity issue with an uninformative and out-of-sync example in the documentation. Addressing these points will make the new function more reliable.

@strengejacke strengejacke requested a review from Copilot February 15, 2026 15:17
@strengejacke
Copy link
Member Author

@gemini-code-assist review

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 introduces a valuable new feature for visualizing mixed models by allowing data points to be collapsed by random effect groups. The implementation is mostly solid, with new tests, documentation, and vignette updates. However, I've identified two critical issues in the core logic that could lead to incorrect plots. My review includes specific suggestions to address these bugs and improve the robustness of the new functionality.

Copy link
Contributor

Copilot AI left a 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 adds functionality to collapse/average data points by random effect grouping factors when plotting mixed models. It addresses issue #130 by implementing a new collapse_by_group() function and adding a collapse_group argument to plotting methods.

Changes:

  • New collapse_by_group() function to extract and average raw data points over random effect grouping factors for mixed models
  • Added collapse_group parameter to plot(), visualisation_recipe(), and tinyplot() methods for displaying collapsed data points
  • Updated documentation and vignette with examples showing how to use the collapsing feature

Reviewed changes

Copilot reviewed 11 out of 15 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
R/collapse_by_group.R New function implementing data collapsing by random effects groups
R/visualisation_recipe.R Added collapse_group parameter to visualization recipe methods
R/visualisation_recipe_internal.R Added internal helper .visualization_recipe_collapsed_data() for rendering collapsed data
R/tinyplot.R Integrated collapse_group parameter for tinyplot plotting method
man/collapse_by_group.Rd Documentation for new function
man/visualisation_recipe.estimate_predicted.Rd Updated documentation with new parameter
vignettes/plotting.Rmd Added examples demonstrating the collapse feature
tests/testthat/test-plot-show_data.R New tests for plotting with collapsed data
NEWS.md Documented new feature
NAMESPACE Exported new function
Comments suppressed due to low confidence (1)

R/visualisation_recipe_internal.R:362

  • The logic at lines 325 and 335 prevents showing both raw data and collapsed data, or both residuals and collapsed data. When collapse_by is specified, it will only show collapsed data (lines 349-362), but not the raw data or residuals. This means users cannot layer both raw and collapsed data in the same plot. While this might be intentional, consider whether this limitation aligns with expected user behavior or if it should be documented more clearly.
  if (show_data && is.null(collapse_by)) {
    layers[[paste0("l", l)]] <- .visualization_recipe_rawdata(x, aes, numeric_as_discrete)
    # Update with additional args
    if (!is.null(point)) {
      layers[[paste0("l", l)]] <- utils::modifyList(layers[[paste0("l", l)]], point)
    }
    l <- l + 1
  }

  # add residual data as next lowest layer
  if (show_residuals && is.null(collapse_by)) {
    layers[[paste0("l", l)]] <- .visualization_recipe_residuals(
      x,
      aes,
      numeric_as_discrete
    )
    # Update with additional args
    if (!is.null(point)) {
      layers[[paste0("l", l)]] <- utils::modifyList(layers[[paste0("l", l)]], point)
    }
    l <- l + 1
  }

  # next possible data layer is collapsing by group ---------------
  if (!is.null(collapse_by) && (show_data || show_residuals)) {
    layers[[paste0("l", l)]] <- .visualization_recipe_collapsed_data(
      x,
      aes,
      collapse_by,
      numeric_as_discrete,
      show_residuals
    )
    # Update with additional args
    if (!is.null(point)) {
      layers[[paste0("l", l)]] <- utils::modifyList(layers[[paste0("l", l)]], point)
    }
    l <- l + 1
  }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 37 to 51
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function doesn't handle collapse_by = TRUE as documented. The documentation states that setting collapse_group = TRUE should collapse by the first random effect groups (see man/visualisation_recipe.estimate_predicted.Rd lines 84-86), but the implementation only checks is.null(collapse_by) at line 37. If a user passes collapse_by = TRUE, it will fail at line 49 with an error "Could not find TRUE column." To fix this, add a check like if (isTRUE(collapse_by)) before line 37 to handle this case.

Copilot uses AI. Check for mistakes.
@strengejacke strengejacke merged commit ad5c2be into main Feb 15, 2026
15 of 23 checks passed
@strengejacke strengejacke deleted the strengejacke/issue130 branch February 15, 2026 17:36
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.

Visualization: Collapse by [random] group

1 participant

Comments