Package {buzzMed}


Version: 0.1.2
Date: 2026-05-20
Title: Bayesian Understanding for Mediator Selection Framework
Description: A collection of quantitative tools for selecting mediating effects within exploratory Bayesian mediation models. The package accommodates both continuous and dichotomous outcomes, including the dependent variables and the mediators for identifying and analyzing mediation pathways.
License: GPL (≥ 3)
Depends: R (≥ 4.0.0)
SystemRequirements: JAGS (>= 4.0.0)
Imports: coda, methods, rjags, stats, utils
Suggests: knitr, rmarkdown, spelling, testthat (≥ 3.0.0)
ByteCompile: true
VignetteBuilder: knitr
URL: https://github.com/olfactorybulb/buzzMed
BugReports: https://github.com/olfactorybulb/buzzMed/issues
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.3.3
NeedsCompilation: no
Packaged: 2026-05-21 01:46:21 UTC; jesshsing
Author: Dingjing Shi [aut, cre], Tansu Celikel [aut], Dexin Shi [aut], Chih-Chia Hsing [ctb]
Maintainer: Dingjing Shi <dshi32@gatech.edu>
Repository: CRAN
Date/Publication: 2026-05-28 11:20:06 UTC

Internal Prior Table Printer

Description

Formats and displays the current parameter specifications in a clean, readable table within the R console.

Usage

.print_parms_table(parms)

Arguments

parms

A data.frame containing current prior specifications, typically generated by .make_default_parms().

Value

No return value; called for side effects (printing to console).


Build JAGS Model String (Binary M & Binary Y)

Description

This internal function generates a JAGS model string using logistic regression (logit link) for both the mediator and outcome equations. It incorporates spike-and-slab variable selection on all mediation pathways.

Usage

build_ebmed_model_mcat_ycat(P, K, parms)

Arguments

P

Integer. The number of predictors/covariates.

K

Integer. The number of mediators.

parms

A data.frame generated by make_parms_main containing columns: priors, distribution, arguments, and template.

Details

Constructs the JAGS model specification for a Bayesian mediation model where both mediators and the outcome are binary (0/1).

The model structure for dual-binary data:

Value

A character string containing the full JAGS model specification.

See Also

buzzEBMcatMcatY, define_init_values


Build JAGS Model String (Binary M & Continuous Y)

Description

This internal function generates a JAGS model string using logistic regression (logit link) for the binary mediators and a Gaussian likelihood for the continuous outcome. It incorporates spike-and-slab variable selection on all mediation pathways.

Usage

build_ebmed_model_mcat_ycont(P, K, parms)

Arguments

P

Integer. The number of predictors/covariates.

K

Integer. The number of mediators.

parms

A data.frame generated by make_parms_main containing columns: priors, distribution, arguments, and template.

Details

Constructs the JAGS model specification for a Bayesian mediation model where mediators are binary (0/1) and the outcome is continuous.

The model structure for binary mediators:

Value

A character string containing the full JAGS model specification.

See Also

buzzEBMcatMcontY, define_init_values


Build JAGS Model String (Continuous M & Binary Y)

Description

This internal function generates a JAGS model string that uses Gaussian likelihoods for the mediators and a logistic regression (logit link) for the binary outcome. It incorporates spike-and-slab variable selection on all mediation pathways.

Usage

build_ebmed_model_mcont_ycat(P, K, parms)

Arguments

P

Integer. The number of predictors/covariates.

K

Integer. The number of mediators.

parms

A data.frame generated by make_parms_main containing columns: priors, distribution, arguments, and template.

Details

Constructs the JAGS model specification for a Bayesian mediation model where mediators are continuous and the outcome is binary (0/1).

The model uses a mixed-likelihood approach:

Value

A character string containing the full JAGS model specification.

See Also

buzzEBMcontMcatY, define_init_values


Build JAGS Model String (Continuous M & Continuous Y)

Description

This internal function dynamically generates a JAGS model string based on the number of predictors (P), mediators (K), and the prior distributions defined in the parms object. It handles the multivariate a-paths and b-paths using inprod (inner product) for efficiency in JAGS.

