Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,BayesMallowsSMC2)
export(compute_sequentially)
export(precompute_topological_sorts)
export(set_hyperparameters)
Expand Down
78 changes: 78 additions & 0 deletions R/print.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#' Print Method for BayesMallowsSMC2 Objects
#'
#' Prints a summary of a BayesMallowsSMC2 object returned by
#' [compute_sequentially()].
#'
#' @param x An object of class \code{BayesMallowsSMC2}.
#' @param ... Additional arguments (currently unused).
#'
#' @return Invisibly returns the input object \code{x}.
#'
#' @details
#' The print method displays key information about the fitted Bayesian Mallows
#' model, including:
#' \itemize{
#' \item Number of particles
#' \item Number of timepoints
#' \item Number of items
#' \item Number of clusters
#' \item Log marginal likelihood
#' \item Final effective sample size (ESS)
#' \item Number of resampling events
#' }
#'
#' @export
#'
#' @examples
#' # Fit a model with complete rankings
#' set.seed(123)
#' mod <- compute_sequentially(
#' complete_rankings,
#' hyperparameters = set_hyperparameters(n_items = 5),
#' smc_options = set_smc_options(n_particles = 100, n_particle_filters = 1)
#' )
#'
#' # Print the model
#' print(mod)
#'
#' # or simply
#' mod
#'
print.BayesMallowsSMC2 <- function(x, ...) {
# Basic validation
if (!inherits(x, "BayesMallowsSMC2")) {
stop("x must be an object of class 'BayesMallowsSMC2'")
}

required_fields <- c("alpha", "rho", "ESS", "resampling", "log_marginal_likelihood")
missing_fields <- setdiff(required_fields, names(x))
if (length(missing_fields) > 0) {
stop("Object is missing required fields: ", paste(missing_fields, collapse = ", "))
}

# Extract dimensions
n_particles <- ncol(x$alpha)
n_timepoints <- length(x$ESS)
n_items <- dim(x$rho)[1]
n_clusters <- nrow(x$alpha)

# Count resampling events
n_resampling_events <- sum(x$resampling)

# Create header
cat("BayesMallowsSMC2 Model\n")
cat(strrep("=", nchar("BayesMallowsSMC2 Model")), "\n\n", sep = "")

# Display basic information
cat(sprintf("%-25s %s\n", "Number of particles:", n_particles))
cat(sprintf("%-25s %s\n", "Number of timepoints:", n_timepoints))
cat(sprintf("%-25s %s\n", "Number of items:", n_items))
cat(sprintf("%-25s %s\n\n", "Number of clusters:", n_clusters))

# Display model fit information
cat(sprintf("%-25s %.2f\n", "Log marginal likelihood:", x$log_marginal_likelihood))
cat(sprintf("%-25s %.2f\n", "Final ESS:", x$ESS[n_timepoints]))
cat(sprintf("%-25s %d/%d\n", "Resampling events:", n_resampling_events, n_timepoints))

invisible(x)
}
49 changes: 49 additions & 0 deletions man/print.BayesMallowsSMC2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("print method works for BayesMallowsSMC2 objects", {
set.seed(123)
mod <- compute_sequentially(
complete_rankings,
hyperparameters = set_hyperparameters(n_items = 5),
smc_options = set_smc_options(n_particles = 100, n_particle_filters = 1)
)

# Test that print method runs without error
expect_error(print(mod), NA)

# Test that print returns the object invisibly
expect_identical(print(mod), mod)

# Capture output and verify it contains expected content
output <- capture.output(print(mod))
expect_true(any(grepl("BayesMallowsSMC2 Model", output)))
expect_true(any(grepl("Number of particles:", output)))
expect_true(any(grepl("Number of timepoints:", output)))
expect_true(any(grepl("Number of items:", output)))
expect_true(any(grepl("Number of clusters:", output)))
expect_true(any(grepl("Log marginal likelihood:", output)))
expect_true(any(grepl("Final ESS:", output)))
expect_true(any(grepl("Resampling events:", output)))
})

test_that("print method works with partial rankings", {
set.seed(456)
mod <- compute_sequentially(
partial_rankings,
hyperparameters = set_hyperparameters(n_items = 5),
smc_options = set_smc_options(n_particles = 50, n_particle_filters = 1)
)

# Test that print method runs without error
expect_error(print(mod), NA)

# Capture output and verify it contains expected content
output <- capture.output(print(mod))
expect_true(any(grepl("BayesMallowsSMC2 Model", output)))
})