From bc328d58a502ae724ed89c3e7395ec7ee9b606d1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 16 Dec 2024 15:49:15 +0100 Subject: [PATCH] Multivariate response models Fixes #31 --- R/rope.R | 23 +++++++++++++++++------ man/diagnostic_posterior.Rd | 32 +++++++++++++++++++++++--------- man/equivalence_test.Rd | 10 +++++----- man/p_rope.Rd | 10 +++++----- man/rope.Rd | 21 ++++++++++++++++----- 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/R/rope.R b/R/rope.R index 675bd3581..805761ee1 100644 --- a/R/rope.R +++ b/R/rope.R @@ -16,11 +16,11 @@ #' names. In this case, all parameters that have no matching name in `range` #' will be set to `"default"`. #' -#' In multivariate models, `range` should be a list with a numeric vectors for -#' each response variable. Vector names should correspond to the name of the -#' response variables. If `"default"` and input is a vector, the range is set to -#' `c(-0.1, 0.1)`. If `"default"` and input is a Bayesian model, -#' [`rope_range()`] is used. +#' In multivariate models, `range` should be a list with another list (one for +#' each response variable) of numeric vectors . Vector names should correspond to +#' the name of the response variables. If `"default"` and input is a vector, the +#' range is set to `c(-0.1, 0.1)`. If `"default"` and input is a Bayesian model, +#' [`rope_range()`] is used. See 'Examples'. #' @param ci The Credible Interval (CI) probability, corresponding to the #' proportion of HDI, to use for the percentage in ROPE. #' @param ci_method The type of interval to use to quantify the percentage in @@ -140,6 +140,17 @@ #' rope(model) #' rope(model, ci = c(0.90, 0.95)) #' +#' # different ROPE ranges for model parameters. For each response, a named +#' # list (with the name of the response variable) is required as list-element +#' # for the `range` argument. +#' rope( +#' model, +#' range = list( +#' mpg = list(b_mpg_wt = c(-1, 1), b_mpg_cyl = c(-2, 2)), +#' disp = list(b_disp_wt = c(-5, 5), b_disp_cyl = c(-4, 4)) +#' ) +#' ) +#' #' library(BayesFactor) #' bf <- ttestBF(x = rnorm(100, 1, 1)) #' rope(bf) @@ -453,7 +464,7 @@ rope.brmsfit <- function(x, !all(names(range) %in% insight::find_response(x)) ) { insight::format_error( - "With a multivariate model, `range` should be 'default' or a list of named numeric vectors with length 2." + "With a multivariate model, `range` should be 'default' or a list with multiple lists (one for each response) of named numeric vectors with length 2." ) } } else if (!is.list(range) && (!all(is.numeric(range)) || length(range) != 2)) { diff --git a/man/diagnostic_posterior.Rd b/man/diagnostic_posterior.Rd index a44d74274..c62d7d71b 100644 --- a/man/diagnostic_posterior.Rd +++ b/man/diagnostic_posterior.Rd @@ -38,16 +38,30 @@ diagnostic_posterior(posterior, ...) \item{diagnostic}{Diagnostic metrics to compute. Character (vector) or list with one or more of these options: \code{"ESS"}, \code{"Rhat"}, \code{"MCSE"} or \code{"all"}.} -\item{effects}{Should parameters for fixed effects, random effects -or both be returned? Only applies to mixed models. May be abbreviated.} +\item{effects}{Should variables for fixed effects (\code{"fixed"}), random effects +(\code{"random"}) or both (\code{"all"}) be returned? Only applies to mixed models. May +be abbreviated.} -\item{component}{Should all predictor variables, predictor variables for the -conditional model, the zero-inflated part of the model, the dispersion -term or the instrumental variables be returned? Applies to models -with zero-inflated and/or dispersion formula, or to models with instrumental -variable (so called fixed-effects regressions). May be abbreviated. Note that the -\emph{conditional} component is also called \emph{count} or \emph{mean} -component, depending on the model.} +\item{component}{Which type of parameters to return, such as parameters for +the conditional model, the zero-inflated part of the model, the dispersion +term, the instrumental variables or marginal effects be returned? Applies to +models with zero-inflated and/or dispersion formula, or to models with +instrumental variables (so called fixed-effects regressions), or models with +marginal effects (from \strong{mfx}). See details in section \emph{Model Components} +.May be abbreviated. Note that the \emph{conditional} component also refers to the +\emph{count} or \emph{mean} component - names may differ, depending on the modeling +package. There are three convenient shortcuts (not applicable to \emph{all} model +classes): +\itemize{ +\item \code{component = "all"} returns all possible parameters. +\item If \code{component = "location"}, location parameters such as \code{conditional}, +\code{zero_inflated}, \code{smooth_terms}, or \code{instruments} are returned (everything +that are fixed or random effects - depending on the \code{effects} argument - +but no auxiliary parameters). +\item For \code{component = "distributional"} (or \code{"auxiliary"}), components like +\code{sigma}, \code{dispersion}, \code{beta} or \code{precision} (and other auxiliary +parameters) are returned. +}} \item{parameters}{Regular expression pattern that describes the parameters that should be returned.} diff --git a/man/equivalence_test.Rd b/man/equivalence_test.Rd index 2e2281f66..8355855a5 100644 --- a/man/equivalence_test.Rd +++ b/man/equivalence_test.Rd @@ -62,11 +62,11 @@ names. In this case, all parameters that have no matching name in \code{range} will be set to \code{"default"}. } -In multivariate models, \code{range} should be a list with a numeric vectors for -each response variable. Vector names should correspond to the name of the -response variables. If \code{"default"} and input is a vector, the range is set to -\code{c(-0.1, 0.1)}. If \code{"default"} and input is a Bayesian model, -\code{\link[=rope_range]{rope_range()}} is used.} +In multivariate models, \code{range} should be a list with another list (one for +each response variable) of numeric vectors . Vector names should correspond to +the name of the response variables. If \code{"default"} and input is a vector, the +range is set to \code{c(-0.1, 0.1)}. If \code{"default"} and input is a Bayesian model, +\code{\link[=rope_range]{rope_range()}} is used. See 'Examples'.} \item{ci}{The Credible Interval (CI) probability, corresponding to the proportion of HDI, to use for the percentage in ROPE.} diff --git a/man/p_rope.Rd b/man/p_rope.Rd index 45d296f8e..b8200c748 100644 --- a/man/p_rope.Rd +++ b/man/p_rope.Rd @@ -53,11 +53,11 @@ names. In this case, all parameters that have no matching name in \code{range} will be set to \code{"default"}. } -In multivariate models, \code{range} should be a list with a numeric vectors for -each response variable. Vector names should correspond to the name of the -response variables. If \code{"default"} and input is a vector, the range is set to -\code{c(-0.1, 0.1)}. If \code{"default"} and input is a Bayesian model, -\code{\link[=rope_range]{rope_range()}} is used.} +In multivariate models, \code{range} should be a list with another list (one for +each response variable) of numeric vectors . Vector names should correspond to +the name of the response variables. If \code{"default"} and input is a vector, the +range is set to \code{c(-0.1, 0.1)}. If \code{"default"} and input is a Bayesian model, +\code{\link[=rope_range]{rope_range()}} is used. See 'Examples'.} \item{verbose}{Toggle off warnings.} diff --git a/man/rope.Rd b/man/rope.Rd index 242edb880..38b182f35 100644 --- a/man/rope.Rd +++ b/man/rope.Rd @@ -65,11 +65,11 @@ names. In this case, all parameters that have no matching name in \code{range} will be set to \code{"default"}. } -In multivariate models, \code{range} should be a list with a numeric vectors for -each response variable. Vector names should correspond to the name of the -response variables. If \code{"default"} and input is a vector, the range is set to -\code{c(-0.1, 0.1)}. If \code{"default"} and input is a Bayesian model, -\code{\link[=rope_range]{rope_range()}} is used.} +In multivariate models, \code{range} should be a list with another list (one for +each response variable) of numeric vectors . Vector names should correspond to +the name of the response variables. If \code{"default"} and input is a vector, the +range is set to \code{c(-0.1, 0.1)}. If \code{"default"} and input is a Bayesian model, +\code{\link[=rope_range]{rope_range()}} is used. See 'Examples'.} \item{ci}{The Credible Interval (CI) probability, corresponding to the proportion of HDI, to use for the percentage in ROPE.} @@ -202,6 +202,17 @@ model <- brm( rope(model) rope(model, ci = c(0.90, 0.95)) +# different ROPE ranges for model parameters. For each response, a named +# list (with the name of the response variable) is required as list-element +# for the `range` argument. +rope( + model, + range = list( + mpg = list(b_mpg_wt = c(-1, 1), b_mpg_cyl = c(-2, 2)), + disp = list(b_disp_wt = c(-5, 5), b_disp_cyl = c(-4, 4)) + ) +) + library(BayesFactor) bf <- ttestBF(x = rnorm(100, 1, 1)) rope(bf)