Usage

build_ebmed_model_mcont_ycont(P, K, parms)

Arguments

P

Integer. The number of predictors/covariates.

K

Integer. The number of mediators.

parms

A data.frame generated by make_parms_main containing columns: priors, distribution, arguments, and template.

Details

Constructs the JAGS model specification for a Bayesian mediation model where both mediators and outcome are continuous, incorporating spike-and-slab variable selection.

The model structure follows a standard mediation framework:

Joint inclusion for a specific mediator pathway is calculated internally as ind.joint <- a.pip * b.pip.

Value

A character string containing the full JAGS model specification.

See Also

prepare_ebmed_data, define_init_values, buzzEBMcontMcontY


Fit Bayesian Mediation Model (Binary M & Binary Y)

Description

This function implements variable selection for mediators when dealing with binary data. It utilizes a latent probit link formulation to model the probabilities of the binary responses within the JAGS framework. It automates data preparation, JAGS model construction, and MCMC sampling.

Usage

buzzEBMcatMcatY(
  model,
  dataset,
  my_prior = NULL,
  advanced = NULL,
  a.coef.mean = NULL,
  a.coef.prec = NULL,
  b.coef.mean = NULL,
  b.coef.prec = NULL,
  a.pip.hyperalpha = NULL,
  a.pip.hyperbeta = NULL,
  b.pip.hyperalpha = NULL,
  b.pip.hyperbeta = NULL,
  direct.coef.mean = NULL,
  direct.coef.precision = NULL,
  direct.coef.init = NULL,
  a.pip.hyperprior.init = NULL,
  b.pip.hyperprior.init = NULL,
  n_chains = NULL,
  n_adapt = NULL,
  n_burnin = NULL,
  n_iter = NULL,
  thin = NULL
)

Arguments

model

A description of the model to be fitted. This is typically a formula or a character string using lavaan syntax (e.g., Y ~ M + X).

dataset

A data.frame containing the variables specified in the model.

my_prior

Optional data.frame containing custom prior specifications. Run parms <- run_parms_wizard() to see the required structure.

advanced

Character. Use "interactive" for an interactive wizard to choose parameter distributions, or leave NULL for defaults.

a.coef.mean, a.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the a path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

b.coef.mean, b.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the b path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

a.pip.hyperalpha, a.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the a path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

b.pip.hyperalpha, b.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the b path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

direct.coef.mean, direct.coef.precision

