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.
RMediation provides several methods for computing confidence intervals for indirect effects:
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.406
#> $ Estimate: num 0.3
#> $ SE : num 0.0521
#> $ MC.Error: num 5.21e-07
#>
#> Asymptotic Normal :
#> List of 3
#> $ 95% CI : num [1:2] 0.198 0.402
#> $ Estimate: num 0.3
#> $ SE : num 0.0521Different 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.201, 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.041, 0.209]
#> 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.001, 0.042]
#> Asymptotic Normal : [-0.001, 0.041]# 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.202 0.407
#> $ Estimate: num 0.3
#> $ SE : num 0.0523
#> $ MC.Error: num 5.23e-07# 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.0521The 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 methodBased on simulation research and statistical theory, here are practical recommendations:
For simple mediation models with two paths: Use DOP method as it provides the most accurate confidence intervals.
For complex models: Use Monte Carlo methods which are more flexible and extend to higher-order products.
For hypothesis testing: Consider using the MBCO procedure which provides better Type I error control.
For publication: When possible, report results from multiple methods to show robustness.
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.