Determine the significance of a mediation effect in R, the Sobel test is a statistical test to determine the significance of a mediation effect.
A mediation effect occurs when the relationship between two variables (X and Y) is explained by the presence of a third variable (M).
The Sobel test is used to determine whether the mediation effect is statistically significant or not.
In R, there are several packages that can be used to conduct a Sobel test. One of the most commonly used packages is ‘mediation’.
In this article, we will demonstrate how to conduct a Sobel test using the ‘bda’ package in R.
Computational Gastronomy for Data Science » Data Science Tutorials
Example 1: Simple Mediation
In this example, we will use a dataset named ‘mtcars’ that is included in the R base package. We will test whether the relationship between mpg and hp is mediated by the variable wt.
First, we need to load the ‘bda’ package:
library(bda)
Next, we need to define the variables:
mtcars_data <- mtcars X <- mtcars_data$hp[1:10] M <- mtcars_data$wt[1:10] Y <- mtcars_data$mpg[1:10]
We can create a simple mediation model using the ‘mediate.test’ function:
mediation.test(X,M,Y)
Sobel Aroian Goodman z.value -1.5071302 -1.4743966 -1.5421454 p.value 0.1317773 0.1403748 0.1230383
The output will display the estimated mediation effect, the standard error, the z-value, and the p-value of the Sobel test.
Example 2: Multiple Mediators
In this example, we will use a hypothetical dataset with multiple mediators to test the indirect effect of X on Y.
We define the variables:
X <- rnorm(100) M <- rnorm(100) Y <- rnorm(100)
We can create a mediation model with the two mediators using the ‘mediate.test’ function:
mediation.test(X,M,Y)
Sobel Aroian Goodman
z.value 0.09745783 0.09081421 0.1058115
p.value 0.92236283 0.92764022 0.9157319
The output will display the estimated mediation effect, bias-corrected confidence intervals, and the p-value of the Sobel test.