diff --git a/NEWS.md b/NEWS.md index b4f35a209..46f2c3cbc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ ## New features +* Updating density diagnostic function (#71) * Changes to `run_mod()` (#79): - `jags.post` now optionally included in output, as specified by argument `with_post` diff --git a/R/plot_jags_densitydx.R b/R/plot_jags_densitydx.R index 69e04cd3c..f4e532b2d 100644 --- a/R/plot_jags_densitydx.R +++ b/R/plot_jags_densitydx.R @@ -26,7 +26,7 @@ #' - `y1` = posterior estimate of peak antibody concentration #' @param strat Specify [character] string to produce plots of specific #' stratification entered in quotes. -#' @return A [base::list()] of [ggplot2::ggplot()] objects producing density +#' @return A [list] of [ggplot2::ggplot] objects producing density #' plots for all the specified input. #' @export #' @example inst/examples/examples-plot_jags_densitydx.R @@ -61,18 +61,23 @@ plot_jags_dens <- function(data, # Changing parameter name to reflect the input dplyr::mutate(Parameter = paste0("iso = ", j, ", parameter = ", .data$Parameter_sub, ", strat = ", - i), - value = log(.data$value)) + i)) # Assigning attributes, which are needed to run ggs_density attributes(visualize_jags_plot) <- c(attributes(visualize_jags_plot), attributes_jags) # Creating density plot densplot <- ggmcmc::ggs_density(visualize_jags_plot) + ggplot2::theme_bw() + - ggplot2::labs(x = "log(value)") + ggplot2::labs(x = "parameter value") + + ggplot2::scale_x_log10(labels = scales::label_comma()) density_out[[j]] <- densplot } + dens_strat_list[[i]] <- density_out } + #Printing only one plot if only one exists. + if (length(dens_strat_list) == 1) { + dens_strat_list <- dens_strat_list[[1]][[iso]] + } dens_strat_list } diff --git a/inst/examples/examples-plot_jags_densitydx.R b/inst/examples/examples-plot_jags_densitydx.R index a70526d92..269abd2d6 100644 --- a/inst/examples/examples-plot_jags_densitydx.R +++ b/inst/examples/examples-plot_jags_densitydx.R @@ -1,7 +1,7 @@ + data <- serodynamics::nepal_sees_jags_output -# Specifying isotype and stratification for traceplot. plot_jags_dens( data = data, iso = "HlyE_IgA", diff --git a/man/plot_jags_dens.Rd b/man/plot_jags_dens.Rd index 63b3e5326..78b6145c4 100644 --- a/man/plot_jags_dens.Rd +++ b/man/plot_jags_dens.Rd @@ -32,7 +32,7 @@ specific parameter, entered with quotes. Options include: stratification entered in quotes.} } \value{ -A \code{\link[base:list]{base::list()}} of \code{\link[ggplot2:ggplot]{ggplot2::ggplot()}} objects producing density +A \link{list} of \link[ggplot2:ggplot]{ggplot2::ggplot} objects producing density plots for all the specified input. } \description{ @@ -52,9 +52,9 @@ The antibody dynamic curve includes the following parameters: } \examples{ + data <- serodynamics::nepal_sees_jags_output -# Specifying isotype and stratification for traceplot. plot_jags_dens( data = data, iso = "HlyE_IgA", diff --git a/tests/testthat/_snaps/plot_jags_densitydx/typhoid-plot.svg b/tests/testthat/_snaps/plot_jags_densitydx/typhoid-plot.svg index 475655a9a..722886cf5 100644 --- a/tests/testthat/_snaps/plot_jags_densitydx/typhoid-plot.svg +++ b/tests/testthat/_snaps/plot_jags_densitydx/typhoid-plot.svg @@ -27,22 +27,21 @@ - - - - - - - - + + + + + + + - - - - - - - + + + + + + + @@ -58,20 +57,19 @@ - - - - - - - + + + + + + - - - - - - + + + + + + @@ -87,23 +85,25 @@ - - - - - - - - + + + + + + + + + - - - + + + + + - - - - + + + @@ -119,21 +119,21 @@ - - - - - - - + + + + + + + - - - - + + + + + - - + @@ -149,26 +149,23 @@ - - - - - - - - - - + + + + + + + + + - - - - - - - - - + + + + + + + @@ -231,91 +228,89 @@ iso = HlyE_IgA, parameter = alpha, strat = typhi - - - - - -2.5 -5.0 -7.5 -10.0 -12.5 - + + + + + +10 +100 +1,000 +10,000 +100,000 + - - --2 -0 -2 -4 + +0.1 +1.0 +10.0 - - - - -0 -1 -2 -3 -4 - - - - -0.4 -0.8 -1.2 -1.6 - - - - --8 --6 --4 --2 -0.0 -0.1 -0.2 -0.3 + + + +1 +3 +10 +30 + + + +2 +3 +5 + + + + +0.0001 +0.0010 +0.0100 +0.1000 +0.00 +0.25 +0.50 +0.75 - - - + + + 0 -1 -2 +2 +4 +6 - - + + + 0.0 -0.3 -0.6 -0.9 +0.5 +1.0 +1.5 +2.0 +2.5 - - - -0.00 -0.25 -0.50 -0.75 + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 - - - -0.00 -0.05 -0.10 -0.15 -0.20 + + + + +0.0 +0.2 +0.4 - - - - -log(value) + + +parameter value density Chain diff --git a/tests/testthat/test-plot_jags_densitydx.R b/tests/testthat/test-plot_jags_densitydx.R index a5e889f28..04c710ef4 100644 --- a/tests/testthat/test-plot_jags_densitydx.R +++ b/tests/testthat/test-plot_jags_densitydx.R @@ -3,11 +3,8 @@ test_that( desc = "results are consistent with ggplot output", code = { skip_if(getRversion() < "4.4.1") # 4.3.3 had issues - library(runjags) - library(dplyr) - data <- serodynamics::nepal_sees_jags_output |> - suppressWarnings() + data <- serodynamics::nepal_sees_jags_output # Testing for any errors: results <- plot_jags_dens(data) |> expect_no_error() diff --git a/tests/testthat/test-run_mod.R b/tests/testthat/test-run_mod.R index e40bbdc1b..555d870bc 100644 --- a/tests/testthat/test-run_mod.R +++ b/tests/testthat/test-run_mod.R @@ -12,7 +12,7 @@ test_that( sim_case_data(n = 100, antigen_isos = "HlyE_IgA") |> mutate(strat = "stratum 1") - dataset <- bind_rows(strat1, strat2) + dataset <- dplyr::bind_rows(strat1, strat2) withr::with_seed( 1, code = {