-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added `plot.SMCMallows()` method + example (#114) * Changed class of SMC outputs to SMCMallows (#114) This makes proper dispatching of possible, withou having any other apparent consequences. * Adapted SMC vignette and unit tests to new `plot()` method (#114) * Fixed CodeFactor issue Redundant blank line at the end of a code block should be deleted. * Gathererd SMC plot functions on same file (#114) This should help with future DRYing, perhaps both subfunctions should eventually be internalized? * Increment version number to 1.2.0.9004 * Updated NEWS.md * Updated build CI config file Using template from https://github.com/r-lib/actions/tree/v2-branch/examples#standard-ci-workflow * Added `plot.SMCMallows()` method (#263) * Added `plot.SMCMallows()` method + example (#114) * Changed class of SMC outputs to SMCMallows (#114) This makes proper dispatching of possible, withou having any other apparent consequences. * Adapted SMC vignette and unit tests to new `plot()` method (#114) * Fixed CodeFactor issue Redundant blank line at the end of a code block should be deleted. * Gathererd SMC plot functions on same file (#114) This should help with future DRYing, perhaps both subfunctions should eventually be internalized? * Increment version number to 1.2.0.9004 * Updated NEWS.md * Updated docs * Removed duplicated function As mentioned in the in-code comment, `scalefun()` was already defined in a different source file. * Increment version number to 1.2.1.9001 Co-authored-by: Øystein Sørensen <oystein_sorensen@hotmail.com> * Fix documentation for `plot.SMCMallows()` (#266) * Fixed links to other package functions Links were formatted wrongly assuming the package was setup to support markdown-formatted links. This fixes it. * Increment version number to 1.2.1.9002 * Reverting changes to the `compute_mallows()` docs * Reverting changes to NEWS.md Textual change is automatically introduced by `usethis::use_dev_version()`. * Deprecated `plot_*_posterior()` (#267) Following the conversation started [here](#263 (comment)), this commit moves the superseded subfunctions of `plot.SMCMallows()` into the `smc_mallows_deprecated.R` file for eventual removal. The subfunctions themselves were renamed and test units to test the deprecation warnings were written. * Matching SMC defaults to their MCMC counterparts (#269) * Updated documentation Some text were taken from the original implementation and make no longer sense. * Matched SMC defaults with their original counterparts (#114) * SMC metric defaults to footrule (#114) * SMC leap_size defaults to 1 (#114) * SMC alpha_prop_sd defaults to 0.5 (#114) * SMC alpha_max defaults to 1e6 (#114) * SMC lambda defautls to 0.1 (#114) * Update DESCRIPTION Incremented development version. Co-authored-by: Øystein Sørensen <oystein_sorensen@hotmail.com> * Added changes from PR #269 to NEWS.md Those are important to mention, since they technically break backwards compatibility. I forgot to do that on he PR itself, thankfully there's a second chance. :) Co-authored-by: Waldir Leoncio <w.l.netto@medisin.uio.no>
- Loading branch information
Showing
48 changed files
with
573 additions
and
517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#' @title Plot SMC Posterior Distributions | ||
#' @description Plot posterior distributions of SMC-Mallow parameters. | ||
#' @param x An object of type \code{SMC-Mallows}, returned for example from | ||
#' \code{\link{smc_mallows_new_users}}. | ||
#' @param nmc Number of Monte Carlo samples | ||
#' @param burnin A numeric value specifying the number of iterations | ||
#' to discard as burn-in. Defaults to \code{model_fit$burnin}, and must be | ||
#' provided if \code{model_fit$burnin} does not exist. See | ||
#' \code{\link{assess_convergence}}. | ||
#' @param parameter Character string defining the parameter to plot. Available | ||
#' options are \code{"alpha"} and \code{"rho"}. | ||
#' @param time Integer determining the update slice to plot | ||
#' @param C Number of cluster | ||
#' @param colnames A vector of item names. If NULL, generic names are generated | ||
#' for the items in the ranking. | ||
#' @param items Either a vector of item names, or a vector of indices. If NULL, | ||
#' five items are selected randomly. | ||
#' @param ... Other arguments passed to \code{\link[base]{plot}} (not used). | ||
#' @return A plot of the posterior distributions | ||
#' @author Waldir Leoncio | ||
#' @export | ||
#' @example /inst/examples/plot.SMCMallows_example.R | ||
plot.SMCMallows <- function(x, nmc = nrow(x$rho_samples[, 1, ]), burnin = 0, | ||
parameter = "alpha", time = ncol(x$rho_samples[, 1, ]), C = 1, | ||
colnames = NULL, items = NULL, ...) { | ||
|
||
if (parameter == "alpha") { | ||
output <- x$alpha_samples[, time] | ||
plot_alpha_smc(output, nmc, burnin) | ||
} else if (parameter == "rho") { | ||
output <- x$rho_samples[, , time] | ||
plot_rho_smc(output, nmc, burnin, C, colnames, items) | ||
} else { | ||
stop("parameter must be either 'alpha' or 'rho'.") | ||
} | ||
} | ||
|
||
plot_alpha_smc <- function(output, nmc, burnin) { | ||
alpha_samples_table <- data.frame(iteration = 1:nmc, value = output) | ||
|
||
plot_posterior_alpha <- ggplot2::ggplot(alpha_samples_table, ggplot2::aes_(x = ~value)) + | ||
ggplot2::geom_density() + | ||
ggplot2::xlab(expression(alpha)) + | ||
ggplot2::ylab("Posterior density") + | ||
ggplot2::ggtitle(label = "Implemented SMC scheme") + | ||
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5)) | ||
|
||
print(plot_posterior_alpha) | ||
} | ||
|
||
plot_rho_smc <- function(output, nmc, burnin, C, colnames = NULL, items = NULL) { | ||
n_items <- dim(output)[2] | ||
|
||
if (is.null(items) && n_items > 5) { | ||
message("Items not provided by user or more than 5 items in a ranking. Picking 5 at random.") | ||
items <- sample(seq_len(n_items), 5, replace = FALSE) | ||
items <- sort(items) | ||
} else if (is.null(items) && n_items <= 5) { | ||
items <- seq_len(n_items) | ||
items <- sort(items) | ||
} | ||
|
||
# do smc processing here | ||
smc_plot <- smc_processing(output = output, colnames = colnames) | ||
|
||
if (!is.character(items)) { | ||
items <- unique(smc_plot$item)[items] | ||
} | ||
|
||
iteration <- rep(seq_len(nmc), times = n_items) | ||
df <- cbind(iteration, smc_plot) | ||
|
||
if (C == 1) { | ||
df <- cbind(cluster = "Cluster 1", df) | ||
} | ||
|
||
df <- df[df$iteration > burnin & df$item %in% items, , drop = FALSE] | ||
|
||
# Compute the density, rather than the count, since the latter | ||
# depends on the number of Monte Carlo samples | ||
df <- aggregate(list(n = df$iteration), | ||
list(cluster = df$cluster, item = df$item, value = df$value), | ||
FUN = length | ||
) | ||
df$pct <- df$n / sum(df$n) | ||
|
||
df$item <- factor(df$item, levels = c(items)) | ||
|
||
# Finally create the plot | ||
p <- ggplot2::ggplot(df, ggplot2::aes(x = .data$value, y = .data$pct)) + | ||
ggplot2::geom_col() + | ||
ggplot2::scale_x_continuous(labels = scalefun) + | ||
ggplot2::xlab("rank") + | ||
ggplot2::ylab("Posterior probability") | ||
|
||
if (C == 1) { | ||
p <- p + ggplot2::facet_wrap(~ .data$item) | ||
} else { | ||
p <- p + ggplot2::facet_wrap(~ .data$cluster + .data$item) | ||
} | ||
|
||
return(p) | ||
} |
Oops, something went wrong.