% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/wrapper_mediation.R
\name{rtmb_mediation}
\alias{rtmb_mediation}
\title{RTMB-based Mediation Analysis Wrapper}
\usage{
rtmb_mediation(
  formula,
  data,
  family = "gaussian",
  prior = prior_flat(),
  y_range = NULL,
  fixed = NULL,
  view = NULL,
  WAIC = FALSE,
  ...
)
}
\arguments{
\item{formula}{A list of formulas defining the regression paths (e.g., `list(M ~ X, Y ~ X + M)`).}

\item{data}{A data frame containing the variables.}

\item{family}{A single character string or a list of character strings specifying the error distribution
for each equation (e.g., `family = list("gaussian", "binomial")`). Default is "gaussian".}

\item{prior}{An object of class "rtmb_prior" specifying the prior distribution.}

\item{y_range}{Theoretical minimum and maximum values of the response variable.}

\item{fixed}{A named list of parameter values to fix (optional).}

\item{view}{Character vector of parameter names to prioritize in summary.}

\item{WAIC}{Logical; if TRUE, add pointwise `log_lik` to the generate block for WAIC.}

\item{...}{Additional arguments passed to the model construction.}
}
\value{
An `RTMB_Model` object.
}
\description{
`rtmb_mediation` performs mediation analysis by simultaneously estimating multiple
GLM regression equations. It automatically identifies mediation paths and calculates
indirect, direct, and total effects.
}
\details{
The function identifies mediation paths by looking for variables that are
responses in one equation and predictors in another. Indirect effects are
calculated as the product of coefficients along these paths (\eqn{a * b}).

\strong{Uncertainty Estimation}:
When using `$optimize(ci_method = "sampling")`, the function provides asymmetric
confidence intervals for indirect effects based on the distribution of products,
which is more accurate than the standard Sobel test (Delta Method).
}
\examples{

  # Simulate mediation data
  set.seed(123)
  N <- 100
  X <- rnorm(N)
  # M is influenced by X
  M <- 0.5 * X + rnorm(N, 0, 0.5)
  # Y is influenced by both X and M
  Y <- 0.3 * X + 0.8 * M + rnorm(N, 0, 0.5)
  dat <- data.frame(X, M, Y)

  # Fit a mediation model
  # The formula list specifies the two regression equations
  fit_med <- rtmb_mediation(list(M ~ X, Y ~ X + M), data = dat)
  
  # Maximum A Posteriori (MAP) estimation
  map_med <- fit_med$optimize()
  # The summary automatically calculates indirect, direct, and total effects
  map_med$summary()
}
