Author: RMediation Development Team

# Comparing Confidence Interval Methods in RMediation

#### RMediation Development Team

## Introduction

This vignette compares the different confidence interval methods available in the RMediation package. Understanding the differences between methods is important for selecting the most appropriate approach for your mediation analysis.

## Overview of Methods

RMediation provides several methods for computing confidence intervals for indirect effects:

- Distribution of Product (DOP): Exact method for the product of two or three normal random variables (`medci()` / `ProductNormal3`)
- Monte Carlo (MC): Simulation-based approach using random sampling
- Asymptotic Normal: Traditional approach based on normal theory (e.g., Sobel test)
- MBCO (Model-Based Constrained Optimization): Advanced frequentist method for hypothesis testing

## Method Comparison

Let’s compare different methods using the same example:

    # Same example parameters
    mu.x <- 0.5 # Effect of X on M
    mu.y <- 0.6 # Effect of M on Y (controlling for X)
    se.x <- 0.08 # Standard error of a path
    se.y <- 0.04 # Standard error of b path
    rho <- 0 # Correlation between a and b

    # Compare all methods
    results <- medci(
      mu.x = mu.x, mu.y = mu.y,
      se.x = se.x, se.y = se.y,
      rho = rho,
      type = "all", # This gives results for all methods
      plot = FALSE
    )

    # Print results
    for (method_name in names(results)) {
      cat("\n", method_name, ":\n")
      # Display the structure of each result
      str(results[[method_name]])
    }
    #>
    #>  Distribution of Product :
    #> List of 3
    #>  $ 95% CI  : num [1:2] 0.201 0.405
    #>  $ Estimate: num 0.3
    #>  $ SE      : num 0.0521
    #>
    #>  Monte Carlo :
    #> List of 4
    #>  $ 95% CI  : num [1:2] 0.201 0.405
    #>  $ Estimate: num 0.3
    #>  $ SE      : num 0.0522
    #>  $ MC.Error: num 5.22e-07
    #>
    #>  Asymptotic Normal :
    #> List of 3
    #>  $ 95% CI  : num [1:2] 0.198 0.402
    #>  $ Estimate: num 0.3
    #>  $ SE      : num 0.0521

## Performance and Accuracy Comparison

Different methods have different strengths and weaknesses:

    # Example of comparing results across different parameter values

    # Define parameter values to test
    param_sets <- list(
      list(mu.x = 0.5, mu.y = 0.6, se.x = 0.08, se.y = 0.04),
      list(mu.x = 0.3, mu.y = 0.4, se.x = 0.10, se.y = 0.05),
      list(mu.x = 0.1, mu.y = 0.2, se.x = 0.05, se.y = 0.03)
    )

    for (i in seq_along(param_sets)) {
      params <- param_sets[[i]]
      cat("\nParameter set", i, ":\n")
      cat("Effect sizes: a =", params$mu.x, ", b =", params$mu.y, "\n")

      results_subset <- medci(
        mu.x = params$mu.x, mu.y = params$mu.y,
        se.x = params$se.x, se.y = params$se.y,
        rho = 0,
        type = "all",
        plot = FALSE
      )

      # Print confidence intervals from each method
      for (method_name in names(results_subset)) {
        ci_values <- results_subset[[method_name]][[1]] # Extract CI values
        cat(sprintf("%-25s: [%.3f, %.3f]\n", method_name, ci_values[1], ci_values[2]))
      }
    }
    #>
    #> Parameter set 1 :
    #> Effect sizes: a = 0.5 , b = 0.6
    #> Distribution of Product  : [0.201, 0.405]
    #> Monte Carlo              : [0.202, 0.405]
    #> Asymptotic Normal        : [0.198, 0.402]
    #>
    #> Parameter set 2 :
    #> Effect sizes: a = 0.3 , b = 0.4
    #> Distribution of Product  : [0.040, 0.209]
    #> Monte Carlo              : [0.040, 0.210]
    #> Asymptotic Normal        : [0.036, 0.204]
    #>
    #> Parameter set 3 :
    #> Effect sizes: a = 0.1 , b = 0.2
    #> Distribution of Product  : [0.000, 0.042]
    #> Monte Carlo              : [0.000, 0.042]
    #> Asymptotic Normal        : [-0.001, 0.041]

