knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(RMediation)
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]]) }
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])) } }
# 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") str(result_dop)
# 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") str(result_mc)
# 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") str(result_asymp)
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
Based 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.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.