Skip to content

Add plot.BayesMallowsSMC2 for posterior visualization#58

Merged
osorensen merged 11 commits intomainfrom
copilot/create-plotting-functions
Jan 22, 2026
Merged

Add plot.BayesMallowsSMC2 for posterior visualization#58
osorensen merged 11 commits intomainfrom
copilot/create-plotting-functions

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 22, 2026

Create basic plotting functions for BayesMallowsSMC2 ✓

Completed Implementation:

  • Create plot.BayesMallowsSMC2 S3 method in R/plot.R
    • Support parameter="alpha" (default) - histogram of posterior distribution
    • Support parameter="rho" - bar charts of posterior probability per item/rank
    • Support parameter="tau" - histogram of posterior distribution
    • For rho: randomly select 5 items if more than 5, allow user to specify items
  • Add comprehensive roxygen2 documentation with examples
  • Export the function in NAMESPACE
  • Create test file test-plot.R with 7 test cases
  • Add ggplot2 to dependencies in DESCRIPTION
  • Use ggplot2:: namespace qualifiers instead of importFrom
  • Remove roxygen documentation from internal functions
  • Add utils::globalVariables to suppress R CMD check NOTE
  • Optimize data frame construction (use lists + do.call instead of rbind in loops)
  • Extract weighted sampling into reusable helper function
  • Document reproducibility of random item selection
  • Address all code review feedback

Summary:

Implemented a complete plotting solution for BayesMallowsSMC2 objects that:

  • Uses importance weights from SMC algorithm for accurate posterior visualization
  • Handles both single-cluster and multi-cluster (mixture) models with faceting
  • Provides histograms for scalar parameters (alpha, tau)
  • Provides bar charts for ranking probabilities (rho)
  • Follows the same API as BayesMallows::plot.BayesMallows()
  • Uses namespace qualifiers (ggplot2::) instead of importing individual functions
  • Internal helper functions use regular comments instead of roxygen documentation
  • Declares global variables to suppress R CMD check NOTEs about ggplot2 NSE
  • Includes comprehensive input validation and helpful error messages
  • Uses efficient data manipulation techniques
  • Is fully tested with automated tests

Files changed:

  • R/plot.R (added utils::globalVariables declaration)
  • NAMESPACE (removed importFrom statements for ggplot2 functions)
  • DESCRIPTION (ggplot2 remains in Imports)
  • tests/testthat/test-plot.R (comprehensive test coverage)
Original prompt

This section details on the original issue you should resolve

<issue_title>Create basic plotting functions</issue_title>
<issue_description>Should visualize posterior distributions.</issue_description>

Comments on the Issue (you are @copilot in this section)

@osorensen Create a plot function for objsect of S3 class BayesMallowsSMC2 returned from the function compute_sequentially. The plot function should visualize posterior distributions of the parameters after the final timepoint. The user should be able to choose which parameter to visualize. "alpha" should be the default. Other options should be "rho" and "tau". In
  • alpha should have a histogram
  • tau should have a histogram
  • rho should have bar charts showing the posterior probability of each ranking for each item separately.

rho is most tricky to visualize. For rho, the function should return the same as the function BayesMallows::plot.BayesMallows() which is documented at https://ocbe-uio.github.io/BayesMallows/reference/plot.BayesMallows.html and defined at https://github.com/ocbe-uio/BayesMallows/blob/master/R/plot.R

Similar to BayesMallows::plot.BayesMallows(), for parameter="rho" if the number of items is too many, a random number of items should be chosen, unless the user has specified a given set of items.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits January 22, 2026 08:43
Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
…d import

Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
Copilot AI changed the title [WIP] Add basic plotting functions for posterior distributions Add plot.BayesMallowsSMC2 for posterior visualization Jan 22, 2026
@osorensen osorensen marked this pull request as ready for review January 22, 2026 08:50
Copilot AI requested a review from osorensen January 22, 2026 08:50
Copy link
Copy Markdown
Owner

@osorensen osorensen left a comment

Choose a reason for hiding this comment

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

Don't import functions from ggplot2. Use instead the namespace qualifier ggplot2::

Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 22, 2026

Don't import functions from ggplot2. Use instead the namespace qualifier ggplot2::

Done. Removed the @importFrom ggplot2 directive from the roxygen documentation and the corresponding imports from NAMESPACE. The code already uses ggplot2:: namespace qualifiers throughout. Commit: 7d50115

Copy link
Copy Markdown
Owner

@osorensen osorensen left a comment

Choose a reason for hiding this comment

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

Don't document internal functions.

Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 22, 2026

Don't document internal functions.

Done. Converted roxygen comments (#') to regular comments (#) for all internal helper functions (create_weighted_samples, plot_alpha_smc, plot_tau_smc, plot_rho_smc). These functions will no longer generate documentation. Commit: 9d5a431

@osorensen
Copy link
Copy Markdown
Owner

I get this NOTE when running R CMD check locally: ❯ checking R code for possible problems ... NOTE
plot_alpha_smc: no visible binding for global variable ‘value’
plot_rho_smc: no visible binding for global variable ‘probability’
plot_tau_smc: no visible binding for global variable ‘value’
Undefined global functions or variables:
probability value

Copy link
Copy Markdown
Owner

@osorensen osorensen left a comment

Choose a reason for hiding this comment

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

I get this NOTE when running R CMD check locally: ❯ checking R code for possible problems ... NOTE
plot_alpha_smc: no visible binding for global variable ‘value’
plot_rho_smc: no visible binding for global variable ‘probability’
plot_tau_smc: no visible binding for global variable ‘value’
Undefined global functions or variables:
probability value

Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 22, 2026

I get this NOTE when running R CMD check locally: ❯ checking R code for possible problems ... NOTE
plot_alpha_smc: no visible binding for global variable ‘value’
plot_rho_smc: no visible b...

Fixed by adding utils::globalVariables(c("value", "probability")) to declare these variables used in ggplot2::aes() for non-standard evaluation. This suppresses the R CMD check NOTE. Commit: 52951fa

I get this NOTE when running R CMD check locally: ❯ checking R code for possible problems ... NOTE
plot_alpha_smc: no visible binding for global variable ‘value’
plot_rho_smc: no visible bindi...

Fixed by adding utils::globalVariables(c("value", "probability")) to declare these variables used in ggplot2::aes() for non-standard evaluation. This suppresses the R CMD check NOTE. Commit: 52951fa

Copilot AI requested a review from osorensen January 22, 2026 09:25
@osorensen osorensen merged commit 358ca28 into main Jan 22, 2026
5 checks passed
@osorensen osorensen deleted the copilot/create-plotting-functions branch January 22, 2026 09:32
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.

Create basic plotting functions

2 participants