| Title: | Probabilistic Effect Sizes for Causal Mediation Analysis |
|---|---|
| Description: | Compute P_med, a scale-free probabilistic effect size for causal mediation analysis. |
| Authors: | Davood Tofighi [aut, cre] (ORCID: <https://orcid.org/0000-0001-8523-7776>) |
| Maintainer: | Davood Tofighi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-06-12 11:45:21 UTC |
| Source: | https://github.com/Data-Wise/probmed |
Generic function to extract mediation structure from fitted models. This function is provided by the medfit package and re-exported here for convenience.
object |
Fitted model object |
... |
Additional arguments passed to methods |
A medfit::MediationData object
extract_mediation for full documentation
Compute , a scale-free probabilistic effect size for mediation
analysis, along with the traditional Indirect Effect ().
Provides point estimates and bootstrap confidence intervals.
represents
:
the probability that the outcome under treatment with the mediator at its
treated level exceeds the outcome under treatment with the mediator at its
control level (a one-half correction is added for ties). Both potential
outcomes hold treatment at , so the direct effect cancels and
reflects mediation only; values indicate positive
mediation.
pmed(object, ...)pmed(object, ...)
object |
Either:
|
... |
Additional arguments passed to methods (see method documentation) |
PmedResult object containing:
estimate: P_med point estimate
ci_lower, ci_upper: Confidence interval bounds
ie_estimate: Indirect Effect point estimate
ie_ci_lower, ie_ci_upper: IE confidence interval
boot_estimates: Bootstrap distribution (if applicable)
method: Inference method used
Available inference methods via method argument:
"parametric_bootstrap" (default): Fast, assumes normality
"nonparametric_bootstrap": Robust to assumptions, slower
"plugin": Point estimate only, no CI
extract_mediation for extracting from fitted models
# Basic example with formula interface set.seed(123) n <- 200 data <- data.frame(X = rnorm(n), C = rnorm(n)) data$M <- 0.5 * data$X + 0.3 * data$C + rnorm(n) data$Y <- 0.4 * data$M + 0.2 * data$X + 0.2 * data$C + rnorm(n) # Compute with parametric bootstrap result <- pmed( Y ~ X + M + C, formula_m = M ~ X + C, data = data, treatment = "X", mediator = "M", method = "parametric_bootstrap", n_boot = 500 ) print(result) ## Not run: # View bootstrap distribution summary(result) plot(result) ## End(Not run)# Basic example with formula interface set.seed(123) n <- 200 data <- data.frame(X = rnorm(n), C = rnorm(n)) data$M <- 0.5 * data$X + 0.3 * data$C + rnorm(n) data$Y <- 0.4 * data$M + 0.2 * data$X + 0.2 * data$C + rnorm(n) # Compute with parametric bootstrap result <- pmed( Y ~ X + M + C, formula_m = M ~ X + C, data = data, treatment = "X", mediator = "M", method = "parametric_bootstrap", n_boot = 500 ) print(result) ## Not run: # View bootstrap distribution summary(result) plot(result) ## End(Not run)
Compute P_med from Formula
object |
Formula for outcome model (Y ~ X + M + C) |
formula_m |
Formula for mediator model (M ~ X + C) |
data |
Data frame |
treatment |
Character: treatment variable name |
mediator |
Character: mediator variable name |
family_y |
Family for outcome model (default: gaussian()) |
family_m |
Family for mediator model (default: gaussian()) |
x_ref |
Reference treatment value (default: 0) |
x_value |
Treatment value (default: 1) |
method |
Inference method: "parametric_bootstrap", "nonparametric_bootstrap", "plugin", "mbco" |
n_boot |
Number of bootstrap samples (default: 1000) |
ci_level |
Confidence level (default: 0.95) |
seed |
Random seed for reproducibility |
... |
Additional arguments |
method = "mbco" returns a Model-Based Constrained Optimization interval
(Tofighi & Kelley, 2020): a likelihood-ratio interval for P_med and for the
indirect effect a*b, obtained by inverting the constrained-likelihood test
rather than by resampling. It is deterministic (no n_boot, no seed) and
supports a Gaussian outcome and mediator, with covariates, and any treatment
contrast x_ref != x_value. For binary or other non-Gaussian models, use the
bootstrap methods.
For method = "mbco", the converged flag reflects the P_med interval
only. The indirect-effect interval is reported separately and may be NA on
a degenerate design (e.g. a non-finite delta-method scale for a*b) even when
the P_med interval converges; check ie_ci_lower / ie_ci_upper directly.
PmedResult object
# Toy example: Simple mediation model # Generate data where X affects Y through M set.seed(123) n <- 100 data <- data.frame( X = rnorm(n), C = rnorm(n) ) data$M <- 0.5 * data$X + 0.3 * data$C + rnorm(n, sd = 0.5) data$Y <- 0.4 * data$M + 0.2 * data$X + 0.2 * data$C + rnorm(n, sd = 0.5) # Compute P_med using plugin estimator (fast, no CI) result_plugin <- pmed( Y ~ X + M + C, formula_m = M ~ X + C, data = data, treatment = "X", mediator = "M", method = "plugin" ) print(result_plugin) # With parametric bootstrap for confidence intervals result_boot <- pmed( Y ~ X + M + C, formula_m = M ~ X + C, data = data, treatment = "X", mediator = "M", method = "parametric_bootstrap", n_boot = 200, # Use more (e.g., 1000+) in practice seed = 456 ) print(result_boot)# Toy example: Simple mediation model # Generate data where X affects Y through M set.seed(123) n <- 100 data <- data.frame( X = rnorm(n), C = rnorm(n) ) data$M <- 0.5 * data$X + 0.3 * data$C + rnorm(n, sd = 0.5) data$Y <- 0.4 * data$M + 0.2 * data$X + 0.2 * data$C + rnorm(n, sd = 0.5) # Compute P_med using plugin estimator (fast, no CI) result_plugin <- pmed( Y ~ X + M + C, formula_m = M ~ X + C, data = data, treatment = "X", mediator = "M", method = "plugin" ) print(result_plugin) # With parametric bootstrap for confidence intervals result_boot <- pmed( Y ~ X + M + C, formula_m = M ~ X + C, data = data, treatment = "X", mediator = "M", method = "parametric_bootstrap", n_boot = 200, # Use more (e.g., 1000+) in practice seed = 456 ) print(result_boot)
Computes the joint P_med for k parallel mediators (all moved together from
their control to their treated levels). Gaussian outcome and mediators only.
See vignette("parallel-mediation", package = "probmed") for the estimand.
S7 class for P_med computation results.
PmedResult( estimate = integer(0), ci_lower = integer(0), ci_upper = integer(0), ci_level = integer(0), method = character(0), n_boot = NA_integer_, boot_estimates = numeric(0), ie_estimate = NA_real_, ie_ci_lower = NA_real_, ie_ci_upper = NA_real_, ie_boot_estimates = numeric(0), x_ref = integer(0), x_value = integer(0), source_extract = NULL, converged = logical(0), call = NULL )PmedResult( estimate = integer(0), ci_lower = integer(0), ci_upper = integer(0), ci_level = integer(0), method = character(0), n_boot = NA_integer_, boot_estimates = numeric(0), ie_estimate = NA_real_, ie_ci_lower = NA_real_, ie_ci_upper = NA_real_, ie_boot_estimates = numeric(0), x_ref = integer(0), x_value = integer(0), source_extract = NULL, converged = logical(0), call = NULL )
estimate |
Numeric: P_med point estimate |
ci_lower |
Numeric: lower bound of confidence interval |
ci_upper |
Numeric: upper bound of confidence interval |
ci_level |
Numeric: confidence level (e.g., 0.95) |
method |
Character: inference method used |
n_boot |
Integer: number of bootstrap samples (NA if not bootstrap) |
boot_estimates |
Numeric vector: bootstrap distribution (empty if not bootstrap) |
ie_estimate |
Numeric: Indirect Effect (NIE) point estimate |
ie_ci_lower |
Numeric: lower bound of NIE confidence interval |
ie_ci_upper |
Numeric: upper bound of NIE confidence interval |
ie_boot_estimates |
Numeric vector: bootstrap distribution of NIE |
x_ref |
Numeric: reference treatment value |
x_value |
Numeric: treatment value for contrast |
source_extract |
medfit::MediationData object: source of the estimates |
converged |
Logical: did computation converge |
call |
Call object: original function call |