From 7a930de5e22362cd5752b55d395b30cb013b1d38 Mon Sep 17 00:00:00 2001 From: Adam Howes Date: Mon, 16 Sep 2024 17:47:12 +0100 Subject: [PATCH] Issue #304: Update approximate inference vignette and remove `draws_to_long` (#328) * Use tidybayes geom, get rid of draws_to_long, and add figure captions * Remove draws_to_long and replace in FAQ also * Remove draws_to_long * Split problem chunk to see if I can isolate the issue * Try comment out this section * Try reverting hotfix * Readd hotfix --- NAMESPACE | 1 - R/globals.R | 11 ++++++++ R/postprocess.R | 14 ---------- man/add_mean_sd.Rd | 1 - man/add_mean_sd.default.Rd | 1 - man/add_mean_sd.gamma_samples.Rd | 1 - man/add_mean_sd.lognormal_samples.Rd | 1 - man/draws_to_long.Rd | 23 --------------- man/predict_delay_parameters.Rd | 3 +- vignettes/approx-inference.Rmd | 42 +++++++++++++--------------- vignettes/faq.Rmd | 8 +++++- 11 files changed, 39 insertions(+), 67 deletions(-) delete mode 100644 man/draws_to_long.Rd diff --git a/NAMESPACE b/NAMESPACE index 86a8b6773..008ba33c8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,7 +18,6 @@ S3method(epidist_validate,default) S3method(epidist_validate,epidist_latent_individual) export(add_mean_sd) export(as_latent_individual) -export(draws_to_long) export(epidist) export(epidist_diagnostics) export(epidist_family) diff --git a/R/globals.R b/R/globals.R index d7a6f93b9..de1d8fd88 100644 --- a/R/globals.R +++ b/R/globals.R @@ -1,11 +1,14 @@ # Generated by roxyglobals: do not edit by hand utils::globalVariables(c( + ":=", # "no_at_max_treedepth", # "max_treedepth", # "per_at_max_treedepth", # "samples", # + ":=", # "id", # + ".N", # "obs_t", # "obs_at", # "ptime_lwr", # @@ -19,6 +22,7 @@ utils::globalVariables(c( "row_id", # "woverlap", # "row_id", # + ":=", # "ptime_daily", # "ptime", # "ptime_lwr", # @@ -31,25 +35,32 @@ utils::globalVariables(c( "delay_lwr", # "delay_upr", # "obs_at", # + ":=", # "obs_at", # "ptime", # "censored_obs_time", # "ptime_lwr", # "censored", # "stime_upr", # + ":=", # "censored", # "ptime_upr", # "stime_upr", # "ptime", # "censored_obs_time", # "ptime_lwr", # + ":=", # "mu", # "sigma", # "sd", # + ":=", # "mu", # "sd", # "shape", # + "rlnorm", # + ":=", # "delay", # + ".N", # "stime", # "ptime", # "prior_old", # <.replace_prior> diff --git a/R/postprocess.R b/R/postprocess.R index 63bb5f383..21eb2beaf 100644 --- a/R/postprocess.R +++ b/R/postprocess.R @@ -34,20 +34,6 @@ predict_delay_parameters <- function(fit, newdata = NULL, ...) { #' @export predict_dpar <- predict_delay_parameters -#' Convert posterior lognormal samples to long format -#' -#' @param draws ... -#' @family postprocess -#' @export -draws_to_long <- function(draws) { - long_draws <- data.table::melt( - draws, - measure.vars = c("mu", "sigma", "mean", "sd"), - variable.name = "parameter" - ) - return(long_draws[]) -} - #' Add natural scale mean and standard deviation parameters #' #' @param data A dataframe of distributional parameters diff --git a/man/add_mean_sd.Rd b/man/add_mean_sd.Rd index 43a23aa74..ec86b48cb 100644 --- a/man/add_mean_sd.Rd +++ b/man/add_mean_sd.Rd @@ -19,7 +19,6 @@ Other postprocess: \code{\link{add_mean_sd.default}()}, \code{\link{add_mean_sd.gamma_samples}()}, \code{\link{add_mean_sd.lognormal_samples}()}, -\code{\link{draws_to_long}()}, \code{\link{predict_delay_parameters}()} } \concept{postprocess} diff --git a/man/add_mean_sd.default.Rd b/man/add_mean_sd.default.Rd index b51b9d0fb..8a9e9fbdc 100644 --- a/man/add_mean_sd.default.Rd +++ b/man/add_mean_sd.default.Rd @@ -19,7 +19,6 @@ Other postprocess: \code{\link{add_mean_sd}()}, \code{\link{add_mean_sd.gamma_samples}()}, \code{\link{add_mean_sd.lognormal_samples}()}, -\code{\link{draws_to_long}()}, \code{\link{predict_delay_parameters}()} } \concept{postprocess} diff --git a/man/add_mean_sd.gamma_samples.Rd b/man/add_mean_sd.gamma_samples.Rd index a364761eb..dc55d0133 100644 --- a/man/add_mean_sd.gamma_samples.Rd +++ b/man/add_mean_sd.gamma_samples.Rd @@ -20,7 +20,6 @@ Other postprocess: \code{\link{add_mean_sd}()}, \code{\link{add_mean_sd.default}()}, \code{\link{add_mean_sd.lognormal_samples}()}, -\code{\link{draws_to_long}()}, \code{\link{predict_delay_parameters}()} } \concept{postprocess} diff --git a/man/add_mean_sd.lognormal_samples.Rd b/man/add_mean_sd.lognormal_samples.Rd index 2eb59cebf..23cde7039 100644 --- a/man/add_mean_sd.lognormal_samples.Rd +++ b/man/add_mean_sd.lognormal_samples.Rd @@ -21,7 +21,6 @@ Other postprocess: \code{\link{add_mean_sd}()}, \code{\link{add_mean_sd.default}()}, \code{\link{add_mean_sd.gamma_samples}()}, -\code{\link{draws_to_long}()}, \code{\link{predict_delay_parameters}()} } \concept{postprocess} diff --git a/man/draws_to_long.Rd b/man/draws_to_long.Rd deleted file mode 100644 index f729dd07f..000000000 --- a/man/draws_to_long.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/postprocess.R -\name{draws_to_long} -\alias{draws_to_long} -\title{Convert posterior lognormal samples to long format} -\usage{ -draws_to_long(draws) -} -\arguments{ -\item{draws}{...} -} -\description{ -Convert posterior lognormal samples to long format -} -\seealso{ -Other postprocess: -\code{\link{add_mean_sd}()}, -\code{\link{add_mean_sd.default}()}, -\code{\link{add_mean_sd.gamma_samples}()}, -\code{\link{add_mean_sd.lognormal_samples}()}, -\code{\link{predict_delay_parameters}()} -} -\concept{postprocess} diff --git a/man/predict_delay_parameters.Rd b/man/predict_delay_parameters.Rd index cd1f51f07..a10af5740 100644 --- a/man/predict_delay_parameters.Rd +++ b/man/predict_delay_parameters.Rd @@ -28,7 +28,6 @@ Other postprocess: \code{\link{add_mean_sd}()}, \code{\link{add_mean_sd.default}()}, \code{\link{add_mean_sd.gamma_samples}()}, -\code{\link{add_mean_sd.lognormal_samples}()}, -\code{\link{draws_to_long}()} +\code{\link{add_mean_sd.lognormal_samples}()} } \concept{postprocess} diff --git a/vignettes/approx-inference.Rmd b/vignettes/approx-inference.Rmd index a2b50d07a..9700e4b8e 100644 --- a/vignettes/approx-inference.Rmd +++ b/vignettes/approx-inference.Rmd @@ -98,6 +98,7 @@ library(gt) library(purrr) library(tidyr) library(tibble) +library(tidybayes) ``` First, we begin by simulating data. @@ -171,7 +172,12 @@ fits <- list( draws <- imap(fits, function(fit, name) { predict_delay_parameters(fit) |> - draws_to_long() |> + as.data.frame() |> + pivot_longer( + cols = c("mu", "sigma", "mean", "sd"), + names_to = "parameter", + values_to = "value" + ) |> filter(parameter %in% c("mu", "sigma")) |> mutate(method = as.factor(name)) }) @@ -194,37 +200,29 @@ pars |> gt() ``` -More comprehensively, the estimated posterior distributions are as follows. +More comprehensively, the estimated posterior distributions are shown in Figure \@ref(fig:posterior). -```{r} -draws |> - filter(parameter == "mu") |> - ggplot(aes(x = value, fill = method)) + - geom_histogram(aes(y = ..density..)) + - scale_fill_manual(values = c("#56B4E9", "#009E73", "#E69F00", "#CC79A7")) + - facet_grid(method ~ parameter) + - theme_minimal() + - guides(fill = "none") + - labs(x = "", y = "") -``` +(ref:posterior) Estimated posterior distributions for the `mu` and `sigma` parameters using each inference method, shown using `tidybayes::stat_slabinterval()`. -```{r} +```{r posterior, fig.cap="(ref:posterior)"} draws |> - filter(parameter == "sigma") |> - ggplot(aes(x = value, fill = method)) + - geom_histogram(aes(y = ..density..)) + - scale_fill_manual(values = c("#56B4E9", "#009E73", "#E69F00", "#CC79A7")) + - facet_grid(method ~ parameter) + + ggplot(aes(x = value, col = method)) + + stat_slabinterval(density = "histogram", breaks = 30, alpha = 0.8) + + scale_colour_manual(values = c("#56B4E9", "#009E73", "#E69F00", "#CC79A7")) + + facet_grid(method ~ parameter, scales = "free_x") + theme_minimal() + guides(fill = "none") + - labs(x = "", y = "") + labs(x = "", y = "", col = "Method") + + theme(legend.position = "bottom") ``` ## Comparison of resulting delay distributions -How do these different distributions on the `mu` and `sigma` parameters effect the estimated delay distribution? +Figure \@ref(fig:delay-pdf) shows how the different `mu` and `sigma` posterior mean estimates from each inference method alter an estimated delay distribution. -```{r} +(ref:delay-pdf) Delay probability density functions obtained based on the posterior mean estimated `mu` and `sigma` parameters. + +```{r delay-pdf, fig.cap="(ref:delay-pdf)"} pmap_df( filter(pars), ~ tibble( x = seq(0, 25, by = 0.1), diff --git a/vignettes/faq.Rmd b/vignettes/faq.Rmd index 57de62792..3ca8185fb 100644 --- a/vignettes/faq.Rmd +++ b/vignettes/faq.Rmd @@ -28,6 +28,7 @@ library(brms) library(dplyr) library(ggplot2) library(scales) +library(tidyr) set.seed(1) @@ -135,7 +136,12 @@ fit_ppc <- epidist( pred <- predict_delay_parameters(fit_ppc) pred |> - draws_to_long() |> + as.data.frame() |> + pivot_longer( + cols = c("mu", "sigma", "mean", "sd"), + names_to = "parameter", + values_to = "value" + ) |> filter(parameter %in% c("mean", "sd")) |> ggplot(aes(x = value, y = after_stat(density))) + geom_histogram() +