Skip to content

Commit

Permalink
added density plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Verity committed Jun 26, 2024
1 parent 06b4511 commit f06fd1e
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Imports:
cowplot,
dplyr,
magrittr,
GGally
GGally,
tidyr
Suggests:
testthat,
covr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(define_params)
export(plot_autocorrelation)
export(plot_cor_mat)
export(plot_credible)
export(plot_density)
export(plot_mc_acceptance)
export(plot_pairs)
export(plot_rung_loglike)
Expand All @@ -30,6 +31,7 @@ importFrom(stats,quantile)
importFrom(stats,runif)
importFrom(stats,setNames)
importFrom(stats,var)
importFrom(tidyr,pivot_longer)
importFrom(utils,setTxtProgressBar)
importFrom(utils,txtProgressBar)
useDynLib(drjacoby, .registration = TRUE)
50 changes: 50 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ plot_cor_mat <- function(x, show = NULL, phase = "sampling", param_names = NULL)
#' @export
plot_pairs <- function(x, show = NULL, hide = NULL) {

# check inputs
assert_class(x, "drjacoby_output")

# avoid "no visible binding" note
chain <- NULL

Expand All @@ -575,9 +578,11 @@ plot_pairs <- function(x, show = NULL, hide = NULL) {
# get parameter names and apply show and hide conditions
param_names <- setdiff(names(param_draws), c("chain", "sample"))
if (!is.null(show)) {
assert_string(show)
param_names <- intersect(param_names, show)
}
if (!is.null(hide)) {
assert_string(hide)
param_names <- setdiff(param_names, hide)
}
if (length(param_names) == 0) {
Expand All @@ -590,3 +595,48 @@ plot_pairs <- function(x, show = NULL, hide = NULL) {
columns = param_names,
lower = list(continuous = wrap("points", size = 0.5))) + theme_bw()
}

#------------------------------------------------
#' @title Produce density plots
#'
#' @description Density plots of all parameters. Use \code{show} and \code{hide}
#' to be more specific about which parameters to plot.
#'
#' @inheritParams plot_trace
#'
#' @importFrom tidyr pivot_longer
#' @export
plot_density <- function(x, show = NULL, hide = NULL) {

# avoid "no visible binding" notes
value <- NULL

# check inputs
assert_class(x, "drjacoby_output")

# subsample posterior draws
param_draws <- sample_chains(x, sample_n = 1e3, keep_chain_index = FALSE)

# get parameter names and apply show and hide conditions
param_names <- setdiff(names(param_draws), c("chain", "sample"))
if (!is.null(show)) {
assert_string(show)
param_names <- intersect(param_names, show)
}
if (!is.null(hide)) {
assert_string(hide)
param_names <- setdiff(param_names, hide)
}
if (length(param_names) == 0) {
stop("no parameters remaining after applying show and hide")
}

# produce plot
param_draws |>
select(all_of(param_names)) |>
pivot_longer(cols = everything()) |>
ggplot(aes(x = value)) + theme_bw() +
geom_density(fill = "blue", alpha = 0.5) +
facet_wrap(~name, scales = "free") +
xlab("Parameter value") + ylab("Probability density")
}
2 changes: 2 additions & 0 deletions R_ignore/deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ mcmc <- run_mcmc(data = list(x = x),
samples = 1e3)

plot_trace(mcmc, show = "mu", phase = "burnin")

plot_density(mcmc)
21 changes: 21 additions & 0 deletions man/plot_density.Rd

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

0 comments on commit f06fd1e

Please sign in to comment.