## When to Use Each Method

### Distribution of Product (DOP)

- Best for: Small samples where normality assumptions may not hold; two- and three-variable products
- Advantages: Exact method for product of two or three normals (`ProductNormal3`), maintains Type I error rates
- Disadvantages: Limited to products of two or three variables; use MC for more complex functions

    # Example using DOP method
    result_dop <- medci(
      mu.x = 0.5, mu.y = 0.6,
      se.x = 0.08, se.y = 0.04,
      rho = 0,
      type = "dop"
    )

    cat("DOP Method Results:\n")
    #> DOP Method Results:
    str(result_dop)
    #> List of 3
    #>  $ 95% CI  : num [1:2] 0.201 0.405
    #>  $ Estimate: num 0.3
    #>  $ SE      : num 0.0521

### Monte Carlo (MC)

- Best for: Complex models with multiple mediators
- Advantages: Flexible, works with complex functions of parameters
- Disadvantages: Requires large sample sizes for stability

    # Example using MC method
    result_mc <- medci(
      mu.x = 0.5, mu.y = 0.6,
      se.x = 0.08, se.y = 0.04,
      rho = 0,
      type = "mc",
      n.mc = 1e5 # Specify Monte Carlo sample size
    )

    cat("Monte Carlo Method Results:\n")
    #> Monte Carlo Method Results:
    str(result_mc)
    #> List of 4
    #>  $ 95% CI  : num [1:2] 0.201 0.405
    #>  $ Estimate: num 0.3
    #>  $ SE      : num 0.0521
    #>  $ MC.Error: num 5.21e-07

### Asymptotic Normal

- Best for: Large samples with well-behaved distributions
- Advantages: Computationally fast
- Disadvantages: Can have poor Type I error control in small samples

    # Example using asymptotic method
    result_asymp <- medci(
      mu.x = 0.5, mu.y = 0.6,
      se.x = 0.08, se.y = 0.04,
      rho = 0,
      type = "asymp"
    )

    cat("Asymptotic Method Results:\n")
    #> Asymptotic Method Results:
    str(result_asymp)
    #> List of 3
    #>  $ 95% CI  : num [1:2] 0.198 0.402
    #>  $ Estimate: num 0.3
    #>  $ SE      : num 0.0521

## Working with S7 Classes

The modern S7 implementation provides method-specific functionality:

    # Create a ProductNormal object (represents distribution of product)
    pn <- ProductNormal(
      mu = c(0.5, 0.6), # Means of the two variables
      Sigma = matrix(c(0.0064, 0, 0, 0.0016), 2, 2) # Covariance matrix (se.x^2, 0, 0, se.y^2)
    )

    # Compute cumulative distribution function
    cat("CDF at 0.1:", cdf(pn, q = 0.1), "\n")
    cat("CDF at 0.3:", cdf(pn, q = 0.3), "\n")

    # Compute quantiles
    cat("2.5% quantile:", quantile(pn, p = 0.025), "\n")
    cat("97.5% quantile:", quantile(pn, p = 0.975), "\n")

    # Compute confidence interval
    ci_result <- ci(pn, level = 0.95)
    cat("95% Confidence Interval:", ci_result, "\n")

    # Print and summary methods
    show(pn) # Use show instead of print
    summary(pn) # This should work as it's a different S7 method

## Practical Recommendations

Based on simulation research and statistical theory, here are practical recommendations:

1. For simple mediation models with two paths: Use DOP method as it provides the most accurate confidence intervals.

2. For complex models: Use Monte Carlo methods which are more flexible and extend to higher-order products.

3. For hypothesis testing: Consider using the MBCO procedure which provides better Type I error control.

4. For publication: When possible, report results from multiple methods to show robustness.

## Conclusion

The choice of confidence interval method can significantly impact your conclusions about mediation effects. The RMediation package provides multiple rigorous methods that account for the non-normal distribution of the indirect effect, unlike simpler normal-theory approaches. Using the appropriate method for your specific situation will lead to more reliable inferences in mediation analysis.

For more information about the statistical methods, see the package documentation and references.