### README - Quick Start Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/MANIFEST.txt The README file provides a quick overview and reference for getting started with the mediation package. ```APIDOC ## README Provides an overview and quick reference for the mediation package. ### Getting Started 1. Read `README.md` for overview and quick reference. 2. Pick a function from the function index. 3. Read the corresponding `api-*.md` file for function details. 4. Refer to `types.md` for output structures. 5. Check `configuration.md` for settings. ``` -------------------------------- ### Example 3: Parallel Processing (Non-Windows) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-ivmediate.md Shows how to utilize multicore processing for faster computations on non-Windows systems. ```APIDOC ## Example 3: Parallel Processing (Non-Windows) ### Description This example enables parallel processing using the `multicore` and `mc.cores` arguments for potentially faster simulation and bootstrapping, particularly on Unix-like systems. ### Usage ```r library(parallel) result_parallel <- ivmediate( model_t, model_m, model_y, sims = 2000, boot = TRUE, enc = "treat", treat = "comply", mediator = "job_dich", multicore = TRUE, mc.cores = 4 ) summary(result_parallel) ``` ``` -------------------------------- ### Example 2: Using Nonparametric Bootstrap Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-ivmediate.md Illustrates how to enable nonparametric bootstrapping for confidence interval estimation. ```APIDOC ## Example 2: Using Nonparametric Bootstrap ### Description This example demonstrates the use of nonparametric bootstrapping within the `ivmediate` function to obtain confidence intervals for the mediation effects. ### Usage ```r # Same models as above result_boot <- ivmediate( model_t, model_m, model_y, sims = 1000, boot = TRUE, enc = "treat", treat = "comply", mediator = "job_dich" ) summary(result_boot) plot(result_boot) ``` ``` -------------------------------- ### Example 5: Continuous Mediator (Simple Case) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-ivmediate.md Illustrates mediation analysis with a continuous mediator in a simplified model setting. ```APIDOC ## Example 5: Continuous Mediator (Simple Case) ### Description This example handles a continuous mediator. It's applicable when the mediator is continuous and the regression models for the mediator and outcome include only the encouragement and treatment variables as predictors, alongside the mediator itself in the outcome model. ### Usage ```r # When mediator is continuous but models contain # only encouragement and treatment as predictors model_m_cont <- lm(job_hours ~ comply + treat, data = jobs) # Simple outcome model model_y_cont <- lm(depress2 ~ job_hours + (comply + treat), data = jobs) result_cont <- ivmediate( model_t, model_m_cont, model_y_cont, sims = 1000, boot = TRUE, enc = "treat", treat = "comply", mediator = "job_hours" ) summary(result_cont) ``` ``` -------------------------------- ### Example 6: Different Encouragement Values and Confidence Level Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-ivmediate.md Shows how to customize the confidence level for the intervals and implies handling of different encouragement variable values. ```APIDOC ## Example 6: Different Encouragement Values ### Description This example demonstrates how to specify a custom confidence level (e.g., 90%) for the calculated intervals. It also implicitly covers scenarios where the encouragement variable might have different values, though the example uses the default 0/1 binary encouragement. ### Usage ```r # If using binary encouragement variable with values 0/1 # (default in most designs) # Customize confidence level result_custom <- ivmediate( model_t, model_m, model_y, sims = 1000, boot = FALSE, enc = "treat", treat = "comply", mediator = "job_dich", conf.level = 0.90 # 90% confidence intervals instead of 95% ) summary(result_custom) ``` ``` -------------------------------- ### Setup Multiple Mediations Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Configure multiple mediation analyses by defining datasets, treatment variables, mediators, outcomes, and covariates. Ensure dataset names start with their corresponding treatment variable names. ```r # Dataset naming convention datasets <- list( T1 = data1, # Name must start with treatment variable name T2 = data2 ) treatment <- c("T1", "T2") # Must match dataset name prefixes mediators <- c("M1", "M2") outcome <- c("Y1") covariates <- "X1 + X2 + X3" # Single string for all models result <- mediations( datasets = datasets, treatment = treatment, mediators = mediators, outcome = outcome, covariates = covariates, families = c("gaussian", "gaussian"), sims = 1000 ) ``` -------------------------------- ### Example 4: Point Estimates Only (No Confidence Intervals) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-ivmediate.md Demonstrates how to obtain only point estimates by disabling confidence interval calculations. ```APIDOC ## Example 4: Point Estimates Only (No Confidence Intervals) ### Description This example shows how to request only point estimates (disabling confidence intervals) by setting `ci = FALSE`. This can be useful for exploratory analysis or when computational speed is critical. ### Usage ```r # For exploratory analysis or when computation speed critical result_point <- ivmediate( model_t, model_m, model_y, ci = FALSE, enc = "treat", treat = "comply", mediator = "job_dich" ) # Only returns point estimates (dc0, dc1, zc0, zc1, tauc) summary(result_point) ``` ``` -------------------------------- ### Example 1: Binary Mediator and Continuous Outcome Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-ivmediate.md Demonstrates a typical use case with a binary mediator and a continuous outcome, using standard regression models. ```APIDOC ## Example 1: Binary Mediator and Continuous Outcome ### Description This example shows how to perform an IV mediation analysis when the mediator is binary and the outcome is continuous. It sets up three regression models and then calls `ivmediate`. ### Usage ```r library(mediation) data(jobs) # Model for actual treatment (encouraged vs. not) model_t <- lm(comply ~ treat + sex + age + marital + nonwhite + educ + income, data = jobs) # Model for mediator (binary job discrimination) model_m <- glm(job_dich ~ comply + treat + sex + age + marital + nonwhite + educ + income, data = jobs, family = binomial) # Model for outcome model_y <- lm(depress2 ~ job_dich * (comply + treat) + sex + age + marital + nonwhite + educ + income, data = jobs) # IV mediation analysis result <- ivmediate( model_t, model_m, model_y, sims = 1000, boot = FALSE, enc = "treat", treat = "comply", mediator = "job_dich" ) summary(result) plot(result) ``` ``` -------------------------------- ### Output Structure Example Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md Illustrates the naming convention for elements within the returned list of mediate objects, based on outcome, treatment, and mediator variable names. ```R output$outcome.treatment.mediator1 output$outcome.treatment.mediator2 ``` -------------------------------- ### Usage Example for test.modmed Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-testing-functions.md Demonstrates how to use the `test.modmed` function to compare mediation effects for different moderator values. Requires fitting mediation models first. ```r library(mediation) data(jobs) # Fit models with covariate interactions mediator_int <- lm(job_seek ~ treat*age + econ_hard + sex, data = jobs) outcome_int <- lm(depress2 ~ treat*job_seek*age + econ_hard + sex, data = jobs) # Mediation analysis result <- mediate(mediator_int, outcome_int, sims = 1000, treat = "treat", mediator = "job_seek") # Test difference for age = 20 vs age = 70 test_result <- test.modmed( result, covariates.1 = list(age = 20), covariates.2 = list(age = 70) ) # View results print(test_result) summary(test_result) ``` -------------------------------- ### Enable speedglm for Bootstrap Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Demonstrates how to enable the use of the speedglm package for faster model fitting during bootstrap iterations within the mediate() function. Requires speedglm to be installed. ```r # Enable speedglm for bootstrap iterations use_speed <- TRUE # In mediate() call ``` -------------------------------- ### Mediation Analysis with Weights Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md This example shows how to incorporate survey weights into the mediation analysis. Provide the name of the weight variable in the 'weights' argument. ```r datasets <- list(treat = jobs) treatment <- "treat" mediators <- c("job_seek", "job_dich") outcome <- c("depress2") covariates <- "econ_hard + sex + age" result <- mediations( datasets, treatment, mediators, outcome, covariates = covariates, families = c("gaussian", "gaussian"), weights = "sample_weight", # Variable in data frame sims = 1000 ) summary(result) ``` -------------------------------- ### Binary Probit Models for Mediators and Outcome Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md Use this example when both your mediators and outcome variables are binary. Specify 'binomial' for each in the 'families' argument. ```r datasets <- list(treat = jobs) treatment <- "treat" mediators <- c("job_disc", "job_dich") outcome <- "work1" covariates <- "econ_hard + sex + age" result <- mediations( datasets, treatment, mediators, outcome, covariates = covariates, families = c("binomial", "binomial"), # Binary mediators and outcome sims = 1000 ) summary(result) ``` -------------------------------- ### Mediator Model with Treatment-Mediator Interaction Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate.md Shows how to include a treatment-mediator interaction term in the outcome model to estimate effects when the mediator's effect on the outcome depends on the treatment. Uses the same setup as the linear model example. ```r outcome_int <- lm(depress2 ~ treat + job_seek + treat:job_seek + econ_hard + sex + age, data = jobs) result_int <- mediate( mediator_model, outcome_int, sims = 1000, treat = "treat", mediator = "job_seek" ) summary(result_int) ``` -------------------------------- ### API Reference - Core Functions Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/MANIFEST.txt Detailed documentation for each of the 13 main functions available in the mediation package. This includes function signatures, parameter descriptions, and usage examples. ```APIDOC ## API Reference This section provides detailed documentation for each function in the mediation package. ### Function Index - **Basic mediation**: `api-mediate.md` - **Sensitivity analysis**: `api-medsens.md` - **Noncompliance mediation**: `api-ivmediate.md` - **Multiple mediators**: `api-multimed.md` - **Design-based mediation**: `api-mediate-designs.md` - **Batch processing**: `api-mediations.md` - **Testing functions**: `api-testing-functions.md` Each `api-*.md` file contains: - Function signature - Parameter details (from roxygen comments) - Usage examples with output patterns - Error handling documentation - Return value structures ``` -------------------------------- ### Single Experiment Design with SI and Bootstrap Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate-designs.md Example demonstrating the use of the nonparametric bootstrap method within mediate.sed for estimating ACME when the sequential ignorability assumption holds. Allows for more simulations. ```r result_boot <- mediate.sed("depress2", "job_disc", "treat", jobs, SI = TRUE, boot = TRUE, sims = 1000) summary(result_boot) ``` -------------------------------- ### Get Standard Mediation Summary Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Generate a standard summary of the mediation analysis results. This output includes p-values. ```r # Standard summary summary(result) ``` -------------------------------- ### Ordered Probit Outcome Model Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md This example demonstrates how to use an ordered probit model for the outcome variable. Ensure the outcome variable is an ordered factor and specify 'oprobit' in the 'families' argument for the outcome. ```r # Assuming job_disc is ordered factor with 3 levels jobs$job_disc_ord <- factor(jobs$job_disc, ordered = TRUE) datasets <- list(treat = jobs) treatment <- "treat" mediators <- c("job_seek", "job_dich") outcome <- "job_disc_ord" covariates <- "econ_hard + sex + age" result <- mediations( datasets, treatment, mediators, outcome, covariates = covariates, families = c("gaussian", "oprobit"), # Linear mediators, ordered outcome sims = 1000 ) # Result has class mediations.order class(result) summary(result) plot(result) ``` -------------------------------- ### Quantile Regression for Outcome Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md This example shows how to perform quantile regression for the outcome variable. Specify 'quantile' in the 'families' argument and set 'tau.y' to the desired quantile (e.g., 0.5 for the median). ```r datasets <- list(treat = jobs) treatment <- "treat" mediators <- c("job_seek") outcome <- c("depress2") covariates <- "econ_hard + sex + age" # Median regression result <- mediations( datasets, treatment, mediators, outcome, covariates = covariates, families = c("gaussian", "quantile"), # Quantile outcome tau.y = 0.5, # Median sims = 1000 ) summary(result) ``` -------------------------------- ### Single Experiment Design with SI Assumption (Point Estimates) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate-designs.md Example of using mediate.sed to calculate nonparametric point estimates for ACME under the sequential ignorability assumption. Uses the 'jobs' dataset. ```r data(jobs) result_si <- mediate.sed("depress2", "job_disc", "treat", jobs, SI = TRUE) summary(result_si) ``` -------------------------------- ### R: Mediation Analysis Workflow Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/overview.md This R code demonstrates a typical workflow for mediation analysis using the 'mediation' package. It includes fitting linear models for the mediator and outcome, performing the mediation analysis, summarizing results, plotting, conducting sensitivity analysis, and testing for moderated mediation. Ensure the 'mediation' package and its dependencies are installed. ```r library(mediation) data(jobs) # 1. Fit models model.m <- lm(job_seek ~ treat + econ_hard + sex + age, data = jobs) model.y <- lm(depress2 ~ treat + job_seek + econ_hard + sex + age, data = jobs) # 2. Mediation analysis result <- mediate(model.m, model.y, sims = 1000, treat = "treat", mediator = "job_seek") # 3. Summarize results summary(result) plot(result) # 4. Sensitivity analysis sens <- medsens(result, rho.by = 0.1) summary(sens) plot(sens) # 5. Test for interaction test.TMint(result) # 6. Moderated mediation test.modmed(result, covariates.1 = list(age = 20), covariates.2 = list(age = 70)) ``` -------------------------------- ### Multiple Treatments, Multiple Mediators, Single Outcome Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md This example demonstrates mediation analysis with multiple treatment variables, multiple mediators, and a single outcome. Set 'interaction = TRUE' to include treatment-mediator interactions in the model. ```r # Hypothetical data with multiple treatment variables datasets <- list( treat1 = jobs, # Data frame containing treat1 variable treat2 = jobs # Data frame containing treat2 variable ) treatment <- c("treat1", "treat2") mediators <- c("job_seek", "job_dich") outcome <- c("depress2") covariates <- "econ_hard + sex + age" result <- mediations( datasets, treatment, mediators, outcome, covariates = covariates, families = c("gaussian", "gaussian"), interaction = TRUE, # Include T-M interaction sims = 1000 ) # Access results for specific combinations summary(result$depress2.treat1.job_seek) summary(result$depress2.treat2.job_dich) # Summary of all summary(result) ``` -------------------------------- ### Multiple Outcomes, Treatments, and Mediators Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md This example shows how to perform mediation analysis with multiple outcome variables, multiple treatment variables, and multiple mediators. The 'families' argument should specify the model type for each mediator and outcome combination. ```r # Three different outcome variables datasets <- list( educ = jobs, income = jobs ) treatment <- c("educ", "income") mediators <- c("job_seek", "job_dich") outcome <- c("depress2", "work1") covariates <- "sex + age" result <- mediations( datasets, treatment, mediators, outcome, covariates = covariates, families = c("gaussian", "binomial"), # Linear mediator, binary outcome sims = 1000, boot = TRUE ) # Accessing 2x2x2 = 8 different mediation analyses names(result) ``` -------------------------------- ### Get Mediation Summary by Group Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Generate a mediation summary organized by group, providing an overview of mediation effects within different subgroups. ```r summary(result, output = "bygroup") # Organized by group ``` -------------------------------- ### Single Experiment Design without SI Assumption (Bounds) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate-designs.md Example of using mediate.sed to compute nonparametric bounds for ACME when the sequential ignorability assumption is not made. Requires a subset of the data. ```r library(mediation) data(boundsdata) # Single experiment design without SI assumption (bounds) data_sed <- subset(boundsdata, manip == 0) result_bounds <- mediate.sed("out", "med", "ttt", data_sed, SI = FALSE) summary(result_bounds) ``` -------------------------------- ### Pooling Mediation Results Across Imputations Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-testing-functions.md This example demonstrates how to use the 'amelidiate' function to pool mediation analysis results. It first creates multiply imputed datasets using the 'mice' package, then runs mediation analyses on each imputed dataset, and finally pools the results using 'amelidiate'. This is useful when your data has missing values that have been handled through multiple imputation. ```r library(mediation) library(mice) # For imputation example data(jobs) # Create multiply imputed datasets # (Example uses mice package; any imputation method acceptable) imputed_data <- mice(jobs, m = 5) # 5 imputations datasets <- list() for (i in 1:5) { datasets[[i]] <- complete(imputed_data, i) } # Run mediations on each imputed dataset results_list <- list() for (i in 1:5) { results_list[[i]] <- mediations( datasets = list(treat = datasets[[i]]), treatment = "treat", mediators = "job_seek", outcome = "depress2", covariates = "econ_hard + sex + age", families = c("gaussian", "gaussian"), sims = 1000 ) } # Pool results across imputations pool <- amelidiate(results_list) # Use standard summary and plot methods summary(pooled) plot(pooled) ``` -------------------------------- ### Configure Bootstrap CI Method (BCA) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Sets the bootstrap confidence interval method to Bias-Corrected and Accelerated (BCA). Requires boot = TRUE. ```r # Bias-corrected and accelerated result <- mediate(..., boot = TRUE, boot.ci.type = "bca") ``` -------------------------------- ### Configure Parallel Processing on Linux/Mac Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Demonstrates enabling multicore processing on Linux or Mac systems using ivmediate() with multicore = TRUE and specifying the number of cores. ```r # On Linux/Mac: can use result <- ivmediate(..., multicore = TRUE, mc.cores = 4) ``` -------------------------------- ### Configure Bootstrap CI Method (Percentile) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Sets the bootstrap confidence interval method to the percentile method, which is the default behavior when boot = TRUE. ```r # Percentile method (default) result <- mediate(..., boot = TRUE, boot.ci.type = "perc") ``` -------------------------------- ### Configuration Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/MANIFEST.txt Information on how to customize the behavior of the mediation package. ```APIDOC ## Configuration Customize the behavior of the mediation package. ### Settings - Details on configuration options are available in `configuration.md`. - Includes information on default parameters. ``` -------------------------------- ### Summarize Mediation Design Results Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate-designs.md Use `summary.mediate.design` to get a tabular summary of mediation analysis results. The `print.summary.mediate.design` function displays these results. ```r summary.mediate.design(object, ...) print.summary.mediate.design(x, ...) ``` -------------------------------- ### Basic Mediation and Sensitivity Analysis in R Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/README.md This workflow demonstrates the basic steps for estimating mediation effects, testing their statistical significance, assessing robustness to unmeasured confounding, and testing for interaction between treatment and mediator. Requires prior fitting of mediator and outcome models. ```r # 1. Estimate effects result <- mediate(model.m, model.y, sims = 1000, treat = "T", mediator = "M") # 2. Test statistical significance summary(result) # 3. Assess robustness sens <- medsens(result) summary(sens) # 4. Test for interaction test.TMint(result) ``` -------------------------------- ### Tobit Censored Outcome Model Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediations.md Use this example for outcome variables that are censored. Specify 'tobit' in the 'families' argument and provide the 'LowerY' or 'UpperY' bounds. ```r datasets <- list(treat = jobs) treatment <- "treat" mediators <- c("job_seek") outcome <- c("income") covariates <- "econ_hard + sex + age" result <- mediations( datasets, treatment, mediators, outcome, covariates = covariates, families = c("gaussian", "tobit"), # Tobit outcome model LowerY = 0, # Lower bound (income >= 0) sims = 1000 ) summary(result) ``` -------------------------------- ### Get Mediation Summary by Effect Type Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Generate a mediation summary organized by effect type, useful for detailed analysis of different components of the mediation. ```r # Summary with specific output type (merMod) summary(result, output = "byeffect") # Organized by effect type ``` -------------------------------- ### Retrieve Default Cores Option Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Shows how to retrieve the 'mc.cores' option, defaulting to 2 if not explicitly set. This is used internally by parallel functions. ```r mc.cores = getOption("mc.cores", 2L) # Retrieves option or defaults to 2 ``` -------------------------------- ### S3 Methods (summary, plot, print) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/README.md Provides standard S3 methods for summarizing, visualizing, and printing mediation analysis results. These methods offer convenient ways to inspect and present the output from mediation functions. ```APIDOC ## S3 Methods ### Description Standard S3 methods for mediation objects. ### Methods - `summary()`: Provides tabular results. - `plot()`: Generates graphical summaries. - `print()`: Displays formatted output. ``` -------------------------------- ### Load mediation Package Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Loads the mediation package into the R session. This is a prerequisite for using any of its functions. ```r library(mediation) ``` -------------------------------- ### Sensitivity Analysis for Linear-Linear Models Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Configure the FGLS convergence tolerance for sensitivity analysis in linear-linear models. ```r sens <- medsens(med_result, eps = 1e-7) # FGLS convergence tolerance ``` -------------------------------- ### Fine-Grained Sensitivity Analysis with R2 Increments Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-multimed.md Perform a more detailed sensitivity analysis by specifying finer increments for the R-squared parameter. This allows for a more granular sweep of sensitivity parameters, with `R2.by` controlling the step size. ```r # More detailed sensitivity parameter sweeps result_fine <- multimed( outcome = "immigr", med.main = "emo", med.alt = "p_harm", treat = "treat", covariates = c("age", "educ", "gender", "income"), data = framing, design = "single", sims = 2000, R2.by = 0.05 # Finer increments than default 0.1 ) summary(result_fine) plot(result_fine, type = "R2-total") ``` -------------------------------- ### Coarser Grid Sensitivity Analysis Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Use a coarser grid for faster sensitivity analysis computation by increasing the `rho.by` parameter. ```r # Coarser grid for faster computation sens <- medsens(med_result, rho.by = 0.2) ``` -------------------------------- ### Sensitivity Analysis Visualization Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-multimed.md Visualize sensitivity to interaction heterogeneity and R-squared values for mediation analysis. Useful for understanding the robustness of mediation results to unobserved confounding. ```r # Visualize sensitivity to interaction heterogeneity plot(result, type = "sigma", tgroup = "average") plot(result, type = "R2-total", tgroup = "average") ``` -------------------------------- ### Multiple Imputation Workflow for Mediation Analysis Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-testing-functions.md This workflow demonstrates how to perform mediation analysis with missing data using the `mice` package. It involves imputing missing data, running mediation on each imputed dataset, and then pooling the results using `amelidiate`. ```r library(mice) # 1. Impute missing data imputed <- mice(original_data, m = 10) # 2. Run mediation on each imputed dataset results <- list() for (i in 1:10) { results[[i]] <- mediations( datasets = list(treat = complete(imputed, i)), treatment = "treat", mediators = "mediator", outcome = "outcome", covariates = "X1 + X2", families = c("gaussian", "gaussian"), sims = 1000 ) } # 3. Pool across imputations final <- amelidiate(results) summary(final) ``` -------------------------------- ### Binary Outcome with Nonparametric Bootstrap Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate.md Demonstrates mediation analysis with binary outcome and mediator variables using generalized linear models (probit link). Employs nonparametric bootstrapping for inference by setting `boot = TRUE`. ```r mediator_bin <- glm(job_dich ~ treat + econ_hard + sex + age, data = jobs, family = binomial(link = "probit")) outcome_bin <- glm(work1 ~ treat + job_dich + econ_hard + sex + age, data = jobs, family = binomial(link = "probit")) result_boot <- mediate( mediator_bin, outcome_bin, sims = 1000, boot = TRUE, treat = "treat", mediator = "job_dich" ) summary(result_boot) ``` -------------------------------- ### Basic Mediation Analysis in R Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/README.md Perform a basic mediation analysis by fitting linear models for the mediator and outcome, then using the `mediate` function. Requires the `mediation` package and a dataset like `jobs`. ```r library(mediation) data(jobs) # Fit models mediator_model <- lm(job_seek ~ treat + econ_hard + sex + age, data = jobs) outcome_model <- lm(depress2 ~ treat + job_seek + econ_hard + sex + age, data = jobs) # Estimate mediation effects result <- mediate(mediator_model, outcome_model, sims = 1000, treat = "treat", mediator = "job_seek") # Summary and visualization summary(result) plot(result) ``` -------------------------------- ### Linear Outcome and Mediator Model Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate.md Demonstrates fitting linear models for mediator and outcome, then estimating causal mediation effects using the mediate function. Requires the 'mediation' package and 'jobs' dataset. ```r library(mediation) data(jobs) # Fit mediator and outcome models mediator_model <- lm(job_seek ~ treat + econ_hard + sex + age, data = jobs) outcome_model <- lm(depress2 ~ treat + job_seek + econ_hard + sex + age, data = jobs) # Estimate causal mediation effects result <- mediate( mediator_model, outcome_model, sims = 1000, treat = "treat", mediator = "job_seek" ) summary(result) plot(result) ``` -------------------------------- ### Configure Censored Outcome Models (Tobit) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Use the 'tobit' family for outcome models when dealing with censored data. Specify 'LowerY' and 'UpperY' to define the censoring bounds. Set 'UpperY' to NULL for a one-sided tobit model. ```r # Tobit outcome (censored) result <- mediations( ..., families = c("gaussian", "tobit"), LowerY = 0, # Lower bound (censoring point) UpperY = NULL # No upper bound ) # Two-sided Tobit result <- mediations( ..., families = c("gaussian", "tobit"), LowerY = 0, # Lower bound UpperY = 100 # Upper bound ) ``` -------------------------------- ### Run SED Mediation Design Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-mediate-designs.md Execute the Simplest Experimental Design (SED) for mediation analysis. This requires specifying the treatment, mediator, and outcome variables, along with the dataset. Set `SI = TRUE` to assume Sequential Ignorability. ```r result <- mediate.sed("out", "med", "treat", data, SI = TRUE) summary(result) # Tabular summary ``` -------------------------------- ### Sensitivity Analysis for Binary Models Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Specify the number of Monte Carlo draws for sensitivity analysis when working with binary models. ```r sens <- medsens(med_result, sims = 2000) # MC draws for binary models ``` -------------------------------- ### Configure Parallel Processing on Windows Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Specifies how to disable multicore processing on Windows systems, where it is not supported, by setting multicore = FALSE in ivmediate(). ```r # On Windows: multicore = FALSE result <- ivmediate(..., multicore = FALSE) ``` -------------------------------- ### Configure Quantile Regression Models Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Specify 'quantile' family for mediator or outcome models to perform quantile regression. Use 'tau.m' for mediator quantiles and 'tau.y' for outcome quantiles. ```r # Quantile mediator model result <- mediations( ..., families = c("quantile", "gaussian"), tau.m = 0.5 # Median ) # Quantile outcome model result <- mediations( ..., families = c("gaussian", "quantile"), tau.y = 0.75 # 75th percentile ) ``` -------------------------------- ### Type Reference Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/MANIFEST.txt Documentation for data structures and types used within the mediation package, essential for understanding output patterns. ```APIDOC ## Type Reference This section details the data structures and types used in the mediation package. ### Data Structures - **Output Structures**: Defined in `types.md`. - **Class Definitions**: Derived from source code. ``` -------------------------------- ### Parallel Processing for IV Mediation (Non-Windows) Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/api-ivmediate.md Enables parallel processing for faster computation of IV mediation analysis on non-Windows systems. Specify the number of cores to use for parallel computation. ```r library(parallel) result_parallel <- ivmediate( model_t, model_m, model_y, sims = 2000, boot = TRUE, enc = "treat", treat = "comply", mediator = "job_dich", multicore = TRUE, mc.cores = 4 ) summary(result_parallel) ``` -------------------------------- ### Set Default Confidence Interval Level Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Illustrates setting the confidence level for interval estimation in mediate(). The default is 0.95 (95%). ```r result <- mediate(..., conf.level = 0.95) # Default result <- mediate(..., conf.level = 0.90) # 90% CI result <- mediate(..., conf.level = 0.99) # 99% CI ``` -------------------------------- ### Integrate Multiple Imputation with Mediation Source: https://github.com/kosukeimai/mediation/blob/master/_autodocs/configuration.md Run mediation analyses on multiple imputed datasets and pool the results. Requires the 'mice' package for imputation. ```r library(mice) # Create imputed datasets imputed <- mice(original_data, m = 10) # Run mediations on each results <- lapply(1:10, function(i) { mediations( datasets = list(treat = complete(imputed, i)), treatment = "treat", mediators = "mediator", outcome = "outcome", covariates = "X1 + X2", families = c("gaussian", "gaussian"), sims = 1000 ) }) # Pool using amelidiate pooled <- amelidiate(results) ```