diff --git a/NAMESPACE b/NAMESPACE index da305ab6c..12f70286a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,8 @@ # Generated by roxygen2: do not edit by hand +S3method(add_natural_scale_mean_sd,default) +S3method(add_natural_scale_mean_sd,latent_gamma) +S3method(add_natural_scale_mean_sd,latent_lognormal) S3method(as_latent_individual,data.frame) S3method(epidist,default) S3method(epidist_family,default) diff --git a/R/globals.R b/R/globals.R index f961f4e21..e9c8256d3 100644 --- a/R/globals.R +++ b/R/globals.R @@ -5,9 +5,6 @@ utils::globalVariables(c( "max_treedepth", # "per_at_max_treedepth", # "samples", # - "meanlog", # - "sdlog", # - "sd", # "sdlog", # "sdlog_log", # "meanlog", # @@ -72,6 +69,13 @@ utils::globalVariables(c( "ptime_daily", # "n", # "obs_horizon", # + "mu", # + "sigma", # + "sd", # + "sigma", # + "sd", # + "mu", # + "shape", # "time", # "primary", # "secondary", # diff --git a/R/postprocess.R b/R/postprocess.R index 58df198f0..700506115 100644 --- a/R/postprocess.R +++ b/R/postprocess.R @@ -13,6 +13,7 @@ add_natural_scale_mean_sd <- function(data, ...) { #' @inheritParams add_natural_scale_mean_sd #' @param ... Additional arguments for method. #' @family postprocess +#' @method add_natural_scale_mean_sd default #' @export add_natural_scale_mean_sd.default <- function(data, ...) { cli::cli_inform( @@ -31,6 +32,7 @@ add_natural_scale_mean_sd.default <- function(data, ...) { #' @inheritParams add_natural_scale_mean_sd #' @param ... Additional arguments for method. #' @family postprocess +#' @method add_natural_scale_mean_sd latent_lognormal #' @autoglobal #' @export add_natural_scale_mean_sd.latent_lognormal <- function(data, ...) { @@ -47,6 +49,7 @@ add_natural_scale_mean_sd.latent_lognormal <- function(data, ...) { #' @inheritParams add_natural_scale_mean_sd #' @param ... Additional arguments for method. #' @family postprocess +#' @method add_natural_scale_mean_sd latent_gamma #' @autoglobal #' @export add_natural_scale_mean_sd.latent_gamma <- function(data, ...) { diff --git a/man/add_natural_scale_mean_sd.Rd b/man/add_natural_scale_mean_sd.Rd index d0754226d..5983c4bdf 100644 --- a/man/add_natural_scale_mean_sd.Rd +++ b/man/add_natural_scale_mean_sd.Rd @@ -1,19 +1,24 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/fitting-and-postprocessing.R +% Please edit documentation in R/postprocess.R \name{add_natural_scale_mean_sd} \alias{add_natural_scale_mean_sd} -\title{Add natural scale summary parameters for a lognormal distribution} +\title{Add natural scale parameters} \usage{ -add_natural_scale_mean_sd(dt) +add_natural_scale_mean_sd(data, ...) } \arguments{ -\item{dt}{...} +\item{data}{A dataframe of distributional parameters} + +\item{...}{Additional arguments for method.} } \description{ -Add natural scale summary parameters for a lognormal distribution +Add natural scale parameters } \seealso{ Other postprocess: +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, \code{\link{draws_to_long}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, diff --git a/man/add_natural_scale_mean_sd.default.Rd b/man/add_natural_scale_mean_sd.default.Rd new file mode 100644 index 000000000..1e64690bd --- /dev/null +++ b/man/add_natural_scale_mean_sd.default.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/postprocess.R +\name{add_natural_scale_mean_sd.default} +\alias{add_natural_scale_mean_sd.default} +\title{Default method for add natural scale parameters} +\usage{ +\method{add_natural_scale_mean_sd}{default}(data, ...) +} +\arguments{ +\item{data}{A dataframe of distributional parameters} + +\item{...}{Additional arguments for method.} +} +\description{ +Default method for add natural scale parameters +} +\seealso{ +Other postprocess: +\code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, +\code{\link{draws_to_long}()}, +\code{\link{extract_lognormal_draws}()}, +\code{\link{make_relative_to_truth}()}, +\code{\link{summarise_draws}()}, +\code{\link{summarise_variable}()} +} +\concept{postprocess} diff --git a/man/add_natural_scale_mean_sd.latent_gamma.Rd b/man/add_natural_scale_mean_sd.latent_gamma.Rd new file mode 100644 index 000000000..fee000953 --- /dev/null +++ b/man/add_natural_scale_mean_sd.latent_gamma.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/postprocess.R +\name{add_natural_scale_mean_sd.latent_gamma} +\alias{add_natural_scale_mean_sd.latent_gamma} +\title{Add natural scale parameters for a latent gamma model} +\usage{ +\method{add_natural_scale_mean_sd}{latent_gamma}(data, ...) +} +\arguments{ +\item{data}{A dataframe of distributional parameters} + +\item{...}{Additional arguments for method.} +} +\description{ +Again, \code{mu} and \code{shape} here are the distributional parameters of \code{brms}. +} +\seealso{ +Other postprocess: +\code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, +\code{\link{draws_to_long}()}, +\code{\link{extract_lognormal_draws}()}, +\code{\link{make_relative_to_truth}()}, +\code{\link{summarise_draws}()}, +\code{\link{summarise_variable}()} +} +\concept{postprocess} diff --git a/man/add_natural_scale_mean_sd.latent_lognormal.Rd b/man/add_natural_scale_mean_sd.latent_lognormal.Rd new file mode 100644 index 000000000..ab9d2c247 --- /dev/null +++ b/man/add_natural_scale_mean_sd.latent_lognormal.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/postprocess.R +\name{add_natural_scale_mean_sd.latent_lognormal} +\alias{add_natural_scale_mean_sd.latent_lognormal} +\title{Add natural scale parameters for a latent lognormal model} +\usage{ +\method{add_natural_scale_mean_sd}{latent_lognormal}(data, ...) +} +\arguments{ +\item{data}{A dataframe of distributional parameters} + +\item{...}{Additional arguments for method.} +} +\description{ +Note that the input parameters here are \code{mu} and \code{sigma}, corresponding to +the distributional parameters used by \code{brms} for the \code{lognormal} family. +} +\seealso{ +Other postprocess: +\code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{draws_to_long}()}, +\code{\link{extract_lognormal_draws}()}, +\code{\link{make_relative_to_truth}()}, +\code{\link{summarise_draws}()}, +\code{\link{summarise_variable}()} +} +\concept{postprocess} diff --git a/man/draws_to_long.Rd b/man/draws_to_long.Rd index f4b346ef9..61653792d 100644 --- a/man/draws_to_long.Rd +++ b/man/draws_to_long.Rd @@ -15,6 +15,9 @@ Convert posterior lognormal samples to long format \seealso{ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, \code{\link{summarise_draws}()}, diff --git a/man/extract_lognormal_draws.Rd b/man/extract_lognormal_draws.Rd index d7270f874..bfa0fc37a 100644 --- a/man/extract_lognormal_draws.Rd +++ b/man/extract_lognormal_draws.Rd @@ -19,6 +19,9 @@ Extract posterior samples for a lognormal brms model \seealso{ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, \code{\link{draws_to_long}()}, \code{\link{make_relative_to_truth}()}, \code{\link{summarise_draws}()}, diff --git a/man/make_relative_to_truth.Rd b/man/make_relative_to_truth.Rd index 97cd56564..e31ca744c 100644 --- a/man/make_relative_to_truth.Rd +++ b/man/make_relative_to_truth.Rd @@ -19,6 +19,9 @@ Make posterior lognormal samples relative to true values \seealso{ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, \code{\link{draws_to_long}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{summarise_draws}()}, diff --git a/man/summarise_draws.Rd b/man/summarise_draws.Rd index d8d0b29a9..3ec4afb15 100644 --- a/man/summarise_draws.Rd +++ b/man/summarise_draws.Rd @@ -21,6 +21,9 @@ Summarise posterior draws \seealso{ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, \code{\link{draws_to_long}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, diff --git a/man/summarise_variable.Rd b/man/summarise_variable.Rd index c7a2dc30f..245c8a867 100644 --- a/man/summarise_variable.Rd +++ b/man/summarise_variable.Rd @@ -21,6 +21,9 @@ Summarise a variable \seealso{ Other postprocess: \code{\link{add_natural_scale_mean_sd}()}, +\code{\link{add_natural_scale_mean_sd.default}()}, +\code{\link{add_natural_scale_mean_sd.latent_gamma}()}, +\code{\link{add_natural_scale_mean_sd.latent_lognormal}()}, \code{\link{draws_to_long}()}, \code{\link{extract_lognormal_draws}()}, \code{\link{make_relative_to_truth}()}, diff --git a/tests/testthat/test-unit-postprocess.R b/tests/testthat/test-unit-postprocess.R index a01a67532..0ed2d481b 100644 --- a/tests/testthat/test-unit-postprocess.R +++ b/tests/testthat/test-unit-postprocess.R @@ -16,10 +16,12 @@ brms::posterior_predict(fit) linpred_mu <- brms::posterior_linpred(fit, transform = TRUE, dpar = "mu") linpred_sigma <- brms::posterior_linpred(fit, transform = TRUE, dpar = "sigma") -linpred_mu_melt <- reshape2::melt(linpred_mu, varnames = c("aaa", "bbb"), value.name = "mu") -linpred_sigma_melt <- reshape2::melt(linpred_sigma, varnames = c("aaa", "bbb"), value.name = "sigma") +linpred_mu_melt <- reshape2::melt(linpred_mu, varnames = c("draw", "index"), value.name = "mu") +linpred_sigma_melt <- reshape2::melt(linpred_sigma, varnames = c("draw", "index"), value.name = "sigma") linpred_melt <- dplyr::left_join(linpred_mu_melt, linpred_sigma_melt) +class(linpred_melt) <- c(class(linpred_melt), "latent_lognormal") +add_natural_scale_mean_sd(data.table::as.data.table(linpred_melt)) pp <- brms::prepare_predictions(fit) pp$dpars$mu$fe$b