Numeric scalar or vector. Mean and precision parameters for the Normal prior of the direct effects (c'). By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

direct.coef.init

Numeric or NULL. Initial value for the direct effect (c'). Default is 0.

a.pip.hyperprior.init, b.pip.hyperprior.init

Numeric or NULL. Initial inclusion probabilities (PIP). Default is 0.5.

n_chains

Integer. Number of MCMC chains.

n_adapt

Integer. Number of adaptation iterations.

n_burnin

Integer. Number of burn-in iterations.

n_iter

Integer. Number of post-burn-in iterations.

thin

Integer. Thinning interval.

Details

Fits a Bayesian mediation model specifically designed for cases where both the mediators (M) and the outcome variable (Y) are binary (0/1).

Since both M and Y are binary, residual precisions are not estimated (they are fixed to 1 in the latent probit space). This function identifies X, M, Y via .parse_buzz_syntax and routes the data through an internal pipeline including prepare_ebmed_data and build_ebmed_model_mcat_ycat.

Value

An object of class mcmc.list containing posterior samples.

Note

This function is strictly for binary data. For variables with more than two levels, please convert them into dummy variables or ensure they are binary before running.

References

Shi, D., Shi, D., and Fairchild, A. J. (2023) "Variable Selection for Mediators under a Bayesian Mediation Model" <doi:10.1080/10705511.2022.2164285>

Examples

# Binary case: Both M and Y are Binary
set.seed(101)
n <- 100
toy_data <- data.frame(
   X = rbinom(n, 1, 0.5),
   M1 = rbinom(n, 1, 0.3),
   M2 = rbinom(n, 1, 0.7),
   Y = rbinom(n, 1, 0.5)
)

# Fit the model
results <- buzzEBMcatMcatY(
   model    = "Y ~ M1 + M2 | X",
   dataset  = toy_data,
   n_burnin = 200,
   n_iter   = 1000
)

summary(results)


Fit Bayesian Mediation Model (Binary M & Continuous Y)

Description

This function implements variable selection for mediators in a mixed data framework. It utilizes latent variable formulations for the binary mediators and a Gaussian likelihood for the continuous outcome. It automates data preparation, JAGS model construction, and MCMC sampling.

Usage

buzzEBMcatMcontY(
  model,
  dataset,
  my_prior = NULL,
  advanced = NULL,
  y.prec.shape = NULL,
  y.prec.rate = NULL,
  a.coef.mean = NULL,
  a.coef.prec = NULL,
  b.coef.mean = NULL,
  b.coef.prec = NULL,
  a.pip.hyperalpha = NULL,
  a.pip.hyperbeta = NULL,
  b.pip.hyperalpha = NULL,
  b.pip.hyperbeta = NULL,
  direct.coef.mean = NULL,
  direct.coef.precision = NULL,
  y.prec.init = NULL,
  direct.coef.init = NULL,
  a.pip.hyperprior.init = NULL,
  b.pip.hyperprior.init = NULL,
  n_chains = NULL,
  n_adapt = NULL,
  n_burnin = NULL,
  n_iter = NULL,
  thin = NULL
)

Arguments

model

A description of the model to be fitted. This is typically a formula or a character string using lavaan syntax (e.g., Y ~ M + X).

dataset

A data.frame containing the variables specified in the model.

my_prior

Optional data.frame containing custom prior specifications. Run parms <- run_parms_wizard() to see the required structure.

advanced

Character. Use "interactive" for an interactive wizard to choose parameter distributions, or leave NULL for defaults.

y.prec.shape, y.prec.rate

Numeric scalar. Shape and rate parameters of the Gamma hyperprior for the outcome residual precision. By default, a Gamma distribution is used. The default values are 1 and 0.001, respectively.

a.coef.mean, a.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the a path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

b.coef.mean, b.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the b path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

a.pip.hyperalpha, a.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the a path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

b.pip.hyperalpha, b.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the b path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

direct.coef.mean, direct.coef.precision

Numeric scalar or vector. Mean and precision parameters for the Normal prior of the direct effects (c'). By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

y.prec.init

Numeric or NULL. Initial values for residual precisions. Default is 1.

direct.coef.init

Numeric or NULL. Initial value for the direct effect (c'). Default is 0.

a.pip.hyperprior.init, b.pip.hyperprior.init

Numeric or NULL. Initial inclusion probabilities (PIP). Default is 0.5.

n_chains

Integer. Number of MCMC chains.

n_adapt

Integer. Number of adaptation iterations.

n_burnin

Integer. Number of burn-in iterations.

n_iter

Integer. Number of post-burn-in iterations.

thin

Integer. Thinning interval.

Details

Fits a Bayesian mediation model specifically designed for cases where the mediators (M) are binary (0/1) and the outcome variable (Y) is continuous.

This function is a specific "worker" function. It identifies X, M, Y via .parse_buzz_syntax. While Y is modeled with residual precision parameters, the mediators M are binary and thus their residual precisions are fixed to 1 within the latent variable framework.

Value

An object of class mcmc.list containing posterior samples.

References

Shi, D., Shi, D., and Fairchild, A. J. (2023) "Variable Selection for Mediators under a Bayesian Mediation Model" <doi:10.1080/10705511.2022.2164285>

Examples

# Mixed case: Binary M, Continuous Y
set.seed(789)
n <- 100
toy_data <- data.frame(
   X = rnorm(n),
   M1 = rbinom(n, 1, 0.4),
   M2 = rbinom(n, 1, 0.6),
   Y = rnorm(n)
)

# Fit the model
results <- buzzEBMcatMcontY(
   model    = "Y ~ M1 + M2 | X",
   dataset  = toy_data,
   n_burnin = 200,
   n_iter   = 1000
)

summary(results)


Fit Bayesian Mediation Model (Continuous M & Binary Y)

Description

This function implements variable selection for mediators in a mixed data framework. It assumes a Gaussian likelihood for the mediators and a latent probit link for the binary outcome. It automates data preparation, JAGS model construction, and MCMC sampling.

Usage

buzzEBMcontMcatY(
  model,
  dataset,
  my_prior = NULL,
  advanced = NULL,
  m.prec.shape = NULL,
  m.prec.rate = NULL,
  a.coef.mean = NULL,
  a.coef.prec = NULL,
  b.coef.mean = NULL,
  b.coef.prec = NULL,
  a.pip.hyperalpha = NULL,
  a.pip.hyperbeta = NULL,
  b.pip.hyperalpha = NULL,
  b.pip.hyperbeta = NULL,
  direct.coef.mean = NULL,
  direct.coef.precision = NULL,
  m.prec.init = NULL,
  direct.coef.init = NULL,
  a.pip.hyperprior.init = NULL,
  b.pip.hyperprior.init = NULL,
  n_chains = NULL,
  n_adapt = NULL,
  n_burnin = NULL,
  n_iter = NULL,
  thin = NULL
)

Arguments

model

A description of the model to be fitted. This is typically a formula or a character string using lavaan syntax (e.g., Y ~ M + X).

dataset

A data.frame containing the variables specified in the model.

my_prior

Optional data.frame containing custom prior specifications. Run parms <- run_parms_wizard() to see the required structure.

advanced

Character. Use "interactive" for an interactive wizard to choose parameter distributions, or leave NULL for defaults.

m.prec.shape, m.prec.rate

Numeric scalar or vector of length equal to the number of mediators. Shape and rate parameters of the Gamma hyperprior for the mediator residual precisions. By default, a Gamma distribution is used. The default values are 1 and 0.001, respectively.

a.coef.mean, a.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the a path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

b.coef.mean, b.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the b path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

a.pip.hyperalpha, a.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the a path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

b.pip.hyperalpha, b.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the b path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

direct.coef.mean, direct.coef.precision

Numeric scalar or vector. Mean and precision parameters for the Normal prior of the direct effects (c'). By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

m.prec.init

Numeric or NULL. Initial values for residual precisions. Default is 1.

direct.coef.init

Numeric or NULL. Initial value for the direct effect (c'). Default is 0.

a.pip.hyperprior.init, b.pip.hyperprior.init

Numeric or NULL. Initial inclusion probabilities (PIP). Default is 0.5.

n_chains

Integer. Number of MCMC chains.

n_adapt

Integer. Number of adaptation iterations.

n_burnin

Integer. Number of burn-in iterations.

n_iter

Integer. Number of post-burn-in iterations.

thin

Integer. Thinning interval.

Details

Fits a Bayesian mediation model specifically designed for cases where the mediators (M) are continuous and the outcome variable (Y) is binary (0/1).

This function is a specific "worker" function. It identifies X, M, Y via .parse_buzz_syntax. While M is modeled with residual precision parameters, Y is binary and thus its residual precision is fixed to 1 within the latent variable framework.

Value

An object of class mcmc.list containing posterior samples.

References

Shi, D., Shi, D., and Fairchild, A. J. (2023) "Variable Selection for Mediators under a Bayesian Mediation Model" <doi:10.1080/10705511.2022.2164285>

Examples

# Mixed case: Continuous M, Binary Y
set.seed(456)
n <- 100
toy_data <- data.frame(
   X = rnorm(n),
   M1 = rnorm(n),
   M2 = rnorm(n),
   Y = rbinom(n, 1, 0.5)
)

# Fit the model
results <- buzzEBMcontMcatY(
   model    = "Y ~ M1 + M2 | X",
   dataset  = toy_data,
   n_burnin = 200,
   n_iter   = 1000
)

summary(results)


Fit Bayesian Mediation Model (Continuous M & Continuous Y)

Description

This function implements variable selection for mediators in a fully continuous framework. It automates data preparation, JAGS model construction using Gaussian likelihoods, and MCMC sampling.

Usage

buzzEBMcontMcontY(
  model,
  dataset,
  my_prior = NULL,
  advanced = NULL,
  m.prec.shape = NULL,
  m.prec.rate = NULL,
  y.prec.shape = NULL,
  y.prec.rate = NULL,
  a.coef.mean = NULL,
  a.coef.prec = NULL,
  b.coef.mean = NULL,
  b.coef.prec = NULL,
  a.pip.hyperalpha = NULL,
  a.pip.hyperbeta = NULL,
  b.pip.hyperalpha = NULL,
  b.pip.hyperbeta = NULL,
  direct.coef.mean = NULL,
  direct.coef.precision = NULL,
  m.prec.init = NULL,
  y.prec.init = NULL,
  direct.coef.init = NULL,
  a.pip.hyperprior.init = NULL,
  b.pip.hyperprior.init = NULL,
  n_chains = NULL,
  n_adapt = NULL,
  n_burnin = NULL,
  n_iter = NULL,
  thin = NULL
)

Arguments

model

A description of the model to be fitted. This is typically a formula or a character string using lavaan syntax (e.g., Y ~ M + X).

dataset

A data.frame containing the variables specified in the model.

my_prior

Optional data.frame containing custom prior specifications. Run parms <- run_parms_wizard() to see the required structure.

advanced

Character. Use "interactive" for an interactive wizard to choose parameter distributions, or leave NULL for defaults.

m.prec.shape, m.prec.rate

Numeric scalar or vector of length equal to the number of mediators. Shape and rate parameters of the Gamma hyperprior for the mediator residual precisions. By default, a Gamma distribution is used. The default values are 1 and 0.001, respectively.

y.prec.shape, y.prec.rate

Numeric scalar. Shape and rate parameters of the Gamma hyperprior for the outcome residual precision. By default, a Gamma distribution is used. The default values are 1 and 0.001, respectively.

a.coef.mean, a.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the a path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

b.coef.mean, b.coef.prec

Numeric scalar or vector. Mean and precision parameters of the Normal prior for the b path effects. By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

a.pip.hyperalpha, a.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the a path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

b.pip.hyperalpha, b.pip.hyperbeta

Numeric scalar or vector. Alpha and beta parameters for the Beta hyperprior of the b path inclusion probabilities. By default, a Beta distribution is used. The default value is 3.

direct.coef.mean, direct.coef.precision

Numeric scalar or vector. Mean and precision parameters for the Normal prior of the direct effects (c'). By default, a Normal distribution is used. The default values are 0 and 1.0E-6, respectively.

m.prec.init, y.prec.init

Numeric or NULL. Initial values for residual precisions. Default is 1.

direct.coef.init

Numeric or NULL. Initial value for the direct effect (c'). Default is 0.

a.pip.hyperprior.init, b.pip.hyperprior.init

Numeric or NULL. Initial inclusion probabilities (PIP). Default is 0.5.

n_chains

Integer. Number of MCMC chains.

n_adapt

Integer. Number of adaptation iterations.

n_burnin

Integer. Number of burn-in iterations.

n_iter

Integer. Number of post-burn-in iterations.

thin

Integer. Thinning interval.

Details

Fits a Bayesian mediation model specifically designed for cases where both the mediators (M) and the outcome variable (Y) are continuous.

This function is a specific "worker" function. It identifies X, M, Y via .parse_buzz_syntax and performs Bayesian estimation assuming: M \sim Normal(\dots) and Y \sim Normal(\dots).

Value

An object of class mcmc.list containing posterior samples.

References

Shi, D., Shi, D., and Fairchild, A. J. (2023) "Variable Selection for Mediators under a Bayesian Mediation Model" <doi:10.1080/10705511.2022.2164285>

Examples

# 1. Create a continuous synthetic dataset
set.seed(123)
n <- 100
toy_data <- data.frame(
   X = rnorm(n),
   M1 = rnorm(n),
   M2 = rnorm(n),
   Y = rnorm(n)
)

# 2. Fit the model using lavaan-style syntax
# We define Y as the outcome, with M1, M2, and X as predictors
results <- buzzEBMcontMcontY(
   model    = "Y ~ M1 + M2 | X",
   dataset  = toy_data,
   n_burnin = 500,
   n_iter   = 2000
)

# 3. Check results
summary(results)


Define Initial Values for Bayesian Mediation Model

Description

This internal function constructs the starting values for both the parameters (coefficients and precisions) and the latent variables (inclusion indicators). It handles the conditional inclusion of precision parameters based on whether the mediators and outcome are continuous.

Usage

define_init_values(
  P,
  K,
  M_cont,
  Y_cont,
  m.prec.init,
  y.prec.init,
  direct.coef.init,
  a.pip.hyperprior.init,
  b.pip.hyperprior.init
)

Arguments

P

Integer. Number of predictors (X) in the model.

K

Integer. Number of mediators (M) in the model.

M_cont

Logical. If TRUE, initializes mediator precision (m.prec). Set to FALSE for binary mediators.

Y_cont

Logical. If TRUE, initializes outcome precision (y.prec). Set to FALSE for binary outcomes.

m.prec.init, y.prec.init

Numeric or NULL. Initial values for residual precisions. Default is 1.

direct.coef.init

Numeric or NULL. Initial value for the direct effect (c'). Default is 0.

a.pip.hyperprior.init, b.pip.hyperprior.init

Numeric or NULL. Initial inclusion probabilities (PIP). Default is 0.5.

Details

Generates a named list of initial values for MCMC chains, specifically tailored for the Bayesian mediation model with variable selection.

The function uses the null-coalescing operator %||% to apply system defaults when NULL values are passed from the main wrapper functions.

Value

A named list of initial values compatible with JAGS:

m.prec

Numeric vector of length K (if M_cont = TRUE).

a.coef

Numeric matrix of dimension K x P, initialized to 0.

a.pip

Binary vector of length K, initialized to 0.

b.coef

Numeric vector of length K, initialized to 0.

b.pip

Binary vector of length K, initialized to 0.

y.prec

Numeric scalar (if Y_cont = TRUE).

direct.coef

Numeric vector of length P, initialized to direct.coef.init.

a.pip.hyperprior

Numeric scalar.

b.pip.hyperprior

Numeric scalar.


Extract and Rename Mediation Results from JAGS Output

Description

Parses the summary statistics from a coda MCMC output object and extracts rows corresponding to ind.joint, a.pip.hyperprior, and b.pip.hyperprior.

Usage

extract_results(output, M)

Arguments

output

An mcmc.list object.

M

Character vector of mediator variable names.

Value

A named numeric matrix.


Create Parameter Mapping from Individual Arguments

Description

This internal worker function compiles individual numeric prior arguments into the standard parameters data frame used for JAGS model construction.

Usage

make_parms_from_argument(
  a.coef.mean = NULL,
  a.coef.prec = NULL,
  b.coef.mean = NULL,
  b.coef.prec = NULL,
  m.prec.shape = NULL,
  m.prec.rate = NULL,
  y.prec.shape = NULL,
  y.prec.rate = NULL,
  a.pip.hyperalpha = NULL,
  a.pip.hyperbeta = NULL,
  b.pip.hyperalpha = NULL,
  b.pip.hyperbeta = NULL,
  direct.coef.mean = NULL,
  direct.coef.precision = NULL
)

Arguments

a.coef.mean, a.coef.prec

Numeric. Mean and precision for the a path coefficients (a.coef).

b.coef.mean, b.coef.prec

Numeric. Mean and precision for the b path coefficients (b.coef).

m.prec.shape, m.prec.rate

Numeric. Shape and rate for the mediator residual precision (m.prec).

y.prec.shape, y.prec.rate

Numeric. Shape and rate for the outcome residual precision (y.prec).

a.pip.hyperalpha, a.pip.hyperbeta

Numeric. Alpha and beta parameters for the a path inclusion probability hyperprior.

b.pip.hyperalpha, b.pip.hyperbeta

Numeric. Alpha and beta parameters for the b path inclusion probability hyperprior.

direct.coef.mean, direct.coef.precision

Numeric. Mean and precision for the direct effect (c').

Details

This function is called by make_parms_main when a user provides individual named arguments. It performs several key tasks:

Value

A data.frame containing columns: priors, distribution, arguments, and template.

See Also

make_parms_main


Create Parameter Mapping from Data Frame

Description

This internal worker function validates and merges a user-supplied prior specification data frame with the system defaults.

Usage

make_parms_from_df(my_prior)

Arguments

my_prior

A data.frame containing the columns: priors, distribution, and arguments.

Details

This function is called by make_parms_main when a user provides a custom prior data frame (Method 2) or completes the interactive wizard. It enforces the following logic:

Value

A validated data.frame with columns: priors, distribution, arguments, and template.

See Also

make_parms_main, run_parms_wizard


Internal Router for Parameter Dataframe Construction

Description

This function is the central hub for resolving prior specifications. It determines which method of parameter construction to use based on user input, enforcing a strict hierarchy to handle overlapping arguments.

Usage

make_parms_main(
  m.prec.shape = NULL,
  m.prec.rate = NULL,
  y.prec.shape = NULL,
  y.prec.rate = NULL,
  a.coef.mean = NULL,
  a.coef.prec = NULL,
  b.coef.mean = NULL,
  b.coef.prec = NULL,
  a.pip.hyperalpha = NULL,
  a.pip.hyperbeta = NULL,
  b.pip.hyperalpha = NULL,
  b.pip.hyperbeta = NULL,
  direct.coef.mean = NULL,
  direct.coef.precision = NULL,
  my_prior = NULL,
  advanced = NULL
)

Arguments

m.prec.shape, m.prec.rate, y.prec.shape, y.prec.rate, a.coef.mean, a.coef.prec, b.coef.mean, b.coef.prec, a.pip.hyperalpha, a.pip.hyperbeta, b.pip.hyperalpha, b.pip.hyperbeta, direct.coef.mean, direct.coef.precision

Numeric. Individual prior parameters (Method 1).

my_prior

A data frame or list provided by the user (Method 2). Lists are automatically coerced to data frames.

advanced

Controls routing override behavior. Acceptable values: NULL (default standard routing), "interactive" (CLI wizard), "myprior" (use my_prior directly), or FALSE (force system defaults).

Details

The function evaluates inputs in the following **Priority Order**:

  1. Skip to Named Arguments (Trigger: advanced = FALSE)

    • Bypasses my_prior and the wizard entirely.

    • If named arguments are provided, calls make_parms_from_argument.

    • If no named arguments are provided, falls back to system defaults.

    • Note: my_prior is ignored with a message.

  2. Interactive Wizard (Trigger: advanced = "interactive")

    • Calls run_parms_wizard.

    • If the user completes the wizard, the result is passed to make_parms_from_df.

    • If the user cancels, it falls back to system defaults.

    • Note: All other arguments are ignored with a message.

  3. Use my_prior Directly (Trigger: advanced = "myprior")

    • Requires my_prior to be non-NULL; errors otherwise.

    • Passes my_prior directly to make_parms_from_df.

    • Note: Named arguments are ignored with a message.

  4. Standard Routing (Trigger: advanced = NULL)

    • User Dataframe: if my_prior is not NULL, validates and passes it to make_parms_from_df. Named arguments are ignored with a message.

    • Named Arguments: if any Method 1 argument is not NULL, compiles them and calls make_parms_from_argument.

    • System Defaults: silent fallback if nothing else is provided.

Value

A validated data.frame of parameters (priors, distribution, arguments) ready for model use.


Prepare Data for buzzMed Model

Description

This internal helper extracts the predictor(s), mediators, and outcome from the dataset, handles matrix conversions for the predictors, and creates a named list of mediators (m1, m2, ... mk) to match the indexing used in the generated JAGS model strings.

Usage

prepare_ebmed_data(dataset, X, M, Y, M_cont, Y_cont)

Arguments

dataset

A data.frame containing outcome, predictor, and mediator variables.

X

Character vector naming the predictor variable(s).

M

Character vector naming mediator variables.

Y

Character string naming the outcome variable.

M_cont

Logical. Should M be treated as continuous?

Y_cont

Logical. Should Y be treated as continuous?

Details

Converts a user-supplied data frame into the specific list format required by the Bayesian mediation JAGS models.

Predictors are converted to a matrix to allow for multivariate X (e.g., when including covariates). Mediators are split into individual list elements to simplify the JAGS for loops.

Value

A list containing:

N

Integer. The number of observations (rows).

X

Numeric matrix of predictors.

y

Numeric vector (or binary 0/1 vector) of the outcome.

m1, m2, ...

Individual numeric vectors for each mediator.


Execute JAGS MCMC Sampling for Exploratory Bayesian Mediation Models

Description

A worker function that handles the technical execution of the MCMC chains. It dynamically determines which parameters to monitor based on the distributional assumptions (continuous vs. binary) of the mediators and outcome.

Usage

run_ebmed_jags(
  modelstring,
  bdata,
  init,
  M_cont,
  Y_cont,
  n_chains,
  n_adapt,
  n_burnin,
  n_iter,
  thin
)

Arguments

modelstring

Character string containing the generated JAGS model code.

bdata

Named list of data (N, X, y, m1, m2...) for JAGS.

init

Named list of starting values for the parameters.

Y_cont, M_cont

Logical flags indicating the nature of the variables. These determine whether residual precisions (y.prec, m.prec) are monitored.

n_chains, n_adapt, n_burnin, n_iter, thin

Integer. MCMC tuning parameters. If NULL, system defaults are applied (1, 1000, 1000, 10000, and 1, respectively).

Details

This internal function interfaces with the rjags package to initialize the model, perform burn-in, and collect posterior samples.

The function follows the standard JAGS workflow:

  1. Initialization: Calls jags.model with adaptation.

  2. Burn-in: Discards initial samples using update.

  3. Sampling: Collects final posteriors via coda.samples.

Parameters monitored by default include coefficients (a, b, direct.coef), inclusion probabilities (pip), and joint inclusion indicators (ind.joint).

Value

A coda::mcmc.list object containing the posterior samples for the monitored parameters.


Interactive Prior Specification Wizard

Description

This function allows users to customize priors without needing to manually construct complex data frames. It displays current defaults, allows selection of specific parameters to change, and provides a menu of available JAGS distributions (e.g., Normal, Gamma, Beta).

Usage

run_parms_wizard(verbose = TRUE)

Arguments

verbose

Logical. If TRUE (default), the wizard prints introductory text and the summary review table. If FALSE, only the interactive prompts are displayed.

Details

Launches an interactive console-based interface to guide users through viewing and modifying Bayesian prior distributions for mediation models.

The wizard handles two main types of customization:

For coefficients like a and b, the wizard will note if they reference hyperpriors (back-references). It is generally recommended to modify the shape of the hyperprior rather than the coefficient's distribution directly to maintain the hierarchical structure.

Value

A data.frame with columns priors, distribution, and arguments. This dataframe is formatted to be passed directly to the my_prior argument in related fitting functions.

Note

This function requires an interactive R session. It will return invisible(NULL) if the user cancels the process.

Examples

# Create toy data
toy_data <- data.frame(
   X = rbinom(100, 1, 0.5),
   M = rbinom(100, 1, 0.3),
   Y = rbinom(100, 1, 0.5)
)

custom_priors <- NULL

if (interactive()){
# Launch the wizard
custom_priors <- run_parms_wizard()
}

# Use the resulting object in a model
fit <- buzzEBMcatMcatY(model = "Y ~ M | X",
                     dataset = toy_data,
                     my_prior = custom_priors)